Homebrew .Brewfile, Add/Remove to .Brewfile automatically in ZShell

Logo

Homebrew

Homebrew is an awesome package manager available for both macOS and Linux, although my personal experience is mainly with macOS.

To list installed packages, can be done with:

brew list

So how would you automate a process of saving this list of installed packages? To share or simply to backup for later use?

This can be done using bundle dump

brew bundle dump --file=/path/to/Brewfile

The dumped file contains taps, brews, casks, mas’s (Mac App Store command-line) and vscode extension (if applicable).

To install all of the contents inside a Brewfile:

brew bundle install --file=/path/to/Brewfile

Create and Update Brewfile Automatically in ZShell

To create and update a Brewfile automatically in ZShell (the default shell in macOS), I’ve created the following zsh function (can be placed inside ~/.zshrc)

function brew() {
/opt/homebrew/bin/brew "$@"
 if [[ "$1" == "install" || "$1" == "remove" || "$1" == "uninstall" ]]; then
  /opt/homebrew/bin/brew bundle dump --force --file=~/.Brewfile
 fi
}

The above function will maintain a .Brewfile in your homedir (hence the ~). During install, remove and uninstall.

Keep on brewin’

Leave a Reply

Your email address will not be published. Required fields are marked *