DEV Community

stldo
stldo

Posted on • Originally published at stldo.com

The missing update command for Homebrew

Homebrew is a great tool for managing packages in macOS. It allows to quickly install and update dozens of libraries and apps that otherwise would consume much more time to compile. Whenever I need an open-source package, most likely it'll be available in Homebrew. It's a really useful tool, but I rarely use it to a point that keeps it up-to-date. Every time I need to install something, Homebrew is outdated. So, to keep things simple, I came up with the following shell function:

brew() {
  if [[ $@ == "update" ]]; then
    command brew update && \
      brew upgrade && \
      brew autoremove && \
      brew cleanup -s
  else
    command brew "$@"
  fi
}
Enter fullscreen mode Exit fullscreen mode

Just copy and paste it to your ~/.zshrc file — or ~/.bashrc, if you use Bash — and every time you run brew update, it'll run the following commands:

  • brew update: updates the package definitions and Homebrew itself;
  • brew upgrade: upgrades all the installed packages — brew install upgrades any installed dependencies required by a package, but if it finds conflicts with outdated packages requiring the same libraries, you'll need to sort this out;
  • brew autoremove && brew cleanup -s: I started using this command after discovering a 50 GB Homebrew cache folder in my machine, it only takes a few seconds to run and keeps everything tidy.

If you want to execute this routine every time you call brew install, you can use the following shell function:

brew() {
  if [[ $@ == "install" ]]; then
    command brew update && \
      brew upgrade && \
      brew autoremove && \
      brew cleanup -s && \
      brew "$@"
  else
    command brew "$@"
  fi
}
Enter fullscreen mode Exit fullscreen mode

In the past, I used this function with the brew install command, but now I prefer to use it with brew update. Sometimes my Homebrew is really outdated and it takes some time until the update is done. I prefer to leave it running while I do other things, and then run brew install package-name after everything went well. If you have any questions or know a better approach to keep Homebrew packages updated, please let me know, it'll be great to hear from you.

Top comments (0)