Skip to main content
  1. Blog/

Homebrew Installation on Multi User System

·802 words·4 mins

A Little Bit of Background #

Homebrew is one of the most popular package managers for MacOS. It allows you to install CLI utilities, as well as full-fledged desktop applications, dramatically increasing the number of apps you can install without downloading them from developers’ websites. And it’s quite useful on Linux too. As Homebrew taps contain a lot of developer tools that are always cutting-edge, you may want to use it to install your dev tools (go, node, …), as it may be more up-to-date than packages provided by your OS, especially if you run a Debian-based distribution.

But Homebrew has one major (at least for me) oversight. It’s not designed to run in a multi-user environment. For quite some time, I always create two separate users on all my machines - one is for personal use and one for my day job. It helps me be less distracted by my work when I’m doing something in my free time, and vice versa.

And as I would like to use Homebrew both for my personal projects and for work, I’ve been figuring out a way to use it on my setup. After searching the web and combining different approaches, I found a solution that works for me.

The Guide #

As Homebrew is not designed to be used in a multi-user environment, the main trick is to always use Homebrew from a single user perspective. Unix magic (and sudo specifically) allows us to run a CLI command from another user account without logging into it.

To work with Homebrew, you can either use one of the real users on your system or a purposely created user. I prefer the second option, as it seems more clear and secure to me.

MacOS #

Creating a user #

Creating users from the terminal on MacOS can be a little tricky. Therefore, I recommend creating a user via the UI.

Go to Settings → Users & Groups → Add Account. You’ll need to unlock the settings with your admin password. Create a new Administrator account (for example, Homebrew), set a password for it. Remember the password!

Installing Homebrew #

Log in with the newly created user and install Homebrew

/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"

You may be asked to enter your sudo password. It’s the password that you specified on Homebrew account creation.

Adding Homebrew to the path #

After the installation is complete, log in back as your usual account. Now as you have Homebrew installed on your system, the next goal is to make it accessible from your user. For that, we want to add Homebrew binaries into your path.

The path to the main Homebrew binary on MacOS depends on your platform. If your Mac runs on Apple Silicon (M1/M2 CPU series), then Homebrew is installed in /opt/homebrew. If you use Intel, then it’s located in /usr/local. Knowing that, we add Homebrew to the path

For Apple Silicon

echo '# Set PATH, MANPATH, etc., for Homebrew.' >> $HOME/.zshrc
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> $HOME/.zshrc
eval "$(/opt/homebrew/bin/brew shellenv)"

And for Intel

echo '# Set PATH, MANPATH, etc., for Homebrew.' >> $HOME/.zshrc
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> $HOME/.zshrc
eval "$(/usr/local/bin/brew shellenv)"

Usually, bin/brew binary should be available for execution from all users. But if after running these commands, you see any errors like Permission denied, you may want to ensure that the file is executable.

For Apple Silicon

sudo chmod +x /opt/homebrew/bin/brew

And for Intel

sudo chmod +x /usr/local/bin/brew

Adding an alias for handy usage #

Now we just need to run a handy alias for running brew

echo 'alias brew='sudo -H -i -u homebrew brew' >> $HOME/.zshrc

Now it’s all done!

You may want to add an alias and add brew to the path from your another user as well.

Linux #

Installation on Linux is a little bit more straightforward, as you can create a user from the terminal. Except for that, the process is the same as for MacOS.

Creating a user #

Create a new user to operate Homebrew:

sudo useradd --create-home linuxbrew

Installing Homebrew #

Install Homebrew for this user:

sudo -H -i -u linuxbrew /bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"

Make it executable without sudo (will be useful for shell integration):

sudo chmod -R a+x /home/linuxbrew

Adding Homebrew to the path #

Add packages that were installed by Homebrew to your PATH:

echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /home/$USER/.profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/$USER/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Adding an alias for handy usage #

Lastly, add an alias (you may want to change the alias location):

echo 'alias brew="sudo -H -i -u linuxbrew brew"' >> /home/$USER/.bash_aliases

Now you are good to go! Use brew as usual. You may want to add an alias and add brew to the path from your another user as well.



Evgeny Kuvalkin
Author
Evgeny Kuvalkin