Codementor Events

Automate your macOS Setup Upgrade

Published Mar 13, 2017
Automate your macOS Setup Upgrade

My laptop was running a bit slow and there was a macOS upgrade that I've been postponing for quite some time. So, on one rainy Friday afternoon, I decided to get my hands dirty and do a clean install of macOS Sierra. I thought to myself, why not automate it so I don't have to think about it next time?

One of the benefits of working on my team is that my team mates know how to use chunks of the configuration that are relevant to our work and use it for themselves.

Let's have a look at the tools that we will use.

Dotfiles

It's a bunch of configuration files and directories that are in your home directory. For example, vim uses .vimrc or git uses .gitconfig. You want to bring them with you from your old machine.

Some people store them as a git repository but I just chuck them into Dropbox. Make sure that you are not revealing any sensitive files, like your private SSH keys or infrastructure setup.

When you setup the new machine, you create symlinks to the ones that you like to use. It can get pretty fancy or you can keep it simple with ln -s.

Brew

Homebrew is a package manager for macOS. You can create your own bundles to set up whole environment.

Apart from that, there is an extension for installing desktop applications — cask. You can search for the recipes with the following commands:

brew search vim
brew cask search google-chrome

When you find the mix of applications, you can bundle them together into a Brewfile.

cask_args appdir: '/Applications'

tap 'homebrew/bundle'
tap 'caskroom/cask'
tap 'homebrew/services'

brew 'vim'
brew 'tmux'
brew 'git'
brew 'ruby-build'
brew 'rbenv'

cask 'google-chrome'
cask 'vlc'
cask 'dropbox'

I like to keep my Brewfile in my Dropbox folder with the rest of the dotfiles. Now, with the file, you can run brew bundle and it installs everything for you. Easy.

Everything together

We have our configuration with dotfiles and we can install all the necessary applications with brew and cask. How about putting everything into one script where we can run and leave the computer to do its own thing?

I created an install.sh file that I keep with the rest of the files.

#!/bin/bash

xcode-select --install

# dotfiles
ln -s ~/Dropbox/dotfiles/.bash_profile ~/.bash_profile
ln -s ~/Dropbox/dotfiles/.gitconfig ~/.gitconfig
ln -s ~/Dropbox/dotfiles/.tmux.conf ~/.tmux.conf
ln -s ~/Dropbox/dotfiles/.vimrc ~/.vimrc

# brew stuff
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
cd ~/Dropbox/dotfiles
brew bundle
cd ~

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# ruby
rbenv install 2.2.5
rbenv global 2.2.5

gem install bundler

echo "******************** Done ********************"
echo "Don't forget to configure SSH properly with key and config"

Firstly, it installs XCode command line tools — if you need to install the full version, you might need to do that through AppStore. I haven't found any way around that yet.

After installing XCode, the script will take care of the dotfiles and link them to your home directory. And when those are sorted, it will start the brew magic.

There are a few custom commands afterward — installing plugin managers for tmux and vim and installing ruby with rbenv. You can also use terminal to customize macOS behavior or do other things that you would normally do manually.

Conclusion

Viola! A fresh OS install is not a pain anymore. After finishing the installation and encrypting your disk, you can copy your backed to the Dropbox folder into your home directory, run:

. ~/Dropbox/dotfiles/install.sh

and you can go for a surf, come back, and have your computer ready. Well, if you need to compile Qt with Webkit, then you might want to go for two surf sessions at least.


This tutorial was originally posted by the author on his blog. This version has been edited for clarity and may appear different from the original post.

Discover and read more posts from Jakub Chodounský
get started
post commentsBe the first to share your opinion
Show more replies