43
votes

I have a standard homebrew install inside of usr/local/

When I try:

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

And when I use sudo:

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

What am I doing wrong?

7
You can sudo brew install but you need to first change the permissions of the brew utility so it is owned by root. This is a safety feature.mxcl
Seems like the method below changes the permissions of the brew utility, am I right?Andrew Lauer Barinov

7 Answers

124
votes

You somehow have limited permissions to /usr/local/Cellar. Brew doesn't like to install with sudo which is why it refuses.

Check the permissions:

ls -ld /usr/local/Cellar

Open them up for writing:

sudo chmod a+w /usr/local/Cellar

9
votes

Do not use sudo when working with brew (for security reasons).

You've to simple set-up your permissions.

So I would go even further and change the permissions to:

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

and then apply the specific group (either admin or staff) to user which should be allowed to use brew command. Check groups of your user via: id -Gn).

If there are further issues, run: brew doctor to see what's wrong.

6
votes

I'd change the group permissions:

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

assuming your user account is in group admin.

1
votes

This also happens if you have multiple users on your machine. If so, it would be best for you to change the user since every other approach would have you messing around with a lot more files and folders than just /usr/local/Cellar

Use su userWhoInstalledBrew.

0
votes

The problem can be solved by changing the directory's owner to the current user:

sudo chown -R $USER /usr/local

This answer is taken from: https://github.com/Homebrew/homebrew/issues/17884

0
votes

Following the advice chukcha14 provided in his answer at There is no Cellar file in my usr/local dir for brew, I did this:

jaimes-mbp:SMR jaimemontoya$ brew install [email protected]
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 [email protected]
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$ 
0
votes

funny but I received the Error: Cannot write to /usr/local/Cellar message due to lack of disk space .. :/ ( 18MB left )