DEV Community

cerico
cerico

Posted on • Updated 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 (0)