【開発】os x el capitanでbrew updateする

macで開発している人で、既にel capitanを使っている人は少なくないと思います。ただel capitanでは「System Integrity Protection」というセキュリティ機能が追加されたため、開発する時には注意が必要です。

そこでこの記事は、os xのパッケージ管理システムの一つ「Homebrew」をうまく動作させるためにやることをまとめます。

現象

DBにpostgreSQLを使ったプロジェクトをローカルで動かしたいと思い、brew install postgreSQLpostgreSQLをインストールしようとしたところ、以下のようなエラーが。

(略)
==> Installing postgresql dependency: openssl
==> Downloading https://www.openssl.org/source/openssl-1.0.2a.tar.gz

curl: (22) The requested URL returned error: 404 Not Found
Trying a mirror...
==> Downloading https://raw.githubusercontent.com/DomT4/LibreMirror/master/OpenSSL/openssl-1.0.2a.tar.gz

curl: (22) The requested URL returned error: 404 Not Found
Error: Failed to download resource "openssl"
Download failed: https://raw.githubusercontent.com/DomT4/LibreMirror/master/OpenSSL/openssl-1.0.2a.tar.gz

ネットでいろいろ調べているとbrew updateすると解決するって書かれていたので実行するも、

error: unable to unlink old '.gitignore' (Permission denied)
error: unable to create file .travis.yml (Permission denied)
error: unable to unlink old '.yardopts' (Permission denied)
error: unable to unlink old 'README.md' (Permission denied)
error: unable to unlink old 'SUPPORTERS.md' (Permission denied)
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

といった感じで怒られました。

原因

どうやらos xにアップグレードするときに/usr/localパーミッションが変わってしまったらしいです。

解決策

まずはusr/localの所有者を変更します。

chown user_name:admin /usr/local && sudo chown -R user_name:admin /usr/local

これでbrew updateしてみましたが次のようなエラーが出ました。

(略)
Library/Homebrew/utils/git.rb already exists, no checkout
Could not restore untracked files from stash

よくわからないのですがどうやらgitがおかしいようです。 公式サイトを参考に以下のコマンドを実行します。

cd $(brew --prefix) && git fetch && git reset --hard origin/master

今度はちゃんとbrew updateできるようになりました。 同じようなエラーが出る人がいたらこれをやってみるといいかもしれません。

参考