blog @arfyasu

プログラミングとか趣味のこととか

brew さんのご機嫌を直した

全国8000万人の brew ファンの皆さん、こんにちは。

f:id:kanz-labs:20151012144834p:plain

みんな brew 大好きですよね。
コンソールから

$ brew install <package-name>

と魔法のコマンドを打つだけで、パッケージをインストールしてくれる、とても出来た子。

今日も、何の疑問も持たずに以下のコマンドを実行。

$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-0.12.7_1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring node-0.12.7_1.yosemite.bottle.tar.gz
Error: The `brew link` step did not complete successfully
...

おや?
エラーが出ました。

原因確認

半月前は正常に動いていたはずなのに、今日はご機嫌斜めのご様子。
ということで、何が原因かお医者様に診断してもらいましょう。

$ 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 and just ignore them. Thanks!

Warning: /usr/local/bin isn't writable.

This can happen if you "sudo make install" software that isn't managed by
by Homebrew. If a formula tries to write a file to this directory, the
install will fail during the link step.

You should probably `chown` /usr/local/bin

Warning: /usr/local/share isn't writable.

This can happen if you "sudo make install" software that isn't managed by
by Homebrew. If a formula tries to write a file to this directory, the
install will fail during the link step.

You should probably `chown` /usr/local/share

Warning: Some directories in /usr/local/share/man aren't writable.
This can happen if you "sudo make install" software that isn't managed
by Homebrew. If a brew tries to add locale information to one of these
directories, then the install will fail during the link step.
You should probably `chown` them:
    /usr/local/share/man
    /usr/local/share/man/man1

Warning: You have uncommitted modifications to Homebrew
If this a surprise to you, then you should stash these modifications.
Stashing returns Homebrew to a pristine state but can be undone
should you later need to do so for some reason.
    cd /usr/local/Library && git stash && git clean -d -f

Warning: Your Homebrew is outdated.
You haven't updated for at least 24 hours. This is a long time in brewland!
To update Homebrew, run `brew update`.

たくさん出てきましたが、実際に対処が必要なのは以下の5つです。

  • You should probably chown /usr/local/bin
  • You should probably chown /usr/local/share
  • You should probably chown them:
    /usr/local/share/man
    /usr/local/share/man/man1
  • cd /usr/local/Library && git stash && git clean -d -f
  • brew update

所有者の変更

対象のディレクトリの所有者を確認。

$ cd /usr/local
$ ls -la
...
drwxr-xr-x  105 root    wheel   3570  9 25 06:37 bin
drwxr-xr-x    9 master  admin    306  9 16 17:01 etc
drwxr-xr-x   26 master  admin    884  9 17 16:54 include
drwxr-xr-x   38 master  admin   1292 10 12 12:57 lib
drwxr-xr-x   24 master  admin    816  9 17 16:54 opt
drwxr-xr-x    4 master  admin    136  9  8 15:14 sbin
drwxr-xr-x   11 root    wheel    374  9 16 17:01 share
...

bin と share だけ所有者が root になってますね。
警告が出ていた、share/man、share/man/man1 も同様でした。

他ディレクトリと同じ所有者に変更します。

$ sudo chown -R $(whoami) /usr/local/bin
$ sudo chown -R $(whoami) /usr/local/share

master:admin の部分は、自分のPCのユーザーに合わせてください。

リポジトリをクリーンする

$ cd /usr/local/Library && git stash && git clean -d -f
Saved working directory and index state WIP on master: 7a8703c gammaray: update 2.3.0 bottle.
HEAD is now at 7a8703c gammaray: update 2.3.0 bottle.
Removing Formula/...

追跡対象外ファイルおよび追跡対象外ディレクトリがあると警告がでていたので、それらを削除します。

パッケージ一覧の更新

$ brew update
Updated Homebrew from 7a8703ce to 530b5e4f.
==> New Formulae
...

成功!

ご機嫌直ったかな?

$ brew doctor
Your system is ready to brew.

無事、直りました(^-^)

改めてインストール。

$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-4.1.2.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring node-4.1.2.yosemite.bottle.1.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/4.1.2: 2744 files, 36M

インストールできました。

所感

今回のエラーの直接の原因は、ファイルの所有者がおかしかったことでした。
update してなかったので、随分古いバージョンインストールしようとしてたので、
結果的にエラーになってくれて良かったですがw

ちなみに、ディスクユーティリティのアクセス権の検証等ではエラーとして
認識されませんので、コマンドで修正する必要があります。

リポジトリのクリーンは、今回のエラーとは直接関係なかったと思いますが、
警告をそのままにしておくのも気持ち悪かったのでついでに直しました。

どうしてファイルの所有者がおかしくなったのか?
考えられるものといえば、アップデートくらいかな?

いずれにしても、簡単に直ってよかった。