Fix “brew command not found” on Mac with zsh
If you’re a Homebrew user who is now experiencing a “zsh: command not found: brew” error message in zsh shell, you’re probably perplexed as to why the brew command line line tool is no longer working.
The reason you may be experiencing the command not found error for brew is because the zsh shell is not able to find the brew binary to execute for whatever reason, either because something is awry with your $PATH, or the Homebrew installation, or both. Thus, to fix the brew command not found error, we will perform a series of actions to reinstall Homebrew and then set the path manually if things continue to fail.
Fix “zsh: brew: command not found” on Mac with zsh Shell
First, we’re going to reinstall Homebrew. Do this even if you have already installed Homebrew into Ventura or your current MacOS version, and whether your Mac is an M1/M2 or otherwise.
At the command line, enter the following exactly:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Hit enter and proceed with the Homebrew installation as usual.
When installation has complete, attempt to use the ‘brew’ command again, for example to install a brew package, update, or remove a package.
If you continue to experience the brew command not found error message, you can resolve it by placing brew in your zshrc $PATH. You can check your PATH or add a new PATH with the export command with the
echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.zshrc
Next, type the following to repair brew if needed:
The echo approach may be familiar to you if you have previously set environment variables.
Experiencing ‘command not found’ After Installing Oh-My-Zsh? Try This
Specific to many users who only experience the brew command not found problem after installing the popular Oh-my-zsh package, you may try manually adding the following line to your .zshrc file:
eval $(/opt/homebrew/bin/brew shellenv)
You can also add this to your zshrc with a single command:
echo "eval $(/opt/homebrew/bin/brew shellenv)" >> ~/.zshrc
Again, try running brew doctor afterwards.
If you run the following brew command, you’ll also be given a solution of using the same string in the .zprofile:
brew help shellenv
brew help shellenv
Usage: brew shellenv
Print export statements. When run in a shell, this installation of Homebrew will
be added to your PATH, MANPATH, and INFOPATH.
The variables HOMEBREW_PREFIX, HOMEBREW_CELLAR and HOMEBREW_REPOSITORY are
also exported to avoid querying them multiple times. To help guarantee
idempotence, this command produces no output when Homebrew’s bin and sbin
directories are first and second respectively in your PATH. Consider adding
evaluation of this command’s output to your dotfiles (e.g. ~/.profile,
~/.bash_profile, or ~/.zprofile) with: eval “$(brew shellenv)”
With the Homebrew path set (or the shellenv approach to set necessary environmental variables), the brew command will work as expected again. If you’re curious where Homebrew packages are installed and kept in the file system
Did this trick work to resolve the “brew command not found” error message for you? Did you find another solution? Let us know your experiences in the comments.