Skip to content
ohsudo, Home
macOS

macOS "zsh: command not found: brew": add Homebrew to your PATH

Fix "zsh: command not found: brew" on macOS: Homebrew lives in /opt/homebrew on Apple silicon. Add brew shellenv to .zprofile, or install it if it isn't there.

Beginner4 min readApplies to macOS 12 Monterey+ · Apple silicon · Intel · Homebrew 4+

by OhSudo ··

% brew install wget
zsh: command not found: brew

Usually Homebrew is installed, and your shell just doesn’t know where to find it. That’s especially common on Apple silicon Macs, as the installer puts it in a folder that isn’t on your PATH, and prints the commands to fix that at the very end. Those are easy to miss.

  1. zshlooks for brew
  2. PATHno /opt/homebrew/bin(breaks here)
  3. /opt/homebrew/bin/brewthere, never found
Homebrew is usually installed. On Apple silicon it lives in /opt/homebrew/bin, which isn't on your PATH until you add the line the installer printed at the end.

TL;DR

Apple silicon (M1 and later):

Terminal window
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
brew --version

Intel Mac: replace /opt/homebrew with /usr/local.

1. Is it installed?

Look in both places:

Terminal window
ls -l /opt/homebrew/bin/brew # Apple silicon
ls -l /usr/local/bin/brew # Intel
uname -m # arm64 = Apple silicon, x86_64 = Intel

Run it by its full path to be sure:

Terminal window
/opt/homebrew/bin/brew --version
  • It answers: the problem is your PATH (section 2).
  • No such file: Homebrew isn’t there (section 3).

2. Add it to your PATH

brew shellenv prints the settings your shell needs, including PATH. The right place for it is ~/.zprofile, which zsh reads when a terminal opens:

Terminal window
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

Apply it to this terminal, without opening a new one:

Terminal window
eval "$(/opt/homebrew/bin/brew shellenv)"

Then check:

Terminal window
which brew
echo "$PATH"
brew doctor

If you already had that line, look for what’s undoing it. A line like export PATH=/something with no $PATH in it, in ~/.zshrc or ~/.zprofile, throws away everything before it:

Terminal window
grep -n 'PATH' ~/.zprofile ~/.zshrc ~/.zshenv 2>/dev/null

It should look like export PATH="$HOME/bin:$PATH".

3. Install it

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

Read what it prints when it finishes: the Next steps section gives you the same commands as in section 2.

The installer needs the Command Line Tools. If that step fails with xcrun: error: invalid active developer path, see the xcrun article.

4. It stopped working after moving to a new Mac

Migration Assistant copied your settings from an Intel Mac to an Apple silicon one. Your profile now points at /usr/local, and there’s an old Intel Homebrew there, or nothing. Install Homebrew again, for this machine, so that it goes in /opt/homebrew, then:

Terminal window
brew bundle dump --file=~/Brewfile --force # on the old one, if it still works
brew bundle install --file=~/Brewfile # on the new one

Then remove the old /usr/local line from your profile. Keeping both makes things confusing, since which brew runs depends on the order of PATH.

5. Other shells, and tools that run without a login

You’re on bash or fish. Your shell reads different files. Check with echo $SHELL:

Terminal window
# bash
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
# fish
echo 'eval (/opt/homebrew/bin/brew shellenv)' >> ~/.config/fish/config.fish

In a script, cron or launchd job, or an IDE’s task: these don’t read your profile, so the command isn’t found there, even though it works in your terminal. Use the full path, or set it at the top of the script:

Terminal window
eval "$(/opt/homebrew/bin/brew shellenv)"

The same reasoning applies to cron jobs on any system.

Under Rosetta. A terminal opened with Open using Rosetta reports x86_64, and would use an Intel installation. Turn that off in the app’s Get Info window, and don’t mix the two.

6. Other commands from Homebrew aren’t found

brew works, but node or python3 doesn’t? Some formulae are “keg-only”: they aren’t linked, so as not to conflict with what macOS ships. brew info tells you what to do:

Terminal window
brew info node@22
brew link --overwrite node@22

For a keg-only one, use the export PATH=... line its message suggests.

Warning

Don’t run the installer or brew with sudo. Homebrew refuses on purpose, as files owned by root break later installs. And take care with curl | bash in general: this URL is what Homebrew’s own site gives, so check it against brew.sh rather than a copy from somewhere else.

Shell, we have a problem!

Similar, but different: Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)! means an Intel setup on an Apple silicon Mac: install it fresh in /opt/homebrew (how to move). Permission denied @ apply2files means wrong owners in Homebrew’s folders. command not found: python means you need python3: see that article. An app that won’t open is another matter: see is damaged and can’t be opened. Once brew runs, Permission denied @ apply2files means files in its folder belong to root.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Articles

Related articles