178

I am doing some Rails programming and I consistently see Homebrew referenced in solutions around the web but have never used it.

I also notice Homebrew in the terminal version 2.9 as an option next to "Shell -> New" from the terminal drop down but when I select homebrew and issue commands, they fail.

Usually with the "command not found" error.

Strangely enough I have been unable to locate a simple command to determine whether brew is installed or not.

How do I check to see if Homebrew is already installed on my Mac?

21 Answers 21

197

brew help. If brew is there, you get output. If not, you get 'command not found'. If you need to check in a script, you can work out how to redirect output and check $?.

3
  • 2
    I executed "brew help"and got nothing. I decided to just go ahead with the install and now "brew help" returns results. Thanks.
    – Kmb40
    Commented Feb 5, 2014 at 13:35
  • 65
    brew -v prints the Homebrew version
    – Saif
    Commented Apr 19, 2017 at 11:33
  • 1
    paste it to your command: /usr/bin/ruby -e "$(curl -fsSL raw.githubusercontent.com/Homebrew/install/master/install)"
    – Djama
    Commented Jun 30, 2017 at 15:20
70

I use this to perform update or install:

which -s brew
if [[ $? != 0 ]] ; then
    # Install Homebrew
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    brew update
fi
2
69

The standard way of figuring out if something is installed is to use which.

If Brew is installed.

>>> which brew
/usr/local/bin/brew

If Brew is not installed.

>>> which brew
brew not found

Note: The "not installed" message depends on your shell. zsh is shown above. bash will just not print anything. csh will say brew: Command not found. In the "installed" case, all shells will print the path.)

It works with all command line programs. Try which grep or which python. Since it tells you the program that you're running, it's helpful when debugging as well.

2
  • I am not getting anything when i wrote which brew on my mac machine terminal Commented Jan 25, 2017 at 11:29
  • 2
    Depending on which shell you use, you'll get different messages. That's a good clarification! Commented Jan 27, 2017 at 13:35
44

While which is the most common way of checking if a program is installed, it will tell you a program is installed ONLY if it's in the $PATH. So if your program is installed, but the $PATH wasn't updated for whatever reason*, which will tell you the program isn't installed.

(*One example scenario is changing from Bash to Zshell and ~/.zshrc not having the old $PATH from ~/.bash_profile)

command -v foo is a better alternative to which foo. command -v brew will output nothing if Homebrew is not installed

command -v brew

Here's a sample script to check if Homebrew is installed, install it if it isn't, update if it is.

if [[ $(command -v brew) == "" ]]; then
    echo "Installing Hombrew"
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    echo "Updating Homebrew"
    brew update
fi
3
  • 1
    Very useful. Thanks for posting alternative to which and an explanation.
    – SoEzPz
    Commented Sep 4, 2018 at 17:14
  • From the Bash manual, in the description of command: "Only builtin commands or commands found in the PATH are executed". So command, just as which, uses PATH (which makes sense: it needs to know where to look for). I have Homebrew installed on my computer but I broke my PATH and neither which nor command are able to find it.
    – bfontaine
    Commented Feb 9, 2022 at 17:23
  • are you sure about that? as per the POSIX manual, it seems that command can find shell functions and aliases and shell reserved words, not just built-in or special utilities man7.org/linux/man-pages/man1/command.1p.html
    – Aamnah
    Commented Feb 11, 2022 at 9:36
21

brew -v or brew --version does the trick!

0
13

I just type brew -v in terminal if you have it it will respond with the version number installed.

11

Location of the brew where it installed

which brew 

version of home brew install

brew --version
8

Maybe your mac don't received the path

Terminal Outcome

Run command below

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

And run to check that work brew help

3
  • Thank you - my brew disappeared after changing default shell to bash. This is the only solution that worked! Commented Jul 12, 2023 at 8:48
  • I'm very happy to help you !! Commented Oct 29, 2023 at 12:42
  • It works for me. Thanks @VinhTheNguyen
    – Antony
    Commented Jul 16 at 4:41
5
[ ! -f "`which brew`" ] && echo "not installed"

Explaination: If brew is not installed run command after &&

4

Beginners normally do, the homebrew --version which is wrong.

Do instead, brew --version. brew help works also. If these two commands are not executed, you don't have homebrew installed.

3

brew doctor checks if Homebrew is installed and working properly.

3

jest write brew -v in terminal and if you have it , you will see there version number and the install date . like this : Homebrew 3.3.12 Homebrew/homebrew-core (git revision c3cacc9cd1d; last commit 2022-01-31) Homebrew/homebrew-cask (git revision fb6ec06d8b; last commit 2022-01-31)

2

use either the which or type built-in tools.

i.e.: which brew or type brew

1

Another one possible way:

# Check if Ninja is installed
if ! which ninja > /dev/null
then
echo 'Ninja installation...'
brew install ninja
fi
1

In my case Mac OS High Sierra 10.13.6

brew -v

OutPut-
Homebrew 2.2.2
Homebrew/homebrew-core (git revision 71aa; last commit 2020-01-07)
Homebrew/homebrew-cask (git revision 84f00; last commit 2020-01-07)
1

Yes you can run which brew, but you may have it installed and it says it is not found if you are using zsh. You will need to add it to your .zshrc file.

1

I find it simple to use brew help command to find it is installed or not. There was a user guide on the homebrew download page.

If it is not installed then it will show 'command not found'

If you need to install homebrew then paste this on terminal:

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

1

Another way to do it is using the "command" builtin tool

if [ "$(command -v brew)" ]; then
    echo "command \"brew\" exists on system"
fi 
1

in your terminal, do which brew and itll tell you where it was installed at within your computer, but itll only work in zsh not in bash.

0

Once you install Homebrew, type command brew doctor in terminal.

  • If you get the following message:

    Your system is ready to brew

    then you are good to go and you have successfully installed homebrew.

  • If you get any warnings, you can try fixing it.

-1

Running Catalina 10.15.4 I ran the permissions command below to get brew to install

sudo chown -R $(whoami):admin /usr/local/* && sudo chmod -R g+rwx /usr/local/*

Not the answer you're looking for? Browse other questions tagged or ask your own question.