Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upgrade or install a homebrew formula

Tags:

homebrew

In my CI-setup, i would like to make sure that the newest version of a given formula is installed, regardless of whether it is already installed or not.

i'm currently using something like:

brew update
brew install FORMULA || (brew upgrade FORMULA && brew cleanup FORMULA)

What are the pitfalls with that approach? Is there a nicer approach to the problem (e.g. by first querying whether FORMULA is already installed, rather than relying on brew install to fail only if FORMULA is installed)?

like image 602
umläute Avatar asked Apr 25 '17 19:04

umläute


People also ask

What is the difference between brew update and upgrade?

brew update and upgradebrew update updates the above downloaded git repository with the latest code from GitHub. brew upgrade updates the actual packages to match the versions in the updated local git repository.

What is a Homebrew formula?

Homebrew Formulae is an online package browser for Homebrew – the macOS (and Linux) package manager. For more information on how to install and use Homebrew see our homepage.

Where does Homebrew store formulas?

Packages are installed according to their formulae, which live in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula .


1 Answers

I you want to install a Homebrew package if it does not already exist, and upgrade it otherwise, the best solution is to use Homebrew Bundle which is officially part of the Homebrew family. If that doesn't work for you, and you want to roll your own solution, you should reference at the suggestions below.

There are other situation where a brew install might fail, other than a package already being installed. I'm not sure, but it doesn't look like the brew install command emits an exit status other than 1 on failure, so you have two options:

  1. Search stderr for "not installed" and check against that
  2. Use a different approach

The most common approach I've seen used for this purpose is to check if the package is installed with the command brew ls --versions:

function install_or_upgrade {
    if brew ls --versions "$1" >/dev/null; then
        HOMEBREW_NO_AUTO_UPDATE=1 brew upgrade "$1"
    else
        HOMEBREW_NO_AUTO_UPDATE=1 brew install "$1"
    fi
}

You'll want to use HOMEBREW_NO_AUTO_UPDATE=1 if you're installing multiple packages so that Homebrew does not try to update in between each install/upgrade.

like image 175
joeyhoer Avatar answered Jan 04 '23 05:01

joeyhoer