The other answers are correct, as far as they go, but they don’t answer why this issue might be occurring, and how to address that root cause.
There are two possible causes to this issue:
- The homebrew installation was performed with a user other than the one you are currently using. Homebrew expects that only the user that installed it originally would ever want to use it.
- You installed some software that writes to /usr/local without using brew. This is the cause
brew doctorsuggests, if you run it.
If you have multiple user accounts, and you want more than one of them to be able to use brew, you need to run through a few steps, otherwise you will constantly have to change ownership of the Homebrew file structure every time you switch users, and that’s not a great idea.
Detailed instructions can be found online, but the quick answer is this:
- Create a group named brew:
- Open System preferences
- Click Accounts
- Click the “+” (unlock first if necessary)
- Under New account select Group
- enter brew
- Click Create Group
- Select the brew group, and add the user accounts you want to use brew to it.
- change the /usr/local folder group ownership:
sudo chgrp -R brew /usr/local
- change the permissions to add write to /usr/local as group:
sudo chmod -R g+w /usr/local
- change homebrew cache directory group:
sudo chgrp -R brew /Library/Caches/Homebrew
- change the homebrew cache directory permissions:
sudo chmod -R g+w /Library/Caches/Homebrew
Single User Homebrew
If you’re not trying to use more than one user with Homebrew, then the solution provided by the other answers, based on the suggestions of
brew doctor is probably sufficient:
sudo chown -R $(whoami) /usr/local
sudo chown -R $(whoami) /Library/Caches/Homebrew
After these steps,
brew doctor should report success by any user in the brew group, assuming you’ve logged out and back in to apply the new group memberships (if you went the multiuser route). If you just corrected things for single user homebrew, then logging out and back in shouldn’t be necessary as none of your group memberships have changed.