🤔

HomebrewでYarnが怒られた話

2021/02/25に公開

はじめに

以前、Homebrewを使用して、anyenvとnodenvを使ったNode.jsの開発環境を構築しました。
パッケージマネージャであるYarnを使おうとHomebrewからインストールしたのですが、brew doctorを実行したところ以下のWarning:が出力され焦りました。

$ brew doctor

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause formulae that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  yarn

使用上あまり問題はありませんが、毎回表示されるのは精神衛生上あまり良くないので解決策を探してみました。この記事は解決の備忘録となります。

環境・バージョン等

  • macOS Big Sur 11.2.1
  • HomeBrew 3.0.2
  • anyenv 1.1.2
  • nodenv 1.4.0+3.631d0b6
  • Node.js 14.15.4
  • yarn 1.22.10

ここまでやったこと

https://github.com/anyenv/anyenv#homebrew-for-macos-user

上記を参考にHomebrewでanyenvをインストール、anyenv経由でnodenvをインストールしました。
Yarnはnpm経由ではなく、Homebrew経由でインストールしています。

# Homebrew経由でYarnをインストール
$ brew install yarn

# バージョン確認
$ yarn --version
1.22.10

無事にYarnのインストールに成功し、パッケージの管理を行えるようになりましたが、brew doctorを実行するたび毎回Warningが表示されるようになりました。

解決策

結論としては、すでにYarnが存在しており、シンボリックリンクが切れていることが原因でした。

エラーを見てみると、一番上にYou have unlinked kegs in your Cellar.とあり、どうやらリンクが上手くできないために表示されているようです。
Cellarとは英語で「貯蔵庫」を意味し、Homebrewではコマンドの実体を格納するためのディレクトリを意味します。

Celllarの場所は以下のコマンドで調べることができます。

$ brew --cellar

/opt/homebrew/Cellar

Warning内のメッセージに従い、brew linkを試みるが。

$ brew link yarn

Linking /opt/homebrew/Cellar/yarn/1.22.10...
Error: Could not symlink bin/yarn
Target /opt/homebrew/bin/yarn
already exists. You may want to remove it:
  rm '/opt/homebrew/bin/yarn'

To force the link and overwrite all conflicting files:
  brew link --overwrite yarn

To list all files that would be deleted:
  brew link --overwrite --dry-run yarn

どうやら、シンボリックリンクの作成に失敗しているようです。

$ rm `/opt/homebrew/bin/yarn`

メッセージに従い /opt/homebrew/bin/yarn ディレクトリを削除して、競合するファイルを上書きしつつリンクを行いました。

$ brew link --overwrite yarn

Linking /opt/homebrew/Cellar/yarn/1.22.10... 2 symlinks created.

2つのシンボリックリンクが作成されました。これで上手くシンボリックリンクが作成されたようです。
もう一度brew doctorで問題がないか診てもらいましょう。

$ brew doctor

Your system is ready to brew.

Warningがきれいさっぱりなくなりました。

おわりに

Homebrewを使ってまだ日が浅いため、わからないことが多くありましたが、無事に問題を解決できてよかったです。Homebrewはbrew doctorで問題をわかりやすく提示してくれるのがいいですね。
結果的に今回のエラーがなぜ起こっていたのかわかりませんでしたが、ずっと放置していた警告メッセージがなくなり今後ぐっすり眠れそうです。

同じような問題を抱えている方の参考になれば幸いです。

最後まで読んでいただきありがとうございました。

参考

GitHubで編集を提案

Discussion