Detect if homebrew package is installed

Viewed 55617

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.

5 Answers
# 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

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}

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.

Related