brew install redisでErrorが発生したときの解決策


Hubotでもいじってみようかと思い、環境を設定していると問題が発生したので解決策をまとめておきます。

設定した環境

MacBook Pro (Retina 13-inch、Early 2015)
プロセッサ:2.9 GHz Intel Core i5
メモリ:8 GB 1867 MHz DDR3
バージョン:10.11.4

まずbrew install redis
をしたところ、パーミッションがないと表示されてしまいました。

$ brew install redis
==> Downloading https://homebrew.bintray.com/bottles/redis-3.0.5.el_capitan.bott
######################################################################## 100.0%
==> Pouring redis-3.0.5.el_capitan.bottle.tar.gz
Error: Permission denied - /usr/local/var
Warning: Bottle installation failed: building from source.
==> Downloading http://download.redis.io/releases/redis-3.0.5.tar.gz
######################################################################## 100.0%
==> make install PREFIX=/usr/local/Cellar/redis/3.0.5 CC=clang
Error: Permission denied - /usr/local/var

では、sudo付けて実行すれば解決するかと思い、試してみました。

$ sudo brew install redis
Password:
Error: Cowardly refusing to `sudo brew install`
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.

今度はまた別のエラーが…
調べてみると、chownというコマンドでファイルやディレクトリの所有者を変更することで解決するとあったので、試してみます。
以下のコマンドでは、所有者を”root”に設定しました。

$ sudo chown -R root /usr/local
$ brew install redis
Error: Cannot write to /usr/local/Cellar
counterport-cafe:Documents ken$ brew install node
Warning: node-5.0.0 already installed
Error: Cannot write to /usr/local/Cellar

それでもう一度、brewしてみますが、やはりエラーが出てしまいました。しかもまた違うエラー。
どうやら、指定した所有者が悪かったらしい。
気を取り直して、以下で実行。
※ <username> は自分の環境に合わせて、書き換えてください。

$ sudo chown -R <username> /usr/local
$ brew install redis
==> Downloading https://homebrew.bintray.com/bottles/redis-3.0.5.el_capitan.bott
Already downloaded: /Library/Caches/Homebrew/redis-3.0.5.el_capitan.bottle.tar.gz
==> Pouring redis-3.0.5.el_capitan.bottle.tar.gz
==> Caveats
To have launchd start redis at login:
  ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
Then to load redis now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
Or, if you don't want/need launchctl, you can just run:
  redis-server /usr/local/etc/redis.conf
==> Summary
🍺  /usr/local/Cellar/redis/3.0.5: 9 files, 892K

無事に通りました!

参考資料

『brew install goを実行すると「Error: Cannot write to /usr/local/Cellar」』
http://anton0825.hatenablog.com/entry/20140606/1409898700