Homebrew is one of the most convenient package managers for setting up a development environment on macOS. But right after installing it, you may see the error zsh: command not found: brew when you try to run a brew command. This error means that Homebrew was not installed correctly, or that the path to it has not been added to your shell's environment variables.
This article explains why the error happens and walks through the fixes step by step.
What is Homebrew?
Homebrew is a package management system that lets you install and manage software easily on macOS and Linux. Because you can install software straight from the command line, it is a favorite among developers. With Homebrew you can quickly install development tools such as Git and Node.js. You can find the official site at brew.sh.
What causes the error
The command not found: brew error is usually caused by one of the following.
Homebrew is not installed
The most basic cause is that Homebrew simply has not been installed yet. If the installation never completed, the brew command is not available.
The path is not set
Even when Homebrew is installed, you will get a "command not found" error if the Homebrew install location is not included in your shell's $PATH environment variable. On Apple silicon Macs (M1 and later) in particular, Homebrew is installed under /opt/homebrew/bin.
The shell config file has not been updated
After installing Homebrew, you need to add the required path to your shell configuration file (usually ~/.zshrc). If this step is skipped, the brew command is not recognized.
How to fix
Let's go through the concrete fixes.
1. Check whether Homebrew is installed
First, confirm that Homebrew is installed correctly. Run the following command in your terminal.
brew --versionIf this command still returns an error, Homebrew is either not installed or the installation failed. To install Homebrew, run the following command.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Once the installation finishes, run brew --version again to confirm it was installed correctly.
2. Set the path
If the error occurs even though Homebrew is installed, the path is probably not set. Set it with the steps below.
- Open your terminal.
- Edit your
~/.zshrcfile. Run the following command to open it.
nano ~/.zshrc- Add the following line to the end of the file.
export PATH="/opt/homebrew/bin:$PATH"- Save the change and exit the editor (
Ctrl + X, thenY, thenEnter). - Reload the configuration by running the following command.
source ~/.zshrcThe brew command should now be recognized.
3. Use brew shellenv
After installing Homebrew, you can also set the environment variables with the brew shellenv command. Do it as follows.
- Open your terminal.
- Add the following line to your
~/.zshrcfile.
eval $(/opt/homebrew/bin/brew shellenv)- Save the change and exit the editor.
- Reload the configuration by running the following command.
source ~/.zshrcThis approach makes the brew command recognized as well.
Frequently asked questions
Q1: What is the difference between M1 and Intel Macs?
On M1 (Apple silicon) Macs, Homebrew is installed under /opt/homebrew, whereas on Intel Macs it is installed under /usr/local. So when you set the path, pay attention to the install directory that matches your machine.
Q2: How do I uninstall Homebrew?
To uninstall Homebrew, run the following command.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"Running this command removes Homebrew completely.
Q3: What if the error still is not fixed?
If the error still persists, try restarting your terminal. Trying a different shell (such as bash) is also worth a shot.
Summary
The zsh: command not found: brew error is caused either by Homebrew not being installed correctly or by the path not being set in your shell's environment variables. Following the steps in this article should resolve the problem. Set up Homebrew properly and enjoy a smoother development environment on macOS.