Homebrewがアップデートできない時の対処法

しばらくHomebrewを使っていなくて、mongodbを使うために久々にコマンドを打ったらエラーが出た。

$ brew update
/usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory
/usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0

OSをバージョンアップさせると、このような現象が起こるらしい。 これからまたSierraにバージョンアップする予定があるのだが。。 まあ、また同様のエラーが起きた時のためにも、手順をまとめておく。

エラー対処法

このエラー自体はHomebrewのissueになっている のだが、既にクローズしており以下の操作が必要。

# cd /usr/local/Library
# git pull origin master

ディレクトリーを/usr/local/Library以外に変更した場合は、上記の操作でエラーが出る。 僕は上記で成功したので未検証だが、その場合は以下の操作が必要。

# git fetch --all
# git reset --hard origin/master

確認

早速さっき失敗したupdateを実行。問題なく終了。

# brew update
remote: Counting objects: 934, done.
...

インストール済みのformulaを確認。 mongodbは以前CentOSで使っていたと思ったら、Macでも使っていた。。

# brew list
... mongodb ...

バージョンを確認したら、だいぶ古い。

# brew outdated
...
mongodb (2.2.2-x86_64) < 3.2.9
...

いい機会なので、更新のあるformulaをすべてupgrade。 結構時間がかかるのでターゲットのformulaだけでいい場合は、それをupgradeの次に記述。

# brew upgrade
...
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.2.9.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.2.9.el_capitan.bottle.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
...

更新のあるformulaを確認して、すべて更新されたか確認。

# brew outdated
#

古いformulaが残ったままなので削除。

# brew cleanup -n
...
Would remove: /usr/local/Cellar/mongodb/2.2.2-x86_64 (20 files, 170M)
...
#

その他

実は、やたらエラーが出まくってたのだが、 El Capitanから/usr/local以下が書き換えられなくなったと聞いた気がするけど、それが原因かも。

Error: Running Homebrew as root is extremely dangerous. As Homebrew does not
drop privileges on installation you are giving all build scripts full access
to your system. As a result of the macOS sandbox not handling the root user
correctly HOMEBREW_NO_SANDBOX has been set so the sandbox will not be used. If
we have not merged a pull request to add privilege dropping by November 1st
2016 running Homebrew as root will be disabled. No Homebrew maintainers plan
to work on this functionality.

参考文献