How should I fix my Homebrew installation after upgrading OS X?

Context

My employer gave me a MacBook Pro for development work; however, most of the development work I do in my research group is for Linux platforms, and for a host of policy reasons, I can't install virtualization software on this laptop. Homebrew looks like a good way in which to install these tools, especially because it looks very hackable (since it uses Ruby; MacPorts does not look hackable, because it uses Tcl). However, Homebrew relies on system libraries, and for security reasons, my employer may (and from what I hear, frequently will) upgrade my system at any time. I've heard that OS X upgrades will break Homebrew. (See, for instance, https://stackoverflow.com/questions/7779300/how-should-i-upgrade-xcode-after-upgrading-to-os-x-lion).

Question

How would I go about fixing my Homebrew install after an OS X upgrade? I am fine with deleting it and starting from scratch again, as long as I can do so in an automated way. (For instance, using Puppet plus a Homebrew provider to script a configuration that will automatically build upon running a shell script.) I've been Googling everywhere and can't seem to find any answers to this question.


brew update

Updates the Homebrew installation. If there are any errors, they should be reported by brew doctor. Try fixing them else, you could use the uninstall script as follows:

#!/bin/sh
# Just copy and paste the lines below (all at once, it won't work line by line!)
# MAKE SURE YOU ARE HAPPY WITH WHAT IT DOES FIRST! THERE IS NO WARRANTY!

function abort {
  echo "$1"
  exit 1
}

set -e

/usr/bin/which -s git || abort "brew install git first!"
test -d /usr/local/.git || abort "brew update first!"

cd `brew --prefix`
git checkout master
git ls-files -z | pbcopy
rm -rf Cellar
bin/brew prune
pbpaste | xargs -0 rm
rm -r Library/Homebrew Library/Aliases Library/Formula Library/Contributions
test -d Library/LinkedKegs && rm -r Library/LinkedKegs
rmdir -p bin Library share/man/man1 2> /dev/null
rm -rf .git
rm -rf ~/Library/Caches/Homebrew
rm -rf ~/Library/Logs/Homebrew
rm -rf /Library/Caches/Homebrew

And install homebrew again. Am pretty sure all this is configurable using puppet.


I think you might have been wondering about whether you need to rebuild all the packages rather than just uninstalling. If that is the case, I understand it is generally not a problem, as this answer says.

I've had problems, especially when changing to OS X Mavericks and Xcode 5 I had to re-link all the packages I had installed -- here's my script:

#!/bin/bash
FORMULAS=(`brew list`);
for FORMULA in "${FORMULAS[@]}"
do 
    echo "brew unlink $FORMULA" && echo "brew link $FORMULA";
    OUTPUT=`brew unlink $FORMULA`;
    echo $OUTPUT;
    OUTPUT=`brew link $FORMULA`;
    echo $OUTPUT;
done

Note the output, some formulas will require --force for the link step.

If that still doesn't work, try this command series from Mike McQuaid:

brew list > brew-list.txt
brew uninstall $(cat brew-list.txt)
brew install $(cat brew-list.txt)

There is now a rebuild command in HomeBrew, but that does not currently resolve dependencies.


If you want to force everything to upgrade to the latest version, you need to run brew with a --greedy flag

brew upgrade --greedy

Warning : this may have undesired effects, i.e. upgrade to the next version of the app, that requires a paid upgrade. For example I had Dash.app v5., with this it upgraded me to v6, which I didn't have a license for yet.

So to see what will get upgraded, run it first with --dry-run flag

brew upgrade --greedy --dry-run

Side-effect: if you previously removed a brew-installed app through other means (not via brew) - for example, I deleted Vivaldi browser just from the Application folder, using --greedy flag may re-install it, because brew will think that you still have an older version installed. You may want to clean that up first.