KitBench

Practical Mac, terminal, and digital workflow guides tested on real hardware.

Tested Mac and terminal guides.

3–5 minutes

Homebrew maintenance on macOS: cleanup, Brewfile backups, and fixing broken formulas

Keep Homebrew tidy and reproducible on Apple Silicon with a monthly maintenance routine, automated Brewfile exports, and fixes for PATH and library errors.

KitBench guide cover for Homebrew maintenance and Brewfile backups on macOS

Sources are linked in this article. Found an error? Report a correction.

After a year of normal use, Homebrew collects gigabytes of cached downloads, unused dependencies, and broken symlinks left behind by macOS upgrades. Four commands (brew doctor, brew autoremove, brew cleanup, and brew bundle dump) keep your package tree clean and produce a plain-text Brewfile that rebuilds your Mac toolkit on a fresh machine.

All commands below were verified with Homebrew 7.0.17 on Apple Silicon macOS 27.2.1 (where Homebrew installs under /opt/homebrew).

A monthly four-command Homebrew maintenance routine

Run these steps in order whenever you want to update formulas and recover disk space safely:

# 1. Check outdated packages before upgrading
brew update && brew outdated

# 2. Upgrade CLI formulas (add --cask if you also want GUI apps updated)
brew upgrade

# 3. Remove dependencies no longer required by any installed formula
brew autoremove

# 4. Preview, then delete old version directories and cached downloads
brew cleanup -n
brew cleanup --prune=all

Run brew cleanup -n (dry run) first if an older project might still link to a previous Node or Python minor version. Once the preview looks safe, brew cleanup --prune=all empties old downloads from $(brew --cache), often freeing 3 to 15 GB of macOS System Data. For broader storage cleanup across macOS, see our macOS System Data storage guide.

Export your Mac toolkit as code with a Brewfile

A Brewfile lists every tap, CLI formula, GUI cask, and Mac App Store app (via mas) you installed intentionally. Storing that file in a dotfiles repo or backup folder turns a new Mac setup into one command:

# Export current Homebrew inventory with short descriptions
brew bundle dump --describe --force --file=~/Brewfile

# Inspect the generated list
head -n 30 ~/Brewfile

On a replacement Mac, install Homebrew and run:

brew bundle install --file=~/Brewfile

To trim tools you installed once months ago, remove their lines from ~/Brewfile, preview removals with brew bundle cleanup --file=~/Brewfile, and add --force only after reviewing every item it plans to uninstall.

Pin databases and runtimes before major upgrades

A blanket brew upgrade can bump PostgreSQL, OpenSSL, or Python across major releases right before a deadline. Pin any database or runtime that needs a planned migration:

brew pin postgresql@17
brew list --pinned
# Later, when ready to migrate:
brew unpin postgresql@17

Fixing four common Homebrew errors on Apple Silicon

Error symptomCauseFix
zsh: command not found: brew/opt/homebrew/bin missing from PATH in ~/.zprofile.Add eval "$(/opt/homebrew/bin/brew shellenv)" to ~/.zprofile.
macOS system binary runs instead of Homebrew (e.g., rsync, git)/usr/bin precedes /opt/homebrew/bin in PATH.Check which -a rsync and run brew shellenv early in shell init.
dyld: Library not loaded after an upgradeA formula still links against an older shared library version.Run brew reinstall <formula> and brew doctor to find broken links.
Xcode Command Line Tools mismatch after macOS upgrademacOS update invalidated old CLT headers.Run sudo rm -rf /Library/Developer/CommandLineTools && xcode-select --install.

Keep Homebrew from slowing down new terminal tabs

Calling $(brew --prefix) inside ~/.zshrc launches a slow subprocess on every new tab, adding 150 to 300 ms per call. Use /opt/homebrew directly on Apple Silicon or evaluate brew shellenv once in ~/.zprofile. Measure the speedup with our Zsh startup optimization guide, and store your Brewfile alongside a full 3-2-1 Mac backup strategy.

Verify package health and recover after a major macOS upgrade

Major macOS point releases frequently update system SDK headers and dynamic libraries. Right after upgrading macOS, run a quick health audit before starting project builds so missing headers do not fail halfway through an install:

# Check Homebrew configuration, permissions, and unlinked kegs
brew doctor

# List only top-level formulas you installed explicitly (excluding dependencies)
brew leaves -r

# Check background services managed by Homebrew (PostgreSQL, Redis, etc.)
brew services list

Running brew leaves -r is especially helpful once every few months. While brew list dumps hundreds of transitive libraries, brew leaves shows only the top-level tools you asked for. Spot anything you no longer use, uninstall it with brew uninstall <name>, and follow with brew autoremove to drop its orphaned dependencies cleanly.

If you rely on background services such as PostgreSQL or Redis through brew services, stop the service cleanly (brew services stop postgresql@17) before upgrading its major version and verify your data directory backup first. Keeping both a fresh ~/Brewfile and a current Time Machine backup lets you roll back any broken toolchain upgrade in minutes.

Sources and verification

Stay in the loop

Get tested Mac, terminal, and workflow guides when new tutorials publish. Unsubscribe anytime.

Comments

Questions, corrections, and useful counterpoints are welcome. Keep comments specific and on topic.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.