Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xshapira/f96a412e3dbba4309bf439ab88bfdc3b to your computer and use it in GitHub Desktop.
Save xshapira/f96a412e3dbba4309bf439ab88bfdc3b to your computer and use it in GitHub Desktop.
Homebrew: How to Backup & Restore Homebrew Packages [2022, MacOS]

Homebrew: How to Backup & Restore Homebrew Packages

Homebrew makes it easy to install and keep installed software up to date on your Mac - as part of my backup routine for my Mac I want to be able to run a single command to reinstall all packages.

If you're searching on how to backup & restore Homebrew, then I assume you're here for the commands.

Brewfiles

Brewfiles are files generated with definitions that Homebrew reads and processes, a generated Brewfile looks similar to the below:

tap "heroku/brew"
tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/cask-versions"
tap "homebrew/core"
tap "homebrew/services"
brew "borgmatic"
cask "iterm2-beta"

You can then run the following command to restore all taps, programs and cask-based applications in one go:

brew bundle

Backup

Simply run the following command to set up the repository

brew tap Homebrew/bundle

Once complete, you can then perform a single command to backup to your home directory.

brew bundle dump

By default, the Brewfile will be located at ~/Brewfile

Restoring

As mentioned before you can run the brew bundle command to restore from the backup but this will only restore from ~/Brewfile

You can use specific arguments to choose your Brewfile location

brew bundle --help
--file                        Read the Brewfile from this location. Use
                                      --file=- to pipe to stdin/stdout.
 Using --file  will allow you to select different Brewfiles. An example of this can be selecting a 
 file from a different snapshot.
  • Example: brew bundle --file=~/Brewfile-20200531

Brew Bundle

Per the Bundle github README, Bundler is a [1] “Bundler for non-Ruby dependencies from Homebrew, Homebrew Cask, Mac App Store(MAS) and Whalebrew.”

Brew bundle creates a text file called Brewfile listing all the currently installed applications managed via Brew and the Mac App Store. This Brewfile can be used as a backup and gives you the ability to restore applications when reinstalling macOS or setting up a new environment.

$ brew bundle --help

Bundle Installation

The first time the command, brew bundle, is run it will install, if not already.

brew bundle

Bundle Backup to Brewfile

To create the Brewfile use the command, brew bundle dump, and a text file with all the Brew, Mac App Store apps, and their dependancies will be created in the current directory.

brew bundle dump

Bundle Check and List

Using command, brew bundle check, will check to see if all dependancies are installed from the Brewfile. The command, brew bundle list, will list all the dependancies present in a Brewfile.

  • brew bundle list
  • brew bundle check

Bundle Cleanup

Using the command, brew bundle cleanup, will remove any apps installed, but not listed in the Brewfile.

When the command, brew bundle cleanup, the output shows what will be uninstalled. To actually uninstall the apps, use the command: brew bundle cleanup –force

  • brew bundle cleanup
  • brew bundle cleanup --force

Resources:

brew bundle (man)

Usage: brew bundle [subcommand]

Bundler for non-Ruby dependencies from Homebrew, Homebrew Cask, Mac App Store
and Whalebrew.

brew bundle [install]:
    Install and upgrade (by default) all dependencies from the Brewfile.

You can specify the Brewfile location using --file or by setting the
HOMEBREW_BUNDLE_FILE environment variable.

You can skip the installation of dependencies by adding space-separated values
to one or more of the following environment variables:
HOMEBREW_BUNDLE_BREW_SKIP, HOMEBREW_BUNDLE_CASK_SKIP,
HOMEBREW_BUNDLE_MAS_SKIP, HOMEBREW_BUNDLE_WHALEBREW_SKIP,
HOMEBREW_BUNDLE_TAP_SKIP.

brew bundle will output a Brewfile.lock.json in the same directory as the
Brewfile if all dependencies are installed successfully. This contains
dependency and system status information which can be useful in debugging brew
bundle failures and replicating a "last known good build" state. You can
opt-out of this behaviour by setting the HOMEBREW_BUNDLE_NO_LOCK environment
variable or passing the --no-lock option. You may wish to check this file into
the same version control system as your Brewfile (or ensure your version
control system ignores it if you'd prefer to rely on debugging information from
a local machine).

brew bundle dump:
    Write all installed casks/formulae/images/taps into a Brewfile in the
current directory.

brew bundle cleanup:
    Uninstall all dependencies not listed from the Brewfile.

This workflow is useful for maintainers or testers who regularly install lots of
formulae.

brew bundle check:
    Check if all dependencies are installed from the Brewfile.

This provides a successful exit code if everything is up-to-date, making it
useful for scripting.

brew bundle list:
    List all dependencies present in the Brewfile.

By default, only Homebrew dependencies are listed.

brew bundle exec command:
    Run an external command in an isolated build environment based on the
