DEV Community

cerico
cerico

Posted on • Edited on

Using brew in a multi-user system

Using brew in a multi-user system

On a mac brew can get into a bit of a muddle on a multi-user system if you are not careful. The problem is that brew installs everything in /usr/local and if you have multiple users then the permissions can get a bit messed up. The answer to this is to install brew as normal for the first user, but any subsequent users shouldn't install their own version, but run the first users installation instead.

To do this, set up an alias in your ~/.zshrc to run brew as that user.

# ~.zshrc
unalias brew 2>/dev/null
brewser=$(stat -f "%Su" $(which brew))
alias brew='sudo -Hu '$brewser' brew'
Enter fullscreen mode Exit fullscreen mode

Lets break this down. The first line removes any existing alias for brew. This is because we need to 'real' brew in the second line to find the installation location (which brew).

The second line gets the user that brew is installed under. The third line creates an alias for brew that runs brew as the user that brew is installed under. The 2>/dev/null just stops an error message if there is no existing alias (which we would get on the first sourcing of the file as in that instance brew would be the 'real' brew).

Top comments (2)

Collapse
 
stroiman profile image
Peter Strøiman

Nice,

I was searching for this topic, and found a lot of solutions that are overly complicated, and some that have dependencies on internal dependencies in Homebrew, meaning they could break with new homebrew installations.

The Homebrew readme does say:

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

Your solution is compatible with that advice; it just uses the main account as the source for homebrew, rather than a dedicated account (which is nice, as I don't need a password prompt most of the time), and you make the process almost completely transparent (only the occasional password prompt on the secondary account).

Collapse
 
maksym_krupenko profile image
Maksym Krupenko

Hey
Signed in only to say a big thanks for this post - you saved a lot of time for me and made the creation of a new user for work/personal projects much easier.
I appreciate it.