425

While installing tig, HomeBrew is displaying the following issues while installing a dependency:

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/a2x
/usr/local/bin is not writable.

You can try again using:
  brew link asciidoc
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61

15 Answers15

679

Following Alex's answer I was able to resolve this issue; seems this to be an issue non specific to the packages being installed but of the permissions of homebrew folders.

sudo chown -R `whoami`:admin /usr/local/bin

For some packages, you may also need to do this to /usr/local/share or /usr/local/opt:

sudo chown -R `whoami`:admin /usr/local/share
sudo chown -R `whoami`:admin /usr/local/opt
RonU
  • 5,525
  • 3
  • 16
  • 13
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
  • 29
    Doesn't reowning all /usr/local/bin to the current use give any troubles to the system or to other users? – Don Giulio Mar 27 '15 at 15:32
  • Haven't had an issue yet. – Jikku Jose Mar 27 '15 at 16:05
  • @don giulio, not for me – leoredi Jun 12 '15 at 12:31
  • 11
    Wouldn't `sudo chown -R `whoami`:admin /usr/local/bin` be equivalent to the command in the answer, without the need to replace `yourusename` with your actual username? – Jason Swett Jul 22 '15 at 18:26
  • 5
    I had to also chown `usr/local/opt` recursively. It worked after that. – atripes Oct 14 '15 at 17:13
  • 7
    On my mac (10.9.5) the permissions of `/usr/local/bin` (and friends) seem to revert to `drwxr-xr-x+` and `root wheel` every day. I must re-configure the perms every time I want to `brew install`. I assume the OS is resetting perms to protect me? How can I make it stop? (I've tried `chflags uchg …` but that prevents brew from installing.) – Quinn Comendant Nov 11 '15 at 22:06
  • For now I'm ensuring permissions stay brew-friendly by adding this to my `~/.bashrc` (it sets the perms upon each brew invocation): `alias brew='sudo chown -R $(whoami):admin /usr/local/{bin,share} && brew';` – Quinn Comendant Nov 11 '15 at 22:14
  • You shouldn't have to do that; that seems odd. Did you install brew into any custom location? – Jikku Jose Nov 12 '15 at 04:50
  • Oh man, I just love finding a solution for my problem with my first search result on stackoverflow! thanks @JikkuJose – Mathias Jan 28 '16 at 17:52
  • In my case I had to use "sudo chown 'myuser' man8" on all "man*" folders in /usr/local/share/man to make it work. – Paul Walczewski Jan 22 '17 at 13:34
  • You can change the permissions back after the symlink was created. – kristian Dec 29 '17 at 14:54
  • 2
    I added my account to the "wheel" group. This allows me to "sudo chmod -R g+w /usr/local" -- this allows the members of the group "wheel" to write to the folders. Seems safer than changing the owner of these sensitive folders to a less secure account than root. – Michael Krebs May 10 '18 at 21:31
  • 1
    I have the same issue. It seems that the way brew operates is no longer compatible with the MacOS. Is anyone using brew on a Mac right now who CAN install things? ADDED: The wonderful advice in this thread saved me - I first added my username to the wheel group using sudo dscl . -append /groups/wheel GroupMembership , then I was able to do the "sudo chmod -R g+w /usr/local" above, and viola! Brew works again! – user2465201 Jun 06 '18 at 17:58
  • Just a further insight: This might have to do with a bizarre confusion with case: When I looked into the brew install that finally worked, it had linked the files into /usr/local/Cellar. All the other files were in /user/local/cellar. This should have been equivalent (I'm using a case insensitive file system), but it's not - I have to explicitly refer to this one install with a Capital Cellar, and perhaps that's why this folder didn't have the right permissions and the other did? – user2465201 Jun 06 '18 at 18:15
  • After my recent upgrade to Mojave I had to do `sudo chown -R $(whoami):admin /usr/local/share/locale` to get `brew link` working again (the `brew link` error message complained that it couldn't write to `LC_MESSAGES`, this applied to every locale subfolder) – MikeBeaton Oct 30 '18 at 06:57
262

Rather than running any particular command, I would recommend running brew doctor and taking all warnings seriously. There may be other problems you get stuck at which may not be captured in this question.

Also, as brew gets updated with time, particular commands may or may not remain valid. brew doctor, however, will ensure that you get up to date troubleshooting.

zakishaheen
  • 5,551
  • 1
  • 22
  • 29
  • 9
    I can not believe your answer is less popular. It is certainly more prudent than the guessing expressed above. – Robert of Fairfax Jan 14 '16 at 08:42
  • 41
    `brew doctor` won't fix the problem, it will only tell you about it, and when you take doctor's advice, brew says it can't do it. That's not a solution – Keith Tyler Apr 21 '16 at 01:03
  • 11
    Maybe someone improved it. Now brew doctor suggests I run a similar cmd to the most popular answer, and after doing so the brew symlink works. I'm on a mac, so we'll see how long it lasts. :-) – Joshua Richardson Mar 23 '17 at 18:13
  • 2
    This was very useful thanks. Fixed some other special directories to chown the above suggestion missed. – SwimBikeRun Oct 22 '17 at 05:00
  • `brew doctor` just tells me "Warning: You have unlinked kegs in your Cellar", which is what I'm trying to do in the first place. – Nick T Jan 18 '18 at 21:46
  • 7
    brew doctor provided the EXACT problem and provided the manual solution as well! – netrox Jul 10 '18 at 03:56
  • Thanks. `brew doctor` helped me figure out other issues as well. Much appreciated. – Dave Gregory Jan 10 '19 at 23:35
88

For those who are looking for /usr/local/sbin is not writable error:

UPDATE: It could be /usr/local/someOtherFolderName e.g /usr/local/include. You just need to create that folder with:

  • sudo mkdir someOtherFolderName

First create the sbin folder, note that this requires sudo privileges

  • cd /usr/local

  • sudo mkdir sbin

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

  • brew link yourPackageName

gokcand
  • 6,694
  • 2
  • 22
  • 38
81

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.

Cause

There are two possible causes to this issue:

  1. 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.
  2. You installed some software that writes to /usr/local without using brew. This is the cause brew doctor suggests, if you run it.

Solution

Multiuser Homebrew

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:

  1. Create a group named brew:

    1. Open System preferences
    2. Click Accounts
    3. Click the "+" (unlock first if necessary)
    4. Under New account select Group
    5. enter brew
    6. Click Create Group
  2. Select the brew group, and add the user accounts you want to use brew to it.
  3. change the /usr/local folder group ownership: sudo chgrp -R brew /usr/local
  4. change the permissions to add write to /usr/local as group: sudo chmod -R g+w /usr/local
  5. change homebrew cache directory group: sudo chgrp -R brew /Library/Caches/Homebrew
  6. 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

Verification

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.

Community
  • 1
  • 1
stonecrusher
  • 1,246
  • 10
  • 12
  • 2
    Thanks! I should probably read more about unix user groups. I am totally clueless about how it works to digest the reasoning well enough. But it sounds very sensible, thanks for the pointer! – Jikku Jose Mar 09 '16 at 04:43
  • 1
    unix groups are simpler than you might assume - every file has read, write, and execute permissions available for the 'user' and 'group' that owns that file, and for 'others'. If you set the group that owns a file or folder, and add write permissions to 'group' for that folder, then any user in that group can write to the folder. There's really no more to it than that. not nearly as complex as permissions in windows. – stonecrusher Mar 10 '16 at 19:57
  • 1
    So its an easy way to club the permission system of a set of files to a group so that multiple users can be added at ease to apply all those permissions in a single go? – Jikku Jose Mar 11 '16 at 04:48
  • 2
    This fixed my brew link problems, thank you. – Keith Tyler Apr 21 '16 at 01:04
  • Still strikes me as a bit dangerous to have a sensitive directory such as `/usr/local/bin` group-writable, esp. in a multi-user environment. Is this really the "official" brew way? – corwin.amber Jun 24 '16 at 19:23
  • @corwin.amber nothing I've found indicates to me that there is any official brew position on this topic. As far as concerns about group writability, that's why I've advocated creating a group specifically for this purpose. Group writability is no more dangerous than user writability if you control the group, and works identically to user permissions. – stonecrusher Jul 04 '16 at 23:44
  • @corwin.amber if your concern is that users can install binaries and affect the environment of other users, that's entirely valid, but I don't think any more dangerous than allowing those users to install any other OSX software that requires admin privileges. I approached this with the assumption that these users were already administrators. – stonecrusher Jul 04 '16 at 23:46
  • @stonecrusher Specifically my concern is that any program that I run (or any user in the group runs) can potentially change my `/usr/local/bin` without my being aware of it. When I install things with `sudo` at least I get prompted for my password, so if the program is malicious I will catch on it. Of course you shouldn't run malicious software to begin with, but if you do, why make life easier for them. True, it can also plant binaries in other `PATH` locations, but those binaries are usually few so if smt new "appears" there, I'm more likely to notice. – corwin.amber Jul 10 '16 at 18:02
  • @corwin.amber I suppose if you find that concerning, you could set up a user specifically for managing homebrew, and use sudo -u to execute your homebrew commands as that user. However, the malicious binary really doesn't need to even place anything in any usual location - it could manipulate the path in a script like .bash_profile, or it could use a LaunchDaemon and manipulate the PATH variable itself. That said, there are bigger issues at that point. – stonecrusher Jul 12 '16 at 21:44
  • @corwin.amber For a truly multi-user system I agree there are some security downsides to this approach. In my case, I'm the only actual person using the machine - I just have two accounts to separate work and personal concerns, and I like having homebrew in both. – stonecrusher Jul 12 '16 at 21:45
  • @corwin.amber Homebrew already doesn't require additional authorization from you to install things, so unless you actually do have multiple people using this system, I don't think there's any loss of security here. A malicious binary or script can already do everything you suggest. – stonecrusher Jul 12 '16 at 21:53
38

I found for my particular setup the following commands worked

brew doctor

And then that showed me where my errors were, and then this slightly different command from the comment above.

sudo chown -R $(whoami) /usr/local/opt
kinnth
  • 583
  • 4
  • 10
33

I found same proble, we can resolve in three steps:-

Step 1

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

Step 2

brew doctor

Step 3

brew prune or brew cleanup

If you still get any linking problem, lets say for mysql, just write

brew link mysql

This will work.

Hari_pb
  • 7,088
  • 3
  • 45
  • 53
13

For those running into this issue (granted 4 years after this post was made) while running Mac OS High Sierra - the steps outlined here solved the problem for me. Essentially just outlines uninstalling and reinstalling brew.

https://medium.com/@mrkdsgn/brew-error-on-macos-high-sierra-check-you-have-permission-to-write-to-usr-local-e8bd1c6a22d4

After running those steps, brew link worked like a charm!

Eron Salling
  • 185
  • 2
  • 5
9

For me the solution was to run brew update.

So, DO THIS FIRST.

This might be normal practice for people familiar with homebrew, but I'm not one of those people.

Edit: I discovered that I needed to update by running brew doctor as suggested by @kinnth's answer to this same question.

A general troubleshooting workflow might look like this: 1. run brew update 2. if that doesn't help run brew doctor and follow its directions 3. if that doesn't help check stack overflow

FragmentalStew
  • 779
  • 6
  • 6
  • 1
    This fixed everything for me and is well worth doing before anything else. – Rob Evans Nov 03 '16 at 15:11
  • All brew doctor does is point out that i have unliked kegs in my Cellar... so that does nothing to fix OP's issue – AlxVallejo Oct 16 '17 at 18:42
  • @AlxVallejo This is what I did to solve a problem I had when I was receiving a nearly identical error as OP. The message you received from Brew Doctor sounds to me like a different issue. The workflow of "1. run brew update 2. if that doesn't help run brew doctor and follow its directions 3. if that doesn't help check stack overflow" probably still applies. – FragmentalStew Nov 15 '17 at 23:14
6

This is because the current user is not permitted to write in that path. So, to change r/w (read/write) permissions you can either use 1. terminal, or 2. Graphical "Get Info" window.

1. Using Terminal

Google how to use chmod/chown (change mode/change owner) commands from terminal

2. Using graphical 'Get Info'

You can right click on the folder/file you want to change permissions of, then open Get Info which will show you a window like below at the bottom of which you can easily change r/w permissions: enter image description here

Remember to change the permission back to "read only" after your temporary work, if possible

Hasaan Ali
  • 1,192
  • 16
  • 22
6

While doing brew link node In addition I got the following issues as well:

Error: Could not symlink include/node/usr/local/include is not writable.

Linking /usr/local/Cellar/node/9.3.0... Error: Permission denied @ dir_s_mkdir - /usr/local/lib

To solve the above just go to /usr/local/ and check the availability of folders 'include' and 'lib', if those folders are not available just create them manually.

And run brew install node again

tijko
  • 7,599
  • 11
  • 44
  • 64
2

I've found the following relevant for Sophos Anti-Virus users:

https://stackoverflow.com/a/32981184

https://community.sophos.com/products/free-antivirus-tools-for-desktops/f/17/t/10029

In short, Sophos is changing permissions of certain /usr/local directories. Sophos has patched this and the fix is included with version 9.4.1.

Community
  • 1
  • 1
joshau
  • 29
  • 3
2

My problem was

Error: Could not symlink share/man/man7/ABORT.7
/usr/local/share/man/man7 is not writable.

chown didn't help, but i followed running brew doctor advices and this warning helped me:

Warning: Broken symlinks were found. Remove them with `brew prune`:
/usr/local/share/man/man3/* 

After brew prune all worked fine!

paveldroo
  • 828
  • 9
  • 14
1

If you go to the folder in finder, right click and select "Get Info" you can go to the "Sharing and Permissions" section for the folder and allow "Read & Write" to "everyone"

This is what I do to make symlinks pass with this error. Also brew seems to reset the permissions on the folder also as if you hadn't altered anything

Carl Lippert
  • 329
  • 3
  • 9
1

For those that are not familiar:

sudo chown -R YOUR_COMPUTER_USER_NAME PATH_OF_FILE
Justin
  • 717
  • 1
  • 9
  • 15
0

If you already have a directory in /usr/local for the package you're installing, you can try deleting this directory.

In my case I had previously installed the package I was trying to install without using brew, and had then uninstalled it. There was a directory /usr/local/<my_package>/ left over from that previous install. I deleted this folder (sudo rm -rf /usr/local/<my_package>/) and after that the brew link step was successful.

Samuel Peter
  • 4,136
  • 2
  • 34
  • 42