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.

Steps to configure Homebrew shell environment:

  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. Load the profile in the current shell.
    $ source ~/.zprofile

    Opening a new terminal window also loads the login profile on macOS.

  6. Verify that brew resolves from the expected prefix.
    $ command -v brew
    /opt/homebrew/bin/brew
  7. 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.