Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if homebrew package is installed

I'm about to write a shell script to detect if several homebrew packages are installed in the system. Is there a way to use a brew command to achieve that?

I tried using the exit code of brew install <formula> --dry-run. But this builds the package if it is missing.

like image 603
iltempo Avatar asked Dec 27 '13 14:12

iltempo


People also ask

How do I find Homebrew packages?

brew search <search term> will list the possible packages that you can install. brew search post will return multiple packages that are available to install that have post in their name. brew info <package name> will display some basic information about the package in question.

Is Homebrew installed on Mac by default?

Homebrew boasts that it "installs the stuff you need that Apple (or your Linux system) didn't" install by default. Installation happens with the brew command, which gives us access to thousands of command-line utilities, but not more complex applications.


5 Answers

You can use

brew ls --versions myformula

to output the installed versions of the respective formula. If the formula is not installed, the output will be empty.

When using a recent versions of homebrew, which you can get with brew update, you can just run this (thanks Slaven):

if brew ls --versions myformula > /dev/null; then
  # The package is installed
else
  # The package is not installed
fi

That said, it is probably a good idea to check for the existence of the tool at all and not just checking for the respective homebrew package (e.g. by searching for the executable in the $PATH). People tend to install tools in a rather large amount of ways in practice, with homebrew being just one of them.

like image 120
Holger Just Avatar answered Oct 11 '22 09:10

Holger Just


What about?

for pkg in macvim ngrep other needed packages; do
    if brew list -1 | grep -q "^${pkg}\$"; then
        echo "Package '$pkg' is installed"
    else
        echo "Package '$pkg' is not installed"
    fi
done
like image 31
Johannes Weiss Avatar answered Oct 11 '22 11:10

Johannes Weiss


# install if we haven't installed any version
brew ls --versions $lib || brew install $lib
# install if we haven't installed latest version
brew outdated $lib || brew install $lib
like image 34
timotheecour Avatar answered Oct 11 '22 11:10

timotheecour


Easiest two-liners: Step one, make sure it's installed

$ realpath . || brew install coreutils

This will print out the realpath of current dir, if not, then it will install it. And it will not fail even realpath not found.

Step two, call it in your actual code:

$ realpath ${someDir}
like image 1
ForeverYang Avatar answered Oct 11 '22 10:10

ForeverYang


For script and automation usage, I found out that brew bundle --help is very convenient.

If you do not want to use real bundle file, this snippet works fine in scripts:

brew bundle -v --file=- <<-EOF
brew "mc"
brew "ffmpeg"
brew "wget"
cask "cpuinfo"
cask "intel-power-gadget"
cask "unetbootin"
cask "vlc"
EOF

The good side of it, it will automatically detect if package is not installed, if it is outdated and only then will install it.

If you do not want updates, add a flag --no-upgrade. I have put -v for verbosity, as want more details, but you can skip it, or even use -q for even more silent run.

like image 1
Arunas Bartisius Avatar answered Oct 11 '22 10:10

Arunas Bartisius