Keeping Homebrew Up to Date While Automatically Cleaning Old Versions from the Cellar
I like to keep my system up to date, and I depend on many Homebrew packages on my OS X machine. Homebrew allows for this but I:
- want it automated
- want it to delete old versions that I am no longer using
First, I added this into my .zshrc (could be in your .profile if you use Bash):
Then, I created remove_old_brews.rb in my home directory:
Ok, first the shell will check the current date and compare it with a date stored in a .brew_update_date file. If they differ, it immediately updates the file (so I can open another Terminal while the update is in progress), then it calls brew_update.
brew_update tells Homebrew to update its index, then upgrade all out of date packages. Finally, brew_update calls remove_old_brews.rb
remove_old_brews.rb looks at all installed packages in /usr/local/Library/LinkedKegs and finds packages with more than 1 version installed. For those, it finds whichever ones are not the currently linked version and removes them.
This is tuned for my workflow, but yours may differ. What I wanted was:
- update the first time I open Terminal in a day
- meaning if I really need a Terminal, I can just open another and the 2nd one won’t update
- keep what was updated at the front of my attention (which is why I didn’t just put it into cron)
And that’s about it.