112

Attempting to install rvm and ruby 1.9.2

I already installed homebrew and git, but couldn't get complete updates because I kept getting permission errors. Re-installed Snow Leopard and repaired permissions.

Now this happens...

$ brew install wget

Error: Cannot write to /usr/local/Cellar

Community
  • 1
  • 1
Ibrahim
  • 1,129
  • 2
  • 8
  • 3
  • http://stackoverflow.com/questions/4804169/installing-in-homebrew-errors#5112493 should be marked as accepted answer - i fear that visitors who do not see that this question has not been answered they will move on – dmo Sep 26 '15 at 19:23

8 Answers8

275

sudo chown -R $USER /usr/local

You'll have to give yourself ownership of /usr/local/ using that line right there. I had to do this myself after using the ruby one-liner at the top of the official docs to install Homebrew. Worked like a charm for me. It ought to be the only time you'll ever need to sudo with Homebrew.

I'm not sure if the ruby one-liner does this. If it did, then something else on my system took control of /usr/local since.

Edit: I completely missed this, but @samvermette didn't (see replies to my answer): if you run this command above and have something installed via homebrew that requires special user permissions, like mysql, make sure to give those permissions back (as the above command gives recursive ownership to everything inside /usr/local to you ($USER). In the case of mysql, it's…

sudo chown -RL mysql:mysql /usr/local/mysql/data

Community
  • 1
  • 1
Ben Kreeger
  • 6,355
  • 2
  • 38
  • 53
  • 16
    **Please note**: this command is going to remove ownership of /usr/local/mysql/data from the `mysql` user. In my case that prevented mysql from starting up. Fix that with: `sudo chown -RL mysql:mysql /usr/local/mysql/data` – samvermette Jun 22 '11 at 03:18
  • 24
    I avoided the mysql issue by using `sudo chown -R $USER /usr/local/Cellar` as `/usr/local/` already had the correct permissions for me – Parker Feb 15 '12 at 23:38
  • I'm confused, does mysql need ownership of everything that is within mysql? Such as /var/local/var/mysql and /usr/local/Cellar/ ?? What about /tmp which is where mysql looks for the sock file? I've rattled my brain over the past 24 hours over getting a brew mysql to run properly. Then I get write errors on brew doctor where the directories are 777. It's very annoying and everyone seems to have a different answer. – AlxVallejo Jul 18 '14 at 01:12
  • On OSX Mavericks I had to also run "sudo rm -rf /Library/Caches/Homebrew" because there were some permission issues in there from when I installed some brews with sudo. – David Mann Nov 03 '14 at 19:35
  • 4
    I dislike this answer. It assumes that only one user ever uses the system and that user should own everything /usr/local, despite the question being only about homebrew. Does brew not work with multiuser systems? It's a sloppy answer, even if it does work. Furthermore, it doesn't explain how a user might have encountered this situation. Hint - I installed HomeBrew using the canonical instructions, then restored from a TimeMachine backup (to another machine). I suspect it was that restore that altered the file ownership. – Jason R. Coombs Sep 20 '15 at 16:10
  • After I use this commando, I still get errors: `Error: Permission denied - /Library/Caches/Homebrew/Formula/node.brewing`. Use with caution. – Henrique de Sousa Aug 12 '16 at 10:18
  • 1
    I agree with @JasonR.Coombs; people should not be using this sloppy solution without understanding the practical and security ramifications. Any software in /usr/local may now be overwritten by anything running under your user, opening a security hole. It is entirely possible you will screw something else up (in addition to MySQL) by blindly running the first chown command, after which point it will be too late to identify and fix everything that you may have just screwed up. – rob Aug 20 '16 at 18:23
  • gives `chown: /usr/local: Operation not permitted` in macOS 10.13.3 – Kaushik Nayak May 26 '18 at 10:27
41

I had this issue after upgrading to Mavericks, and this page was the top search result when googling the error message. I continued searching and found this answer on stack overflow.com. Put concisely, it is:

sudo chmod a+w /usr/local/Cellar

This fixed the issue for me, and as it only changes permissions for the specific path referenced in the error message, seemed unlikely to have negative side effects with other installations.

I'm putting this answer here for anyone else who may find this page first like I did. However, credit should go to jdi.

Community
  • 1
  • 1
Nathan
  • 437
  • 4
  • 2
11

You can allow only Admin users writing into /usr/local/?

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

Since that each user who belongs to Admin group, will be able to install new dependencies.

Seb Wilgosz
  • 1,240
  • 15
  • 24
  • Looks like a better solution for multi-administrator machines (although my /usr/local/lib directory also needed g+x for aspell to work). – James Snook Jul 18 '16 at 14:43
  • 1
    To avoid getting the 'The "brew link" step did not complete successfully' error you could also apply what's in [this thread](http://stackoverflow.com/questions/26647412/homebrew-could-not-symlink-usr-local-bin-is-not-writable), namely `sudo chgrp -R admin /usr/local/DDDD` + `sudo chmod -R g+w /usr/local/DDDD` where DDDD is _bin_, _share_, _Library/LinkedKegs_, _opt_ – superk Dec 03 '16 at 15:22
  • 1
    Thnx for the anwer, i had to use "sudo", so if these command does not work, try to prefix it with sudo – MQoder Mar 12 '17 at 20:45
5

On High Sierra you need the following command cause chown will not work:

sudo chown -R $(whoami) $(brew --prefix)/*

Link:

https://github.com/Homebrew/brew/issues/3228

Metodij Zdravkin
  • 914
  • 1
  • 14
  • 23
2

uninstall and re install HomeBrew that will do the trick

1

I suggest ensuring that the current user is a member of the group that owns /usr/local. I believe by default, that group is wheel. To make yourself a member of that group:

$ sudo dscl . append /Groups/wheel GroupMembership $USER

Although something of an inelegant hammer, it has the intended effect - enabling access to items in /usr/local that are intended only for use (read/write) by elevated members. This approach has benefits of the other above because it takes advantage of the group memberships, enabling multiple (authorized) users on the system to use homebrew.

Jason R. Coombs
  • 41,115
  • 10
  • 83
  • 93
0

How did you install Homebrew? Their official installation instructions include running a ruby script. That should take care of the permission issues for you.

If you don't want to run a script, there is a section of that page called "Installing to /usr/local for Developers" that explains the change in permissions needed for the /usr/local directory.

jhuebsch
  • 451
  • 2
  • 8
  • i used a script and it installed but it still had problems updating. I migrated user data onto a new laptop. Had to do a few workarounds from github b/c the script didn't work at first. – Ibrahim Jan 26 '11 at 20:10
-3

EDIT: As mentioned in the comments it's a bad idea to use sudo with homebrew, so don't use the following answer!


You can also prevent this error if you execute the command with sudo:

$ sudo brew install wget

But take care of using sudo because you can make a lot of mistakes.

Pascal
  • 41
  • 1
  • 3