Skip to content
CoreMacs
macOSGuide

Provisioning a fresh Mac reproducibly: Homebrew Bundle, version managers and scripts

A new build or developer Mac does not need hand configuration. This guide covers a repeatable setup: Command Line Tools, a Brewfile, pinned Ruby and Node, fastlane through Bundler, dotfiles in git and a smoke test.

CoreMacs News Desk·Published ·Last verified ·6 min read
A Mac mini beside its white retail box and a coiled power cable on a wooden desk.

A freshly unboxed Mac is a blank slate, and the quickest way to waste an afternoon is to configure it from memory. A scripted setup turns that afternoon into one command you can rerun, and it gives you something more valuable: a second machine that matches the first.

Start with a repository, not a checklist

Create one repository, for example mac-setup, as the single description of your machines. The version files and the Gemfile sit at its root, because the tools look for them in the current directory, not in subfolders.

mac-setup/
  bootstrap.sh
  smoke-test.sh
  Brewfile
  Gemfile
  Gemfile.lock
  .ruby-version
  .node-version
  dotfiles/
    .zshrc
    .gitconfig

Command Line Tools and Homebrew

Homebrew's documentation says Command Line Tools for Xcode are needed when formulae are built from source, while bottles and casks on Apple Silicon do not need developer tools. A build machine nearly always wants them anyway:

xcode-select --install

Apple's documentation says a system dialog appears and you click Install, so an unattended script cannot rely on this alone. Check xcode-select -p first, and if the tools are absent, run the install and ask the person to rerun the script afterwards.

Homebrew's documentation sends readers to its homepage, brew.sh, for the supported installer. The homepage gives a bash one-liner and also offers a .pkg installer for macOS. Read remote scripts before running them:

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

On Apple Silicon, Homebrew uses /opt/homebrew, and the documentation warns it will not work until the shellenv line is in your shell configuration:

eval "$(/opt/homebrew/bin/brew shellenv)"

Describe your tools in a Brewfile

Homebrew's Brewfile page shows one entry per line, using tap, brew, cask and mas. A minimal example:

brew "git"
brew "rbenv"
brew "nodenv"
cask "firefox"

Check names with brew search before committing. Lines starting with mas install from the Mac App Store through the mas tool, which brew bundle installs if it is missing; the mas README says this needs an Apple Account signed in to the App Store. Full Xcode, which provides xcodebuild, is not in this example Brewfile; install it separately.

Per the manual, brew bundle check exits successfully when everything in the Brewfile is installed and up to date. --no-upgrade skips the outdated check, and --verbose lists what is missing. A Brewfile has no lock file; the documentation says Bundle does not and will not have one.

Pin languages and fastlane

fastlane's documentation discourages the macOS system Ruby and names rbenv as a popular alternative. nodenv is the Node counterpart. Both come from Homebrew, and install is supplied by their ruby-build and node-build plugins. Install a version first, because the local command writes the version file for an installed version:

rbenv install 3.3.0     # example version, pick a current one
rbenv local 3.3.0       # writes .ruby-version
nodenv install 22.0.0   # example version, pick a current one
nodenv local 22.0.0     # writes .node-version

Run without arguments, rbenv init and nodenv init edit a shell startup file themselves instead of printing instructions. For rbenv the script uses ~/.zprofile unless ~/.zshrc already mentions rbenv. To keep the setup in your repository, add the lines to dotfiles/.zshrc yourself:

eval "$(rbenv init - --no-rehash zsh)"
eval "$(nodenv init - --no-rehash zsh)"

fastlane through Bundler

fastlane's setup guide prefers Bundler. Keep a Gemfile in the repository root with the two lines it shows, source "https://rubygems.org" and gem "fastlane", run bundle update, and commit both Gemfile and Gemfile.lock. Run lanes with bundle exec fastlane.

Write the bootstrap so it can run twice

An idempotent script checks state before it acts, so reruns after a failure are safe.

#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"

if ! xcode-select -p >/dev/null 2>&1; then
  xcode-select --install
  echo "Finish the tools install, then rerun."
  exit 1
fi

if ! command -v brew >/dev/null 2>&1; then
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
eval "$(/opt/homebrew/bin/brew shellenv)"

brew bundle check --file=Brewfile || brew bundle install --file=Brewfile

ln -sfn "$PWD/dotfiles/.zshrc" "$HOME/.zshrc"
ln -sfn "$PWD/dotfiles/.gitconfig" "$HOME/.gitconfig"

eval "$(rbenv init - bash)"
eval "$(nodenv init - bash)"
rbenv install --skip-existing
nodenv install --skip-existing
gem install bundler
bundle install

./smoke-test.sh

With no version argument, both install commands use the version in the local version file, and --skip-existing makes them do nothing if it is already installed. The check || install pattern comes from Homebrew's documentation. The ln -sfn lines remove whatever is already at the destination, file or link, and put the symlink in its place, which keeps dotfiles in git as the single source of truth. Back up any real .zshrc before the first run.

Keep secrets out of the repository

Do not commit API keys, certificates or passwords, even to a private repository. Read them from environment variables provided by CI, or fetch them from the macOS keychain with the security command (see man security). In .zshrc, source an untracked file such as ~/.secrets.local if it exists; it lives outside the repository, so git never sees it. A secrets file kept inside the repository must be listed in .gitignore.

Prove it worked with a smoke test

The last step checks results rather than assuming them. A script started by launchd or cron does not read ~/.zshrc, so the smoke test sets up its own path before set -euo pipefail:

#!/bin/bash
eval "$(/opt/homebrew/bin/brew shellenv)"
eval "$(rbenv init - bash)"
eval "$(nodenv init - bash)"
set -euo pipefail
cd "$(dirname "$0")"

xcode-select -p
brew --version
brew bundle check --no-upgrade --file=Brewfile
ruby -v
node -v
bundle exec fastlane --version
xcodebuild -version

The script exits non-zero on the first failure, so CI or a person notices. xcodebuild ships only with full Xcode, not with the Command Line Tools, so the last line fails until Xcode is installed and selected, for example with sudo xcode-select -s /Applications/Xcode.app. Then compare the printed Ruby and Node versions with the version files. On a long-lived build host, also run it on a schedule.

Sources

Share