Backup and Configuration Processes for Homebrew

I received questions from followers on Twitter regarding my recent Homebrew article, so I'll be sharing an additional post covering installation and usage tips.

AA

In my recent blog post on package and dependency managers, I highlighted the many benefits they provide for developers, including convenient project/application development, backup, and sharing. However, while package managers like NPM and Yarn make it easy to manage dependencies and keep track of installed packages, homebrew's functionality is more limited in this regard. For example, if you need to reconfigure your macOS system, you cannot simply create a package.json file to list all the applications you have installed and use it to configure a new system, taking into account version differences.

In my blog post, I went into detail about some specific ways to use package managers well and gave more information about configuration options and best practices.Developers can get the most out of their package managers and speed up their development workflows by trying out the different tools and features that are available.

Homebrew Bundler

Homebrew Bundler is a non-Ruby package manager and dependency manager that can be used with Homebrew, Homebrew Cask, and the Mac App Store.

Installing Homebrew Bundler

By running the command brew bundle, Bundler will be automatically installed, or you can install it via tap (formula repository).

brew bundle
# or
brew tap Homebrew/bundle

After the installation process, you may encounter the error message 'Error: No Brewfile found' To create a Brewfile in the current directory, you can use the following command:

touch Brewfile

Then, you can define your dependencies within this file.

cask_args appdir: "/Applications"
tap "homebrew/cask"
tap "telemachus/brew", "https://telemachus@bitbucket.org/telemachus/brew.git", pin: true
brew "imagemagick"
brew "mysql@5.6", restart_service: true, link: true, conflicts_with: ["mysql"]
brew "emacs", args: ["with-cocoa", "with-gnutls"]
cask "google-chrome"
cask "java" unless system "/usr/libexec/java_home --failfast"
cask "firefox", args: { appdir: "~/my-apps/Applications" }
mas "1Password", id: 113234567

Using the Brewfile, all dependencies are now defined and addressed. To use an existing or prepared Brewfile, all you need to do is repeat the brew bundle command. The contents of the file in the current directory will be processed with the command, and the dependencies will begin to be installed to the directory defined as appdir.

So how can we turn installed packages/applications/programs on our system into a Brewfile without manually creating a file? The answer is quite simple. First, we need to find out the directory used by Brew. This is usually set to /usr/local/Homebrew/ or /opt/homebrew. You can verify this by running brew --prefix.

cd $(brew --prefix)
brew bundle dump

# Of course, you can also specify a directory and file
brew bundle --file=/path/to/Brewfile

Now you can backup the file and share it with anyone you want. Personally, I prefer to keep the file in my Dropbox account and update it frequently.

mv Brewfile ~/Dropbox

If I need to configure a new system, after installing Dropbox and syncing the folder, all I need to do is follow these steps:

cd ~/Dropbox
brew bundle

When the installation process starts, the console will show important information about the installation.

Using bat
Using git
Using hidapi
Using node
Using webp
Using pillow
...

If you frequently test or experiment with applications, I think you'll love the cleanup command. With cleanup, you can easily manage all installed packages that are not defined in the Brewfile. All you need to do is apply the relevant command:

brew bundle cleanup

Yes, that's it. For more detailed information about Homebrew Bundle and other commands, you can check GitHub > Homebrew Bundle1 page.