Brewfile dependencies.

This sanitized build environment ignores unrequested dependencies, which makes
sure that things you didn't specify in your Brewfile won't get picked up by
commands like bundle install, npm install, etc. It will also add compiler
flags which will help find keg-only dependencies like openssl, icu4c, etc.

      --file                       Read the Brewfile from this location. Use
                                   --file=- to pipe to stdin/stdout.
      --global                     Read the Brewfile from ~/.Brewfile.
  -v, --verbose                    install prints output from commands as they
                                   are run. check lists all missing
                                   dependencies.
      --no-upgrade                 install won't run brew upgrade on
                                   outdated dependencies. Note they may still be
                                   upgraded by brew install if needed.
  -f, --force                      dump overwrites an existing Brewfile.
                                   cleanup actually performs its cleanup
                                   operations.
      --cleanup                    install performs cleanup operation, same as
                                   running cleanup --force.
      --no-lock                    install won't output a
                                   Brewfile.lock.json.
      --all                        list all dependencies.
      --formula, --brews           list Homebrew dependencies.
      --cask, --casks              list Homebrew Cask dependencies.
      --tap, --taps                list tap dependencies.
      --mas                        list Mac App Store dependencies.
      --whalebrew                  list Whalebrew dependencies.
      --describe                   dump adds a description comment above each
                                   line, unless the dependency does not have a
                                   description.
      --no-restart                 dump does not add restart_service to
                                   formula lines.
      --zap                        cleanup casks using the zap command
                                   instead of uninstall.
  -d, --debug                      Display any debugging information.
  -q, --quiet                      Make some output more quiet.
  -h, --help                       Show this message.

bundle [subcommand]

Bundler for non-Ruby dependencies from Homebrew, Homebrew Cask, Mac App Store and Whalebrew.

brew bundle [install]

Install and upgrade (by default) all dependencies from the Brewfile.

You can specify the Brewfile location using --file or by setting the HOMEBREW_BUNDLE_FILE environment variable.

You can skip the installation of dependencies by adding space-separated values to one or more of the following environment variables: HOMEBREW_BUNDLE_BREW_SKIP, HOMEBREW_BUNDLE_CASK_SKIP, HOMEBREW_BUNDLE_MAS_SKIP, HOMEBREW_BUNDLE_WHALEBREW_SKIP, HOMEBREW_BUNDLE_TAP_SKIP.

brew bundle will output a Brewfile.lock.json in the same directory as the Brewfile if all dependencies are installed successfully. This contains dependency and system status information which can be useful in debugging brew bundle failures and replicating a “last known good build” state. You can opt-out of this behaviour by setting the HOMEBREW_BUNDLE_NO_LOCK environment variable or passing the --no-lock option. You may wish to check this file into the same version control system as your Brewfile (or ensure your version control system ignores it if you’d prefer to rely on debugging information from a local machine).

brew bundle dump

Write all installed casks/formulae/images/taps into a Brewfile in the current directory.

brew bundle cleanup Uninstall all dependencies not listed from the Brewfile.

This workflow is useful for maintainers or testers who regularly install lots of formulae.

brew bundle check

Check if all dependencies are installed from the Brewfile.

This provides a successful exit code if everything is up-to-date, making it useful for scripting.

brew bundle list

List all dependencies present in the Brewfile.

By default, only Homebrew dependencies are listed.

brew bundle exec command

Run an external command in an isolated build environment based on the Brewfile dependencies.

This sanitized build environment ignores unrequested dependencies, which makes sure that things you didn’t specify in your Brewfile won’t get picked up by commands like bundle install, npm install, etc. It will also add compiler flags which will help find keg-only dependencies like openssl, icu4c, etc.

 --file: Read the Brewfile from this location. Use --file=- to pipe to stdin/stdout.
 --global: Read the Brewfile from ~/.Brewfile.
 -v, --verbose: install prints output from commands as they are run. check lists all missing 
                dependencies.
 --no-upgrade: install won’t run brew upgrade on outdated dependencies. Note they may still be 
               upgraded by brew install if needed.
 -f, --force: dump overwrites an existing Brewfile. cleanup actually performs its cleanup 
              operations.
 --cleanup: install performs cleanup operation, same as running cleanup --force.
 --no-lock: install won’t output a Brewfile.lock.json.
 --all: list all dependencies.
 --formula: list Homebrew dependencies.
 --cask: list Homebrew Cask dependencies.
 --tap: list tap dependencies.
 --mas: list Mac App Store dependencies.
 --whalebrew: list Whalebrew dependencies.
 --describe: dump adds a description comment above each line, unless the dependency does not have a 
             description.
 --no-restart: dump does not add restart_service to formula lines.
 --zap: cleanup casks using the zap command instead of uninstall.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment