Automating your macOS setup with Homebrew and Cask

How to quickly prepare your environment in a fresh macOS installation

Daria Sova
Mac O’Clock
4 min readApr 28, 2020

--

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

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:

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:

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

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:

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:

If you want to install ruby or 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.

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:

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

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!

--

--