Keywords: M1 Mac | Homebrew Installation | zsh PATH Configuration
Abstract: This article addresses the common issue of 'zsh: command not found: brew' after installing Homebrew on M1 Macs. It provides an in-depth analysis of the error causes, including outdated installation scripts, changes in Homebrew's installation path for ARM architecture, and PATH environment variable configuration in zsh shell. Step-by-step instructions guide users to correctly install Homebrew and configure the .zshrc file, ensuring brew commands are accessible in zsh. The article also covers handling bash deprecation warnings and includes verification commands to confirm successful installation.
Problem Background and Error Analysis
When installing Homebrew on M1 Macs, users often encounter a typical error: the installation appears successful, but running commands like brew doctor or brew help in zsh shell returns zsh: command not found: brew. This error primarily stems from two core issues: using outdated installation scripts and incorrect PATH environment variable configuration.
First, the initial installation command ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" is deprecated. Homebrew officially recommends the updated script: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)". This new script is optimized for compatibility with both bash and zsh, ensuring cross-shell functionality.
Second, on ARM-based M1 Macs, Homebrew's default installation path has changed. Traditional Intel Macs use /usr/local/bin, while M1 Macs use /opt/homebrew/bin. This adjustment avoids the need for sudo permissions in /usr/local, enhancing security. If the PATH variable does not include this new path, zsh cannot locate the brew executable, leading to the command not found error.
Solution: Installation and Path Configuration
To resolve this issue, follow these steps to ensure proper Homebrew installation and PATH setup.
Step 1: Reinstall Homebrew using the recommended command. In the terminal, run: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)". This script automatically detects the system architecture and installs Homebrew to /opt/homebrew on M1 Macs. During installation, you might see a bash deprecation warning, which is normal on macOS; it can be ignored or silenced permanently by adding export BASH_SILENCE_DEPRECATION_WARNING=1 to the ~/.bashrc file.
Step 2: Configure the PATH environment variable in zsh. Open or create the ~/.zshrc file, which is zsh's configuration file for setting shell environments. Use a text editor or terminal commands to add the path. For M1 Macs, it is recommended to use zsh-specific syntax: path+=/opt/homebrew/bin. This appends /opt/homebrew/bin to the PATH variable, ensuring brew commands are accessible. If preferred, a generic method like export PATH="/opt/homebrew/bin:$PATH" can also be used. After saving the file, run source ~/.zshrc or restart the terminal to apply changes.
Step 3: Verify the installation and configuration. Execute brew doctor; if it outputs Your system is ready to brew., this confirms that Homebrew is installed correctly and PATH is properly configured. Additionally, run which zsh to ensure the default shell is still zsh; if needed, change it back with chsh -s /bin/zsh.
Additional Notes and Best Practices
During installation, users might encounter other related issues. For example, if an uninstall was attempted previously, use the updated uninstall script: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)" to avoid path residue problems.
For PATH configuration, understanding zsh's path management mechanism is crucial. zsh supports dynamic path arrays, and using path+= syntax aligns with its design philosophy, preventing overwrites of existing PATH settings. Also, ensure the .zshrc file exists; if missing, create it with touch ~/.zshrc.
This article is based on the best answer (Answer 2), with references to other answers for supplementation. Key takeaways include: using updated installation scripts, path differences on ARM architecture, and methods for zsh PATH configuration. By following these steps, users can efficiently resolve the brew command not found error and enhance their development experience on M1 Macs.