Homebrew is a powerful package manager for macOS, but encountering the “zsh: command not found: brew” error can be frustrating. This error typically occurs when the Homebrew binary is not correctly installed or not added to your system’s PATH variable. In this guide, we’ll walk through the steps to resolve this issue and ensure Homebrew is properly configured.


Step-by-Step Solution

Step 1: Verify Homebrew Installation

First, check if Homebrew is installed on your system. Open your terminal and run:

1
brew --version

If Homebrew is installed correctly, this command should return the version number. If you see an error message, proceed to the next step.

Step 2: Reinstall Homebrew

If Homebrew is not installed or the installation is corrupted, you can reinstall it by running the following command:

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

This script will install Homebrew on your system.

Step 3: Add Homebrew to Your PATH

After installation, ensure that Homebrew is added to your PATH variable. This step is crucial because it allows your shell to locate the brew command.

  1. Open your ~/.zshrc file in a text editor:

    1
    nano ~/.zshrc
  2. Add the following line to the file:

    1
    export PATH="/opt/homebrew/bin:$PATH"

    Note: If you are using an Intel-based Mac, the path might be /usr/local/bin instead of /opt/homebrew/bin.

  3. Save the file and apply the changes by running:

    1
    source ~/.zshrc
Step 4: Verify the Installation

Run the following command to verify that Homebrew is accessible:

1
brew --version

If everything is set up correctly, this should return the installed version of Homebrew.

Step 5: Run brew doctor

To ensure there are no lingering issues, run:

1
brew doctor

This command will check for common problems and provide suggestions for resolving them.


Additional Tips

  1. Check for Multiple Installations: If you have multiple versions of Homebrew installed, it might cause conflicts. Ensure you have only one installation by running:

    1
    brew --prefix

    This command will show the installation path of Homebrew.

  2. Update Homebrew: If Homebrew is installed but outdated, run:

    1
    brew update

    This will update Homebrew to the latest version.

  3. Add brew shellenv to Your Shell Configuration: For a more robust solution, add the following line to your ~/.zshrc file:

    1
    eval $(/opt/homebrew/bin/brew shellenv)

    This ensures that Homebrew’s environment variables are correctly set.


Conclusion

The “zsh: command not found: brew” error can be resolved by reinstalling Homebrew and ensuring it is correctly added to your PATH variable. By following these steps, you should be able to use Homebrew without any issues. If the problem persists, consider running brew doctor for additional troubleshooting tips.

Happy coding!