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

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

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).


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)