100

I installed brew before, but it does not work now.

When I run brew, it gives me the error -bash: brew: command not found

So I tried to reinstall using the code

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

But it gives me another one :

It appears Homebrew is already installed. If your intent is to reinstall you should do the following before running this installer again: rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup

If I use the command rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup, it gives back the first error : -bash: brew: command not found

What should I do now?

F1Linux
  • 1,581
  • 1
  • 14
  • 33
cindywmiao
  • 1,123
  • 2
  • 8
  • 5

13 Answers13

144

I had the same problem—installed brew, used it, but now it doens't work, ie, brew command not recognized anymore.

The context of my brew-not-recognized-anymore problem is a bit more specific: I'm using iTerm instead of Terminal, I installed brew in the standard way to the standard place, I used brew to install zsh and oh-my-zsh, and at that point the brew command stopped working.

I wasn't able to find the solution to my subset of the brew-not-recognized-anymore problem anywhere, so I'm posting the solution in case anyone else has this sort of brew-not-recognized-anymore problem.

Adding this to my .zshrc solved the problem on a machine with Apple M1 CPU:

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

Note that this is not the same as adding to the PATH variable. The brew command is indeed located in those standard locations listed in other solutions. It turned out not to be a PATH variable thing that was causing this subset of the brew-not-recognized-anymore problem.

mmmmmm
  • 28,660
  • 17
  • 84
  • 140
Matt S
  • 1,549
  • 1
  • 4
  • 3
  • 1
    I don't know if it needs to be run every time I load my terminal, but this is the command brew had me run after installation (right after one other command) and that fixed my issue. – Eric Majerus Dec 06 '21 at 03:30
  • 9
    I am using Apple Mac M1 Pro CPU. This worked for me. Thanks – TommyDo Jan 17 '22 at 19:41
  • 1
    I had exactly the same issue as Matt, brew was not recognized anymore after installing oh-my-zsh. The above solution works, but I have to run that command every time I open the terminal. Is there a way to make it permanent? – cholo14 Feb 07 '22 at 09:45
  • 7
    @cholo14 `open -a textedit ~/.zshrc` and add the command on it's own line at the top of the file and save the file. if the file doesn't exist, create it by running `touch ~/.zshrc` – Matt S Feb 11 '22 at 02:33
  • 1
    Can confirm that > eval "$(homebrew/bin/brew shellenv)" is enough to get my brew back up and running. Thanks for pointing me in the right direction! – user454322 Mar 26 '22 at 00:01
  • For those using `fish` adding the same snippet to `.config/fish/config.fish` makes it work for `fish` on an M1 Mac. Thanks for the solution – MkMan Jun 20 '22 at 22:54
  • Keep in mind you may have to re-open your Terminal window for changes to path or changes made by eval "$(/opt/homebrew/bin/brew shellenv)" to be recognised. – BeNice Sep 11 '22 at 08:23
83
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

This code works for me on a MacBook Pro M1.

nohillside
  • 92,308
  • 39
  • 198
  • 242
hriday
  • 939
  • 3
  • 4
  • can confirm that this is the right way - I type in `brew help shellenv` and it says `Consider adding evaluation of this command's output to your dotfiles (e.g. ~/.profile, ~/.bash_profile, or ~/.zprofile) with: eval "$(brew shellenv)"` – CatFly Sep 02 '22 at 09:51
  • The applications I installed were working only in the terminal window I was installing. When I closed the window I had to re-install it. I added these commands to the text opened with the "nano ~/.bash_profile" command and closed it. After this process, everything worked as it should. – Mete Dec 28 '22 at 09:06
26

"Command not found" indicates that the brew binary isn't on one of the directories listed in your PATH environment variable. To check what your path is currently, run echo $PATH. By default, brew installs to /usr/local/bin. To set the path permanently, you should edit your ~/.bashrc file to contain the line export PATH="/usr/local/bin:$PATH".

On Aplle Silicon machines brew by default install path is /opt/homebrew/bin

mmmmmm
  • 28,660
  • 17
  • 84
  • 140
Phil Reinhold
  • 361
  • 2
  • 2
14

the -bash: brew: command not found error means that it didn't find the brew environment variable on your Mac.

The correct environment configuration in .bash_profile file should be like this:

export PATH="/usr/local/bin:$PATH"

So if you haven't config the brew environment variable, the correct step to config the brew environment variable on your Mac is:

  1. add the environment variable to your mac, the correct command is:

    echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
    
  2. Now we need to activate the changes. Type this command to activate and try drew doctor again:

    source .bash_profile
    

    Or if you had config the environment, you can use this command to open the file to check if the environment configuration is correct:

    /usr/bin/open ~/.bash_profile
    

    and then fix it to the correct text and then save and quit.

  3. Don't forget to activate your changes also. Still this command:

    source .bash_profile

nohillside
  • 92,308
  • 39
  • 198
  • 242
Alan Keene
  • 157
  • 1
  • 4
  • 7
    Depending on where `brew` was installed you might have to replace `export PATH="/usr/local/bin:$PATH"` with `export PATH=$PATH:/opt/homebrew/bin`. All other steps are equal. – Jakob May 24 '21 at 15:52
