Mac – “brew” command not found after installing Homebrew on an Arm/M1 Mac

homebrewmaczsh

I have installed Homebrew using:

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

Why is the brew command not found?

(base) rags@Rageshs-Mac-mini ~ % brew help
zsh: command not found: brew

My $PATH variable according to the shell:

(base) rags@Rageshs-Mac-mini ~ % echo $path
/Users/rags/opt/anaconda3/bin /Users/rags/opt/anaconda3/condabin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /Library/Apple/usr/bin

Best Answer

Homebrew isn't included in your $PATH variable, which is the cause of the shell not finding the brew command.

To fix this, you must edit your shell startup script, .zshrc. The Homebrew installer doesn't make this edit, but rather instructs the user to do it, which you probably missed.

Add the following line to .zshrc:

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

You can either add it with a text editor or by executing a shell command:

$ echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> $HOME/.zshrc

Now, reload the shell by opening a new shell window and you should be good to go.


Explanation

The lines in .zshrc are executed each time you open a new shell.

When the shell reaches the line

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

it will at first execute

/opt/homebrew/bin/brew shellenv

, that is, executing the brew binary, given with its full path, with shellenv provided as an argument. From man brew:

    shellenv
        Print export statements. When run in  a  shell,  this  installation  of
        Homebrew will be added to your PATH, MANPATH, and INFOPATH.

        The  variables HOMEBREW_PREFIX, HOMEBREW_CELLAR and HOMEBREW_REPOSITORY
        are also exported to  avoid  querying  them  multiple  times.  Consider
        adding  evaluation  of  this  command's  output  to your dotfiles (e.g.
        ~/.profile, ~/.bash_profile, or ~/.zprofile) with:  eval  $(brew  shel-
        lenv)

Indeed, the output of brew shellenv is:

$ brew shellenv
export HOMEBREW_PREFIX="/opt/homebrew";
export HOMEBREW_CELLAR="/opt/homebrew/Cellar";
export HOMEBREW_REPOSITORY="/opt/homebrew";
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}";
export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";

So in effect, the shell startup script executes eval $(...), with ... replaced by the lines above.

Related Question