83

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

the
  • 21,007
  • 11
  • 68
  • 101
Jonathan
  • 855
  • 1
  • 6
  • 7
  • Use ticks: `\`whomai\`` if you for some reason don't want to type your username. – henrikstroem Nov 27 '17 at 14:18
  • 4
    Generally, this is a better fit for [unix.se] or [apple.se], not being directly about *the process of writing software* as is on-topic here. – Charles Duffy Dec 06 '17 at 23:22
  • Does `/usr/local/` have any ACL parameters, file flags, or extended attributes? Use `ls -le` to check for ACLs, `ls -lO` for flags, and `ls -l@` for xattrs. – bnaecker Dec 06 '17 at 23:34
  • See brew fix here: https://gist.github.com/irazasyed/7732946 – Zlemini Dec 07 '17 at 08:16
  • @CharlesDuffy This _is_ the appropriate place for this question. See https://stackoverflow.com/help/on-topic the guidelines say SO is for questions relating to "software tools commonly used by programmers" and installing Python in a local development environment definitely is a programming task. – Martin Joiner Jan 30 '19 at 11:09
  • @MartinJoine, but this question isn't specific to Python in any meaningful way. The issue at hand applies to *any* software on UNIX, not the Python interpreter (or otherwise, software-development tools) alone. We don't support someone asking how to repair their CRT because they're trying to use the computer to write programs; similarly, we don't support a generic UNIX question if it's equally applicable to non-programming-related purposes. See the phrase "*unique to software development*" in the same page you linked to. – Charles Duffy Jan 30 '19 at 14:35

14 Answers14

150
sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks    
brew link python3
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
gfly
  • 1,508
  • 1
  • 9
  • 5
  • It worked for me. I don't know what magic is in the commands but it saved my life. Thanks @Chris Krycho – Shiva Krishna Chippa Jun 08 '18 at 11:08
  • 1
    @ShivakrishnaChippa the magic above creates: 1) the `/usr/local/Frameworks` directory; 2) changes that directory ownership to the current user and finally 3) `brew link` creates symlinks to installations you performed manually in `Cellar`. More info on `brew link` here https://stackoverflow.com/questions/33268389/what-does-brew-link-do – Sylvester Loreto Jun 14 '18 at 10:35
  • It worked for me. But still if I give python -V, it is giving me 2.7.10 – Kishor kumar R Dec 22 '18 at 12:21
  • 1
    This worked for macos mojave as well. but install of `brew link python3` I choose to do a `brew uninstall python` and then `brew install python` – ndrone Apr 20 '19 at 20:21
  • worked, but required a `brew reinstall python` as well. – Scott Willeke Feb 09 '20 at 04:18
  • Worked for me (at least for getting rid of the brew doctor warning) without the need to reinstall python – Florag Dec 24 '20 at 14:01
21
sudo mkdir /usr/local/Frameworks

sudo chown $USER /usr/local/Frameworks

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

SelftaughtMonk
  • 987
  • 8
  • 18
15

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
Chris
  • 39,719
  • 45
  • 189
  • 235
10

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.

Niquit
  • 113
  • 8
  • it's worse than that actually. you can't modify /usr/local/ without disabling system integrity protection. it has permissions higher than root. – Walrus the Cat Dec 09 '17 at 19:53
  • @WalrustheCat `/usr/local/` is specifically *not* included in the files protected by SIP. See [this](https://support.apple.com/en-us/HT204899). – bnaecker Dec 09 '17 at 22:10
  • mine is? and i've seen results on the internet saying the same thing? – Walrus the Cat Dec 09 '17 at 23:51
  • @WalrustheCat Your `/usr/local` directory lists `restricted` next to it when you run `ls -lO /usr`? That sounds like a *big* problem, and definitely not the way it should be set up. All official Apple docs I've seen (and plenty of unofficial stuff) explicitly *exclude* `/usr/local` from SIP. I wonder how it got that way. – bnaecker Dec 09 '17 at 23:57
8

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
Bill Chan
  • 3,199
  • 36
  • 32
6

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

sudo chown -R $(whoami) $(brew --prefix)/*
H. Gourlé
  • 884
  • 7
  • 15
6

Command for users on macOS

bash/zsh:

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

fish:

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

Rajat Jain
  • 1,339
  • 2
  • 16
  • 29
4

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

Walrus the Cat
  • 2,314
  • 5
  • 35
  • 64
  • I don't quite see how this could work. Homebrew will just try to re-install to `/usr/local/`, and if the permissions are already out of wack, how would that behave differently than the current installation trying to create symlinks? I guess you could force Homebrew to use a different `brew --prefix`. – bnaecker Dec 09 '17 at 22:20
  • How did that work if you have SIP protecting `/usr/local`, as you mentioned in your other comment? How did you even delete the previous installation of Homebrew? – bnaecker Dec 09 '17 at 23:59
  • 1
    The brew install script from brew.sh does things that brew doctor does not (yet) know to take care of. – Simon B. Jan 21 '18 at 02:13
4

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)"
3
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

0

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.

user1747036
  • 524
  • 4
  • 12
  • I don't think there's a difference between `sudo command` and `sudo su -; command` except for the resulting environment in which `command` runs. And the question is about Homebrew, which definitely requires that the user actually own `/usr/local/` to work correctly. – bnaecker Dec 07 '17 at 02:20
  • I guess on a monouser system it can't be that bad – user1747036 Dec 07 '17 at 02:26
  • guys none of your answers even come close to touching this problem. i know for a fact you didn't even *try* to *do* what is asked here. – Walrus the Cat Dec 09 '17 at 19:54
  • @WalrustheCat How exactly would we "try" anything? We don't have the problem the OP has, and I'm not about to put in place some obviously difficult-to-disable permissions on one of my most-used directories. – bnaecker Dec 09 '17 at 22:14
0

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.

bnaecker
  • 6,152
  • 1
  • 20
  • 33
-1

I just did this and it worked ok:

sudo touch /usr/local/Frameworks
brew link python
charles ross
  • 570
  • 4
  • 15
  • 1
    Did I say otherwise? Looks like @Chris' answer is better. Why not post a helpful comment rather than throw stones? – charles ross Nov 15 '18 at 15:27
-1

Using this command

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