With Homebrew, how to check if a software/package is installed?
I'm installing a set of softwares/packages/formulas via homebrew with command like
brew install <formula1>
...
brew cask install <formula2>
...
I wonder if it's a good idea to verify if the softwares <formula1>
, ..., <formula2>
, ... are already installed and only trigger the above commands for the ones NOT already installed. If so, how?
you could do something like this:
brew list <formula1> || brew install <formula1>
This will error on list and continue with install if not installed otherwise it will just list package files. (one could modify this further as a function or alias in .bashrc to make it easier to type)
It should also be noted that you can type brew info <formula>
which will tell you whether or not a formula is installed. You can parse the response for "Not installed" and then run the installer if it finds the string.
Building on the earlier answers, but packed into a ready to use function without all the homebrew logging:
brew_install() {
echo "\nInstalling $1"
if brew list $1 &>/dev/null; then
echo "${1} is already installed"
else
brew install $1 && echo "$1 is installed"
fi
}
brew_install "ripgrep"
In similar vein to Hans Fredric, here is a snippet I actually use myself. The funny looking <(cmd)
is Bash command substitution.
alias strip-empty="egrep -v '^\s*$'"
NOT_INSTALLED=$(comm -23 <(sort < apps.local) <( brew list --versions | awk '{print $1}' ) | strip-empty)
while read FORMULA; do
brew install "$FORMULA"
done <<< "$NOT_INSTALLED"
Here, apps.local
is just a list of apps to install, one per line. The improvement over just looping over each app and trying something like brew_install
basically comes down to speed. Invoking brew list
is slow (like up to a second), so I just do the test once by listing out all installed apps. The difference is very noticeable if you have > 5 apps.
If you need something with the same speed, but that works equally well with apps installed using a cask, you need something more elaborate (like this).