"brew" command not found after installing Homebrew on an Arm/M1 Mac

7,521

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.

Share:
7,521

Related videos on Youtube

Ragesh Kr
Author by

Ragesh Kr

Updated on September 18, 2022

Comments

  • Ragesh Kr
    Ragesh Kr almost 2 years

    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
    
    • Spiff
      Spiff over 3 years
      Why do you have a lower-case "path" variable? Shouldn't it be all caps? I'm suspicious of how your shell environment is set up.
    • Ξένη Γήινος
      Ξένη Γήινος over 3 years
      Maybe it's case insensitive?
    • Por981c
      Por981c over 3 years
      There should be some questions asked by the install script, and some output. Can you share that? Also, is there anything installed in /opt/homebrew? If not, then the install didn't work. If there is (and I mean like a LOT of stuff), then the install might have worked but it might have failed to configure your shell properly. You mostly just need add /opt/homebrew/bin to your $PATH in your shell config file (which could be .profile, .bashrc, or .zshrc), like this: stackoverflow.com/questions/11530090/…