brew: create install script for all installed packages/casks

I am currently planning to reinstall my OS from scratch. Over the years, I installed a LOT OF packages via brew. Obviously, I'd like to reinstall those after setting up the system again, and I want to automate this as much as possible.

Hence my question: How do I list all packages installed via brew, including casks, and how do I create an "install script" from those that will add all added casks for me and then install all packages again?"

Apple Silicon: port all Homebrew packages under /usr/local/opt/ to /opt/homebrew asks a similar question, but doesn't address the creation of a automated install script, rather the port between two architectures.


Solution 1:

As basically explained in Apple Silicon: port all Homebrew packages under /usr/local/opt/ to /opt/homebrew (and in https://docs.brew.sh/Manpage#bundle-subcommand):

  • Create a Brewfile with brew bundle dump
  • Store the file somewhere safe (flash drive for instance)
  • Once the computer is set up again, install Homebrew, copy the Brewfile back and run brew bundle install --file /path/to/Brewfile