python - pip3 - touch:/usr/local/homebrew/.git/fetch_head: permission denied

How to Fix Permissions on Home-brew on MacOS High Sierra (6)

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

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

When I tried to install python onto home-brew 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"

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.

I reinstalled brew and fixed the issue.

to uninstall use the following command.

ruby -e "$(curl -fsSL"

and to install brew again.

/usr/bin/ruby -e "$(curl -fsSL"

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.

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

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.