13

If your shell is zsh and your Mac is M1-based, enter these two commands after installing brew:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

The first command adds:

  • an expanded PATH variable that includes the directories where brew and commands installed with brew are located
  • several shell variables used by brew

to the .zprofile initialization file.

The second command adds those same variables to the current shell, so that you can start using brew without re-reading .zprofile.

PS: For Intel Macs, use /usr/local instead of /opt/homebrew.

PPS: If you are running bash, use .bashrc instead of .zprofile.

nohillside
  • 92,308
  • 39
  • 198
  • 242
Chirag Awale
  • 131
  • 1
  • 2
  • 1
    For most homebrew installs and definitely when this question was written Homebrew install in /usr/local as one of its main selling points so that you don't need to edit the PATH in any shell files. Only in the last few months has it been forced to use a more standard Unix path and you have to edit the PATH – mmmmmm Jul 21 '21 at 22:06
  • 1
    Also this assumes both an M1 Mac (Homebrew installs to `/opt/homebrew` on M1 only) and use of `zsh` (which is the modern default but doesn't automatically apply to accounts created before the switch). Can you edit the answer to make it more generic, and also explain what the commands actually do and how they solve the problem described in the question? – nohillside Jul 22 '21 at 10:51
5

If your shell is zsh and your Mac is M1-based

edit .zshrc

nano .zshrc

add

export PATH=/opt/homebrew/bin:$PATH

Quit terminal and re-open

try with

brew
1

The FAQ has a section on this and a specific uninstall script, start there to get a clean system and then install again.

https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/FAQ.md

To uninstall Homebrew, paste the command below in a terminal prompt.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

Download the uninstall script and run ./uninstall --help to view more uninstall options.

Yuri Schimke
  • 389
  • 3
  • 10
0

Might be that you just never downloaded.. I made that mistake. Thought I'd downloaded right, but found out I hit something other then return when it says to "press return to continue or any other key to cancel"

Anon
  • 29
  • 1
0

This is what I did on Ubuntu 20:

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"'>> ~/.bashrc

in contrast with what brew documentation suggests:

==> Next steps: Run these two commands in your terminal to add Homebrew to your PATH:

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/user/.profile

eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

It seems .profile does not get read when .bashrc is there.

DimiDak
  • 166
  • 1
  • 5
  • Incorrect. .profile is not read if .bash_profile exists .bashrc is read by all interactive bash sessions see bash documentation https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files – mmmmmm Sep 16 '21 at 12:57
  • @mmmmmm This is also mentioned in ".profile" but ".bash_profile" is not there and yet thet first does not get read. The point is that this solution works. – DimiDak Sep 16 '21 at 13:29
  • Agreed that it works but the reason is wrong - to check on that how are you starting bash – mmmmmm Sep 16 '21 at 13:51
-1

I chased my tail with the "command not found" error. I thought upgrading to Catalina might have borked my Homebrew installation (upgrading OSX requires you to upgrade MacPorts).

Half-Broken:

I checked my path and /usr/local/bin was there and an ls -al of that directory listed the installed Homebrew packages. So why was Homebrew still not happy?!?!? I tried another Homebrew command and that command SUCCEEDED. So Homebrew was half-broken: some things work, some things don't. Drat.

Solution:

The command puking the error command not found was mtr which requires sudo access. Although the Homebrew documentation and every other answer I found on this error tells you to add /usr/local/binto your path, executables requiring sudo access live in the path:

/usr/local/sbin

Add this to:

/etc/paths

And things will work as expected.

F1Linux
  • 1,581
  • 1
  • 14
  • 33
-1

the easy answer here is: you need to read the output of the installation.

after your run the brew command it will guide you what to do next.

In my case it did tell me to run the following 2 commands to make brew available at the PATH

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/moelwekil/.zprofile    
eval "$(/opt/homebrew/bin/brew shellenv)"
-2

Following command doesn't work if your are under proxy.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Instead use following -

ruby -e "$(curl -x http://DOMAIN%5cUSER_NAME:PASSWORD@PROXY:PORT -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Note we have to use %5c instead of "\" Similarly if your password has any special character replace it with unicode e.g for @ use %40 Refer this Unicodes

Replace above command with your own params

DOMAIN - Your Domain

USER_NAME - Your User Name

PASSWORD - password

PROXY - 10.10.10.10

PORT - 8080

Harshad
  • 105
  • 1
-2
sudo ln -s /opt/homebrew/bin/brew /usr/local/bin/brew

This line bind homebrew from /opt with /usr/local, so you can use brew command everywhere. This is the only thing that helped me.

source

carrymiss
  • 1
  • 1
  • 1
    What does this do, how does it solve the problem described in the question? – nohillside Mar 19 '22 at 16:25
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '22 at 16:28
  • 1
    Woudn't it be easier to add `/opt/homebrew/bin` to `PATH` then, with the additional benefit that any command installed via Homebrew will also work? – nohillside Mar 20 '22 at 17:48
  • @nohillside I don't know, bro. Nothing else worked but this, so I shared the solution. Do you understand?:)) – carrymiss Mar 21 '22 at 14:04