DEV Community

cerico
cerico

Posted on • Edited on

21

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️