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.
by OhSudo ··
% brew install wgetzsh: command not found: brewUsually 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.
- zshlooks for brewsearches
- PATHno /opt/homebrew/bin(breaks here)not in it(fails)
- /opt/homebrew/bin/brewthere, never found
TL;DR
Apple silicon (M1 and later):
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofileeval "$(/opt/homebrew/bin/brew shellenv)"brew --versionIntel Mac: replace /opt/homebrew with /usr/local.
1. Is it installed?
Look in both places:
ls -l /opt/homebrew/bin/brew # Apple siliconls -l /usr/local/bin/brew # Inteluname -m # arm64 = Apple silicon, x86_64 = IntelRun it by its full path to be sure:
/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:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofileApply it to this terminal, without opening a new one:
eval "$(/opt/homebrew/bin/brew shellenv)"Then check:
which brewecho "$PATH"brew doctorIf 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:
grep -n 'PATH' ~/.zprofile ~/.zshrc ~/.zshenv 2>/dev/nullIt should look like export PATH="$HOME/bin:$PATH".
3. Install it
/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:
brew bundle dump --file=~/Brewfile --force # on the old one, if it still worksbrew bundle install --file=~/Brewfile # on the new oneThen 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:
# bashecho 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
# fishecho 'eval (/opt/homebrew/bin/brew shellenv)' >> ~/.config/fish/config.fishIn 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:
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:
brew info node@22brew link --overwrite node@22For 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.