Automating your macOS setup with Homebrew and Cask

How to quickly prepare your environment in a fresh macOS installation

Daria Sova
Apr 28, 2020 · 4 min read

Need to wipe clean your MacBook or simply got a new one and thinking about all this time it will take you to set it up the way the old one works? You might have an easy way to copy over your dotfiles (maybe be even the same way I described in my previous blog post 😉), but what about all the programs you had installed? What about all the programming languages, packages, and IDEs?

Don’t worry, your setup shouldn’t take forever and you shouldn’t even be thinking about it as much. Luckily it is very easy to write a little script that will just do everything for you! And as long as you keep it updated your setup will take just a few minutes.

Starting a script is just a matter of creating a new shell file, for example, I called mine `os-setup-script.sh` and added a few basic lines to it

#!/usr/bin/env bash
# Setup script for setting up a new macos machine
echo "Starting setup"# install xcode CLI
xcode-select —-install

This indicates that we will use shell for running the script, produces simple test output signalling that the script started executing, and as the first step, installs Xcode CLI.

Homebrew

Homebrew is an opened-source free package management system for macOS. And if you have been using MacBooks it’s likely you used for package installations (like Ruby, MySQL, FZF, etc).

If you want to see which packages you have installed with homebrew to include them in your script you can run the `brew list` command in your terminal.

The first thing we want to do though if you are planning to run it on a new computer is making sure that homebrew is installed, so we will add the following lines to our script:

# Check for Homebrew to be present, install if it's missing
if test ! $(which brew); then
echo "Installing homebrew..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi

This will install homebrew if it’s missing.

Then we want to make sure that we have updated the version of homebrew, in case it was already installed:

# Update homebrew recipes
brew update

Now we can list and install homebrew packages with the following block:

PACKAGES=(
git
tmux
bat
macvim
mysql
fzf
ctags
<...list all the packages you want to install>
readline
)
echo "Installing packages..."
brew install ${PACKAGES[@]}
# any additional steps you want to add here# link readline
brew link --force readline

Homebrew will automatically uninstall old versions of the formulas after 1 month, but if you want to do a manual clean up you can also add this step:

echo "Cleaning up..."
brew cleanup

Specific languages

If you want to install any specific languages and associated packages you can also easily do so. For example, if you want to install some python packages you can add something like this to your script:

echo "Installing Python packages..."
PYTHON_PACKAGES=(
virtualenv
virtualenvwrapper
)
sudo pip install ${PYTHON_PACKAGES[@]}

If you want to install ruby or ruby gems:

# ruby install
ruby-install ruby 2.5.5
echo "Installing Ruby gems"
RUBY_GEMS=(
bundler
byebug
json
middleman
middleman-cli
rake
rspec
)
sudo gem install ${RUBY_GEMS[@]}

Cask

Cask is an extension of the Homebrew package manager that lets you install macOS apps. It comes with homebrew so you do not need to install it, you can simply use brew cask <app_name> out of the box. As with the packages, you can also see a list of installed apps with the `brew cask list` command, and then add those to your script.

echo "Installing cask..."CASKS=(
iterm2
adobe-acrobat-reader
skype
slack
spotify
intellij-idea-ce
visual-studio-code
steam
evernote
1password
macdown
)
echo "Installing cask apps..."
brew cask install ${CASKS[@]}

Some additional configurations

You might want to add other things to your setup, this is just covering the basics. For example, these are some things that I added:

echo "Configuring OS..."# Set fast key repeat rate
defaults write NSGlobalDomain KeyRepeat -int 0
# Require password as soon as screensaver or sleep mode starts
defaults write com.apple.screensaver askForPassword -int 1
defaults write com.apple.screensaver askForPasswordDelay -int 0
# Show filename extensions by default
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
# Enable tap-to-click
defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad Clicking -bool true
defaults -currentHost write NSGlobalDomain com.apple.mouse.tapBehavior -int 1

Once you are happy with everything you need for the brand new setup you can add the final line

echo "Macbook setup completed!"

And that’s it! 🎉 ✅

Now you have your perfect setup! And if you wipe your laptop clean or buy a new one you just run this script with sh <path to file> and your computer will do everything for you!

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store