123

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?

0

13 Answers 13

175

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.

8
  • 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. Dec 6, 2021 at 3:30
  • 14
    I am using Apple Mac M1 Pro CPU. This worked for me. Thanks
    – TommyDo
    Jan 17, 2022 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 7, 2022 at 9: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, 2022 at 2: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, 2022 at 0:01
107
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

This code works for me on a MacBook Pro M1.

PS: If you are using bash on M1, run

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

instead of the first line above.

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 2, 2022 at 9:51
  • 1
    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, 2022 at 9:06
  • to make it work on M1 with bash, more or less same but: echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
    – os11k
    Apr 28 at 8:33
  • can confirm on my M2 as well. thank you so much. Jun 4 at 16:55
27

"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

3
  • yes, I already did it.
    – cindywmiao
    Oct 9, 2014 at 16:34
  • 1
    did you also rebooted your mac? this is mandatory after changes in bash/bashrc/bash_profile
    – AlessioX
    Feb 8, 2016 at 18:40
  • 4
    @Alessiox no it is not necessary to reboot the mac - only need to start a new Terminal and run from there (or even on most cases resource ~/.bash_profile
    – mmmmmm
    Feb 8, 2016 at 18:51
16

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.

3
  • 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, 2021 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, 2021 at 10:51
  • It Works! macOS: Big Sur (version 11.4) Chip: Apple M1 Mar 15 at 10:50
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

1
  • 8
    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, 2021 at 15:52
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
2
  • If your Mac is M1 based, you could edit the PATH variable with ~/.bash_profile add export PATH=$PATH:/opt/homebrew/bin and save. Exit the terminal. Now run brew command Sep 2, 2022 at 13:30
  • tnx, work for me! Dec 9, 2022 at 14:34
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.

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"

1
  • 1
    No because the OP says he gets the error "It appears Homebrew is already installed. "
    – mmmmmm
    Feb 6, 2019 at 12:31
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.

3
  • Incorrect. .profile is not read if .bash_profile exists .bashrc is read by all interactive bash sessions see bash documentation gnu.org/software/bash/manual/bash.html#Bash-Startup-Files
    – mmmmmm
    Sep 16, 2021 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, 2021 at 13:29
  • Agreed that it works but the reason is wrong - to check on that how are you starting bash
    – mmmmmm
    Sep 16, 2021 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.

-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)"
0
-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

-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

4
  • 1
    What does this do, how does it solve the problem described in the question?
    – nohillside
    Mar 19, 2022 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.
    – Community Bot
    Mar 19, 2022 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, 2022 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, 2022 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .