Quick Summary: The error
zsh: command not found: brewoccurs on macOS when the Z shell cannot locate the Homebrew executable in the current$PATHenvironment variable. On modern Apple Silicon Macs (M1/M2/M3/M4), Homebrew installs into/opt/homebrew/binrather than the legacy Intel path/usr/local/bin. If the post-installation shell environment script is not exported in~/.zprofileor~/.zshrc, new terminal sessions will fail to recognize thebrewcommand.
Understanding the Root Causes
Starting with macOS Catalina, Apple transitioned the default interactive shell from Bash to Zsh. Simultaneously, the introduction of Apple Silicon architecture altered Homebrew’s root prefix to avoid conflicting with x86_64 emulation binaries. The command failure typically stems from:
- Missing Shell Environment Configuration: Closing the terminal window after installing Homebrew without adding
eval "$(/opt/homebrew/bin/brew shellenv)"to your persistent shell configuration profile. - Apple Silicon vs. Intel Path Divergence: Apple Silicon Macs use
/opt/homebrew/bin(which is not included in macOS default/etc/paths), whereas Intel Macs use/usr/local/bin. - Dotfile Loading Order Mismatch: Adding the environment export to
~/.bash_profileor~/.bashrcinstead of the active Zsh configuration files (~/.zprofileor~/.zshrc). - Rosetta 2 Terminal Emulation: Running terminal applications under Rosetta 2 (x86_64 translation) causing Zsh to look for an Intel Homebrew installation that does not exist.
Step 1: Quick Fix – Add Homebrew to Your Zsh Profile
Export Homebrew’s shell environment into your user profile so every new terminal session automatically loads the binary path:
# 1. Add Homebrew shellenv to ~/.zprofile (evaluated for login shells)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
# 2. Evaluate it immediately in the current active session
eval "$(/opt/homebrew/bin/brew shellenv)"
# 3. Test that the brew binary is now discovered
which brew
Step 2: Universal Path Configuration for Both Apple Silicon and Intel Macs
If you sync your dotfiles across multiple machines (e.g., an Apple Silicon MacBook and an Intel iMac/Mac mini), add dynamic architecture detection to ~/.zshrc:
# Open ~/.zshrc in an editor
nano ~/.zshrc
# Add the dynamic Homebrew loader block:
if [[ $(uname -m) == 'arm64' ]]; then
# Apple Silicon M-series path
if [[ -f /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
else
# Intel x86_64 path
if [[ -f /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
# Reload the configuration in your current session
source ~/.zshrc
Step 3: Fix Directory Permissions and Diagnose Installation Health
If Homebrew was installed but the binary is still inaccessible or reports permission warnings, run Homebrew’s built-in self-diagnostic tool:
# Verify your current user ownership of the Homebrew directory
sudo chown -R $(whoami) /opt/homebrew
# Check for installation health and missing dependencies
brew doctor
# Update Homebrew formulae and core repositories
brew update
Verification & Testing Steps
Ensure that Homebrew is properly recognized across new terminal windows and that package installation functions as expected:
# Check the installed version
brew --version
# Expected output:
# Homebrew 4.x.x
# Homebrew/homebrew-core ...
# Verify the absolute binary path
which brew
# Output should display: /opt/homebrew/bin/brew
# Test installing a lightweight utility
brew install wget && which wget
Troubleshooting Summary Comparison
| Mac Architecture | Default Homebrew Path | Configuration Target | Recommended Shellenv Command |
|---|---|---|---|
| Apple Silicon (M1/M2/M3/M4) | /opt/homebrew/bin |
~/.zprofile or ~/.zshrc |
eval "$(/opt/homebrew/bin/brew shellenv)" |
| Intel x86_64 | /usr/local/bin |
~/.zprofile or ~/.zshrc |
eval "$(/usr/local/bin/brew shellenv)" |
| Rosetta 2 Terminal | Dual prefix needed | ~/.zshrc (conditional) |
Branch on $(uname -m) |
| Custom Bash Shell | Matches CPU architecture | ~/.bash_profile |
Export PATH="/opt/homebrew/bin:$PATH" |
Leave a Reply