'How to Fix Permissions on Home-brew on MacOS High Sierra

When I tried to install python onto homebrew it downloaded it and then an error message popped up at the end that stopped it from completing. When I try to do it again it asks me to do:

$ brew link python

After entering that the same error message appears:

permission denied @ dir_s_mkdir - /usr/local/lib

I have tried to do:

$ sudo chown -R $(whoami) /usr/local

And I get an error message that reads:

chown: /usr/local: Operation not permitted



Solution 1:[1]

sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks    
brew link python3

Solution 2:[2]

sudo mkdir /usr/local/Frameworks

sudo chown $USER /usr/local/Frameworks

And then try re-installing python. This worked absolutely fine for me.

Solution 3:[3]

Run this, and follow its suggestions:

brew doctor

In my case, it wanted me to run:

sudo mkdir -p /usr/local/sbin /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/sbin /usr/local/Frameworks

Solution 4:[4]

I tried and had this same ( I think ) output:

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

So I created a directory /usr/local/Frameworks as root, and than change the ownership:

sudo mkdir /usr/local/Frameworks && chown $USER:admin /usr/local/Frameworks

I tried again brew install python:

Warning: python 2.7.14 is already installed, it's just not linked.
You can use `brew link python` to link this version.

And then brew link python:

Linking /usr/local/Cellar/python/2.7.14... 26 symlinks created

Now in directory /usr/local/Frameworks/Python.framework/ I can see links, for example:

lrwxr-xr-x 1 niquit admin  62 Dec  8 21:41 /usr/local/Frameworks/Python.framework/Headers -> ../../Cellar/python/2.7.14/Frameworks/Python.framework/Headers/
lrwxr-xr-x 1 niquit admin  61 Dec  8 21:41 /usr/local/Frameworks/Python.framework/Python -> ../../Cellar/python/2.7.14/Frameworks/Python.framework/Python*
lrwxr-xr-x 1 niquit admin  64 Dec  8 21:41 /usr/local/Frameworks/Python.framework/Resources -> ../../Cellar/python/2.7.14/Frameworks/Python.framework/Resources/

In your case, I suggest create manually /usr/local/lib:

sudo mkdir /usr/local/lib && chown $USER:admin /usr/local/lib

A made a test by mv /usr/local/lib{,.orig}, and I got:

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/lib
Error: Permission denied @ dir_s_mkdir - /usr/local/lib

Like before I created manually directory sudo mkdir /usr/local/lib && chown $USER:admin /usr/local/lib, and successful did brew link python:

Linking /usr/local/Cellar/python/2.7.14... 324 symlinks created

Now I can find some links:

lrwxr-xr-x 1 niquit admin  54 Dec  8 22:01 python-2.7.pc -> ../../Cellar/python/2.7.14/lib/pkgconfig/python-2.7.pc
lrwxr-xr-x 1 niquit admin  50 Dec  8 22:01 python.pc -> ../../Cellar/python/2.7.14/lib/pkgconfig/python.pc
lrwxr-xr-x 1 niquit admin  51 Dec  8 22:01 python2.pc -> ../../Cellar/python/2.7.14/lib/pkgconfig/python2.pc

I think that Apple after latest update increased security, so its not possible to create now directory in /usr/ without root permission.

Solution 5:[5]

In my case with MacOS 10.14 fresh installation in a new machine:

brew doctor

And it suggests:

sudo mkdir -p /usr/local/lib /usr/local/sbin
sudo chown -R $(whoami) /usr/local/lib /usr/local/sbin

Solution 6:[6]

/usr/local can no longer be chown in High Sierra. Instead use

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

Solution 7:[7]

Command for users on macOS

bash/zsh:

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

fish:

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

Solution 8:[8]

Uninstalling and doing a clean install of homebrew will fix the issue.

Solution 9:[9]

I reinstalled brew and fixed the issue.

to uninstall use the following command.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

and to install brew again.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Solution 10:[10]

brew doctor

This command, as suggested by Chris above in the comments, solved almost ALL my home-brew issues running on a Mac Mini 2011 5,1 unofficially running MacOS Mojave. Simply enter that and follow all directions that terminal prints

Solution 11:[11]

As mentioned, by henrikstroem, backticks can be helpful, and as mentioned by bnaecker special attributes might also hinder the process.

You might also try to run the command directly as root to see if better results, by doing sudo su - and then chown -R username /usr/local

But are you sure that is really what you want? It might be more interesting to create a group that has an access on it (like chown -R originaluser:group /usr/local, set the rights you want, and/or to make your user part of that group.

Solution 12:[12]

macOS provides a number of ways to control access to files, beyond the traditional permissions for user, group, and other. This includes access control lists (ACLs), file flags, extended attributes (xattrs), and, more recently, Apple's System Integrity Protection.

I'm betting that if you run ls -lO /usr/, to list the flags, you'll see uchg in front of /usr/local, which instructs the system to make the file immutable by any user. (The u in uchg means that the file owner may modify this flag. Neither the owner nor any other user may modify the file itself.)

To solve the problem, you'll first need to remove the flag by running: chflags nouchg /usr/local. This should remove the uchg flag, which you should verify again with ls -lO. If another flag, such as schg is set, use noschg, or no<flag> in general, but you'll need to sudo the commands when the flag starts with s.

At this point, you may still need to chown the directory, with sudo chown -R $(whoami) /usr/local. You should now own the directory, and Homebrew tools should work fine.

Solution 13:[13]

I just did this and it worked ok:

sudo touch /usr/local/Frameworks
brew link python

Solution 14:[14]

Using this command

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