How to use Homebrew on a Multi-user MacOS Sierra Setup

MacosPermissionsHomebrewMulti User

Macos Problem Overview


I have a Mac that is shared between two engineers. Both have separate user accounts. Both need to run brew update and brew install... occasionally.

How do I set this up without getting errors like: /usr/local must be writable!?

Yeah, I could have UserA take over the permissions of /usr/local every time he wants to use brew (and same with UserB), but that seems like a lot of unnecessary trouble.

Macos Solutions


Solution 1 - Macos

You can also change the group permissions to admin or another group that both of your users are in:

chgrp -R admin /usr/local
chmod -R g+w /usr/local

Original source: https://gist.github.com/jaibeee/9a4ea6aa9d428bc77925

UPDATE:

In macOS High Sierra you can't change the owner, group or permissions of /usr/local. So you have to change the group and permissions of the subfolders:

chgrp -R admin /usr/local/*
chmod -R g+w /usr/local/*

UPDATE September 2018, High Sierra 10.13.6

  1. Determine the path of the brew prefix, ie. the path that will be used to store files related to working with homebrew
  2. Check that all users on the system who need access to brew are in the admin group
  3. Optional Add a user to the admin group if a user needs access to brew > Will require access / privileges to use the sudo command
  4. Set the brew prefix path to be recursively owned by the admin group
  5. Set the brew prefix path to be recursively writable by all users who are in the admin group
  6. Verify the permissions of the brew prefix
  7. brew 

echo $(brew --prefix)
echo $(groups $(whoami))
sudo dseditgroup -o edit -a $(whoami) -t user admin
sudo chgrp -R admin $(brew --prefix) 
sudo chmod -R g+rwX $(brew --prefix)
ls -lah $(brew --prefix)

Solution 2 - Macos

Every answer that tries to hack permissions, or use sudo is wrong.

Do not use sudo and do not share a single brew installation across user accounts.

The correct answer per the Homebrew docs is to use zero or one global brew installation on a machine, and for all other users install a local version of brew.

This is especially important on Mac, but works on Linux too.

This can be done by one of the following approaches

  1. Git approach: doing a git checkout of the source repo
  2. Untar-anywhere approach: expanding a tarball into some directory – owned by your user

Git approach

For the git approach you'll need to clone brew.

Arbitrarily choosing my user home directory for my checkout:

cd $HOME
git clone https://github.com/Homebrew/brew.git
./brew/bin/brew tap homebrew/core

Untar-Anywhere Approach

As documented at docs.brew.sh, run this command in your home directory, which will create ~/brew.

cd $HOME
mkdir brew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C brew

Finishing up

For either installation method, you'll need to change your PATH to prefer the new brew bin directory, adding something like this to your shell's dot file.

export PATH=$HOME/brew/bin:$PATH >> ~/.zshrc # or ~/.bashrc

Then running this to reload and test

exec $SHELL
which brew # see that brew is found in your path

Since this is a new installation, you have to install all your desired brew packages (again).

Solution 3 - Macos

Install homebrew for each user

According to the brew documentation you can install it inside each User Home folder

That way all packages are going to stay inside your user folder, and will not be visible or affect other users. As a good side effect if you delete that user, no trash is left behind on your system. So system wide pollution is minimised.

This comes at the cost of more storage being used, if you install the same package for multiple users. Just something to be aware if you have a very small SSD.

Instructions

  1. If you currently have brew installed on your system globally, I recommend uninstalling brew first. (You can see where brew is installed running which brew)

  2. If you don't have Command Line Tools installed, you have to run this first:

    xcode-select --install
    
  3. Open terminal and Run:

    • MacOS Catalina 10.15 or newer:
      cd $HOME
      mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
      echo 'export PATH="$HOME/homebrew/bin:$PATH"' >> .zprofile
      
    • MacOS Mojave 10.14 or older:
      cd $HOME
      mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
      echo 'export PATH="$HOME/homebrew/bin:$PATH"' >> .bash_profile
      
  4. Close the Terminal window

  5. Open Terminal again, and run this to ensure your installation is correct:

    brew doctor
    
  6. Done!


Disabling auto update

This is not required I also find useful to disable brew to update all packages before every time you install something.

  • MacOS Catalina 10.15 or newer
    echo 'HOMEBREW_NO_AUTO_UPDATE=1' >> $HOME/.zprofile
  • MacOS Mojave 10.14 or older
    echo 'HOMEBREW_NO_AUTO_UPDATE=1' >> $HOME/.bash_profile

Solution 4 - Macos

EDIT: Please use the answer by Vitim, it's the correct one :)

Hacky workaround solution for macOS Mojave 10.14

This is a edited version of user4815162342's answer, which didn't work for me out-of-the-box.

  1. In System Preferences, go to Users & Groups, click the lock symbol in the bottom left corner to unlock user/group creation, then create a new group called brew-usergroup. Add all users who work with brew to the group (like in the attached screenshot from a german macOS).

enter image description here

  1. In terminal, do this:

     echo $(brew --prefix)
     echo $(groups $(whoami))
     sudo dseditgroup -o edit -a $(whoami) -t user brew-usergroup
     sudo chgrp -R brew-usergroup $(brew --prefix)/*
     sudo chmod -R g+rwX $(brew --prefix)/*
     ls -lah $(brew --prefix)
    

Note that this doesn't change rights of brew folders anymore (like in other answers), it changes subfolders/files of brew folders. brew install should now work fine without errors.

Solution 5 - Macos

The above works fine, but if you want new files to automatically inherit those permissions, set an ACL which gets inherited (otherwise only the user that pours a bottle can remove it). Found hints how to do this here: https://gist.github.com/nelstrom/4988643

As root run once (assuming all users of group "admin" should have access):

cd /usr/local
/bin/chmod -R +a "group:admin allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" Homebrew Caskroom Cellar bin
/usr/bin/chgrp -R admin Homebrew Caskroom Cellar bin
/bin/chmod -R g+rwX Homebrew Caskroom Cellar bin
ls -lae .

the -e on ls shows ACLs.

Update: now I use specific directories (see above) as it failed (sth. like out of memory)

Solution 6 - Macos

Here is the official answer of the Homebrew maintainer.

In addition to it I suggest to do 3 more steps. Suppose you have an admin user niki who owns the /usr/local/* dir and you are logged in as another admin user niki_at_work.

  1. Create ~/brew.sh with these contents:
#!/bin/bash
comm="brew $@"
su niki -c "$comm"
  1. chmod +x ~/brew.sh
  2. Add this alias to .zshrc or equivalent: alias brew="~/brew.sh"

Now you can brew from niki_at_work like always (it will ask for niki's password):

brew update
brew install swiftlint

If you want to use a dedicated admin user for brew ex. brewadmin you should first chown brew dirs: sudo chown -R brewadmin:admin /usr/local/*

Solution 7 - Macos

Homebrew is not designed to be used by different Unix users. From the FAQ:

> If you need to run Homebrew in a multi-user environment, consider creating a separate user account especially for use of Homebrew.

The chmod solution is not viable unless you ensure that every newly created file in the Homebrew prefix also has the group write permission, which is not the case with the default umask – or unless you keep running that chmod command every time a program writes to the Homebrew prefix.

Maintaining separate Homebrew installations for each user do sort the permissions issues but will create a number of other issues, which is why it's not recommended by Homebrew:

> However do yourself a favour and use the installer to install to the default prefix. Some things may not build when installed elsewhere. One of the reasons Homebrew just works relative to the competition is because we recommend installing here. Pick another prefix at your peril!


To ease the official recommendation of using a dedicated account for Homebrew, you can use sudo to easily impersonate that user account. Assuming you named that user homebrew:

sudo -H -u homebrew brew update
  • -H makes sure HOME is set to the homebrew user home (e.g. /Users/homebrew) so that Homebrew can do its housekeeping there.
  • -u homebrew tells sudo to impersonate the homebrew user account instead of the default of root.

Solution 8 - Macos

(Deleted as of 2020, this answer is outdated)

Solution 9 - Macos

The best solution is to add a sudoers record to allow unprivileged user 'joe' to execute any 'brew' related command as the administrative user.

Create a file at /etc/sudoers.d/joe with following content:

joe ALL=(administrator) NOPASSWD: /usr/local/bin/brew

Then you can run brew like this:

sudo -Hu administrator brew install <smth>

Solution 10 - Macos

The above solutions didn't work for me. But running the command below worked for me.

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

Source: https://github.com/Homebrew/brew/issues/3228#issuecomment-333858695

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon GunterView Question on Stackoverflow
Solution 1 - Macosuser4815162342View Answer on Stackoverflow
Solution 2 - MacosjerrybView Answer on Stackoverflow
Solution 3 - MacosVitim.usView Answer on Stackoverflow
Solution 4 - MacosSliqView Answer on Stackoverflow
Solution 5 - MacosDrPsychickView Answer on Stackoverflow
Solution 6 - MacospokidyshevView Answer on Stackoverflow
Solution 7 - MacosValView Answer on Stackoverflow
Solution 8 - MacosHarald NordgrenView Answer on Stackoverflow
Solution 9 - MacosSergey PapyanView Answer on Stackoverflow
Solution 10 - MacosernestkamaraView Answer on Stackoverflow