安装 Redis 时发生错误的解决办法

我打算尝试一下Hubot,并进行设置环境,但在此过程中遇到了问题,现在将解决方案总结如下。

设定好的环境

MacBook Pro(具有Retina显示屏 13英寸、2015年早期款)
处理器:2.9 GHz 英特尔 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,但仍然出现错误。而且还是另一种错误。
看起来是指定的所有者有问题。
让我们重新集中注意力,按以下方式执行。
※ 请根据您自己的环境更改<用户名>。

$ 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」的错误。