Today I learned you can automate and reproduce your Homebrew environment using a Brewfile

Automation is key to becoming an effective, efficient and pragmatic programmer:

"Automate everything. […] Whether you use something as simple as shell scripts or full-featured solutions such as Ansible, Puppet, Chef, or Salt, just don’t rely on any manual intervention."

The Pragmatic Programmer , David Thomas & Andrew Hunt

Since reading The Pragmatic Programmer (highly recommended!), I have been trying to automate every aspect of my development environment setup.

I had made a good start automating my terminal and editor configurations using dotfiles stored in a publicly accessible git repo that I could clone to new machines.

However, I still hadn’t worked out how to automate the installation of command line utilities installed via Homebrew. I just downloaded packages one by one as and when I needed them.

Turns out there is a command called bundle which creates a Brewfile that can be used to backup and automate the installation of your Homebrew packages.

Create a Brewfile From Your Existing Environment

# create Brewfile
brew bundle dump

This command creates a file called Brewfile in your current directory which contains a list of your current brew and cask packages.

For example:

# example Brewfile
tap "heroku/brew"
tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/cask-fonts"
tap "homebrew/cask-versions"
tap "homebrew/core"
brew "awscli"
brew "bash-completion"
brew "bat"
brew "cmake"
brew "cookiecutter"
brew "fontconfig"
brew "jq"
brew "neovim"
brew "pyenv"
brew "pyenv-virtualenv"
brew "ripgrep"
brew "ruby"
brew "stow"
brew "tmux"
brew "tree"
brew "wget"
brew "yarn"
cask "font-hack-nerd-font"

Install All Homebrew Packages

To restore your environment from another machine, copy the Brewfile created from the previous command to your new machine (or clone from publicly accessible repository) and run the following command:

# install packages from Brewfile in current directory
brew bundle

This will install all packages in the Brewfile in one go or upgrade them if already installed.

Note

The Brewfile does not support version lock syntax (e.g. package.lock.json) and will download the latest version of each package.

That’s all today, happy coding!

Resources

Here are some resources for further reading and more detailed documentation on the brew bundle command:

Further Reading