Homebrew relies on shell startup files to put its brew command and installed package binaries in the shell search path. When a fresh terminal cannot find brew, or it finds the wrong Homebrew prefix, the shell needs to load Homebrew's shellenv output during startup.
The brew shellenv command prints shell-specific export statements for $PATH, $MANPATH, $INFOPATH, and Homebrew's own prefix variables. Evaluating that output from the correct startup file lets each new shell use the active Homebrew installation without copying every generated directory by hand.
Apple Silicon macOS normally uses /opt/homebrew and zsh, while Intel macOS uses /usr/local and Linux or WSL uses /home/linuxbrew/.linuxbrew. Replace the prefix and shell name before editing the startup file when your Homebrew install uses a different combination.
Related: How to find a Homebrew package prefix
Related: How to fix Homebrew permission errors
Related: How to run brew doctor
Steps to configure Homebrew shell environment:
- Run the installed Homebrew binary directly to confirm the prefix.
$ /opt/homebrew/bin/brew --prefix /opt/homebrew
Use /usr/local/bin/brew on Intel macOS or /home/linuxbrew/.linuxbrew/bin/brew on Linux and WSL when that is the installed Homebrew binary.
Related: How to find a Homebrew package prefix - Print the shellenv output for zsh.
$ /opt/homebrew/bin/brew shellenv zsh export HOMEBREW_PREFIX="/opt/homebrew"; export HOMEBREW_CELLAR="/opt/homebrew/Cellar"; export HOMEBREW_REPOSITORY="/opt/homebrew"; fpath[1,0]="/opt/homebrew/share/zsh/site-functions"; export FPATH; eval "$(/usr/bin/env PATH_HELPER_ROOT="/opt/homebrew" /usr/libexec/path_helper -s)" [ -z "${MANPATH-}" ] || export MANPATH=":${MANPATH#:}"; export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";Pass bash instead of zsh when Bash owns the startup file. For fish or pwsh, use the syntax printed by brew shellenv fish or brew shellenv pwsh instead of the zsh eval line.
- Open the zsh login profile.
$ nano ~/.zprofile
Use ~/.zshrc for zsh on Linux, ~/.bash_profile for Bash on macOS, or ~/.bashrc for Bash on Linux.
- Add the Homebrew shellenv line near the top of the profile.
- ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv zsh)"
Do not paste zsh syntax into a fish or pwsh startup file. Print brew shellenv fish or brew shellenv pwsh for that shell and use the syntax it returns.
- Load the profile in the current shell.
$ source ~/.zprofile
Opening a new terminal window also loads the login profile on macOS.
- Verify that brew resolves from the expected prefix.
$ command -v brew /opt/homebrew/bin/brew
- Confirm Homebrew reports the same active prefix.
$ brew --prefix /opt/homebrew
If the output still points to another prefix, remove duplicate shellenv lines from older startup files and open a new login shell.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.