Migrating Homebrew Installations: Restoring Without the Original Computer

Have you ever been faced with the problem of trying to restore/migrate your homebrew packages between MacOS installations without access to the original computer? I was recently faced with this, and managed to solve this issue and restore from a timemachine backup without access to the original machine.

How Homebrew Stores Packages:

Homebrew installs all packages to a location that depends on your installation environment. If you have an ARM based Mac, You’ll find them in opt/homebrew/cellar . Intel-Based Mac installations will have them in /usr/local/cellar . You can find your current location by running brew --prefix

brew --prefix
/usr/local

You’ll find a list of folders containing all of the packages, as well as a file in each folder called called INSTALL_RECEIPT.json .

ansible
└── 9.6.0
├── COPYING
├── INSTALL_RECEIPT.json

This INSTALL_RECEIPT json file can be used to determine if the packages are installed as part of a dependency or not. Since I don’t want to reinstall a bunch of dependent packages, just the original packages that may or may not use them, we can check the value of installed_as_dependency using a tool like jq to determine if this is a package I want to manually reinstall, or not.

"installed_as_dependency": false

Step-by-Step Migration Guide:

Back Up Your Homebrew Cellar

Start by copying your Homebrew Cellar directory from the old system. If you have a Time Machine backup or any other form of backup, locate the /opt/homebrew/Cellar directory.

Set Up the New System

Install Homebrew on your new computer if it’s not already installed. You can do this by running the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Copy the Cellar Directory

Transfer the Cellar directory from your backup to the new system. Note the location and use it for cellar_dir in the script below

Generate a Package List

Let’s use a bash script to identify and list packages that were not installed as dependencies. Here’s the script you’ll use (Modify cellar_dir to fit your environment):

Reinstall the Packages

After running the script, you’ll have a reinstall_packages.txt file with all the packages that were not installed as dependencies. Use this command to reinstall them:

xargs brew install < reinstall_packages.txt

Share this content:

Leave a Comment

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