Homebrew安装权限问题

43

我在/usr/local/下安装了标准的Homebrew。

当我尝试运行:

Larson-2:~ larson$ brew install postgresql
Error: Cannot write to /usr/local/Cellar

当我使用sudo时:

Larson-2:~ larson$ sudo brew install postgresql
Cowardly refusing to `sudo brew install'

我哪里做错了?


你可以使用 sudo brew install 命令,但是你需要先更改 brew 工具的权限,使其归属于 root 用户。这是一项安全特性。 - mxcl
似乎下面的方法更改了brew实用程序的权限,我是对的吗? - Andrew Lauer Barinov
7个回答

124

您似乎对 /usr/local/Cellar 有限制权限。Brew 不喜欢使用 sudo 进行安装,这就是为什么它拒绝安装的原因。

检查权限:

ls -ld /usr/local/Cellar

开放写入权限:

sudo chmod a+w /usr/local/Cellar


1
不错,我看过其他方法,例如将 /usr/local/Cellar 的所有者更改为 whoami,或允许组在 /usr/local 上进行写操作。 - orlybg
6
之后我收到了“错误:权限被拒绝 - /Library/Caches/Homebrew/Formula/...”的信息,使用“sudo chmod -R a+w /Library/Caches/Homebrew”命令解决了问题。 - gfpacheco
谢谢!我更新了目录,但R安装仍然显示权限被拒绝。 - Shark Deng

9

出于安全原因,在使用 brew 时请勿使用 sudo

您只需要简单设置权限即可。

因此,我建议进一步更改权限为:

sudo chgrp -R admin /usr/local /Library/Caches/Homebrew
sudo chmod -R g+w /usr/local /Library/Caches/Homebrew

然后将特定的组(adminstaff)应用于允许使用brew命令的用户。通过id -Gn检查您的用户组。

如果有进一步的问题,请运行:brew doctor查看错误信息。


6

我会更改群组权限:

$ chgrp -R admin /usr/local/Cellar
$ chmod g+w /usr/local/Cellar

假设您的用户帐户在管理员组中。

1
如果您的机器上有多个用户,则也会出现这种情况。如果是这样,最好更改用户,因为任何其他方法都需要您处理比 /usr/local/Cellar 更多的文件和文件夹。
使用 su userWhoInstalledBrew

0

对于High Sierra用户的更新,/usr/local不再可以被chown(参见此处)。同时,High Sierra用户可能也需要重新安装Homebrew。 - Grace Mahoney

0

有趣的是,由于磁盘空间不足,我收到了Error: Cannot write to /usr/local/Cellar错误信息.. :/ (剩余18MB)


0
根据chukcha14在There is no Cellar file in my usr/local dir for brew中提供的建议,我做了以下操作:
jaimes-mbp:SMR jaimemontoya$ brew install mongodb-community@4.2
Warning: You are using OS X 10.15.
We do not provide support for this pre-release version.
You may encounter build failures or other breakages.
Error: Could not create /usr/local/Cellar
Check you have permission to write to /usr/local
jaimes-mbp:SMR jaimemontoya$ sudo mkdir /usr/local/Cellar
Password:
jaimes-mbp:SMR jaimemontoya$ sudo chown $(whoami) /usr/local/Cellar
jaimes-mbp:SMR jaimemontoya$ brew install mongodb-community@4.2
Warning: You are using OS X 10.15.
We do not provide support for this pre-release version.
You may encounter build failures or other breakages.
==> Installing mongodb-community from mongodb/homebrew-brew
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.2.3.tgz
###################################################################################### 100.0%
Error: Failed to install plist file
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink .
/usr/local/opt is not writable.

You can try again using:
  brew link mongodb-community
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall mongodb/brew/mongodb-community`
==> Caveats
To have launchd start mongodb/brew/mongodb-community at login:
  ln -sfv /usr/local/opt/mongodb-community/*.plist ~/Library/LaunchAgents
Then to load mongodb/brew/mongodb-community now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
Or, if you don't want/need launchctl, you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
  /usr/local/Cellar/mongodb-community/4.2.3: 20 files, 304M, built in 110 seconds
jaimes-mbp:SMR jaimemontoya$ sudo mkdir /usr/local/opt
jaimes-mbp:SMR jaimemontoya$ sudo chown $(whoami) /usr/local/opt
jaimes-mbp:SMR jaimemontoya$ brew link mongodb-community
Linking /usr/local/Cellar/mongodb-community/4.2.3... 13 symlinks created
jaimes-mbp:SMR jaimemontoya$ 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接