What is brew?

The brew developers call it "The Missing Package Manager" for macOS on their website. A package manager is a software that helps you organize programs installed on your machine. It manages adding, updating and removing tools in one place, so you can easily see all installed tools at once, or remove them very easily.

brew uses Git and Ruby underneath and allows you to easily extend it with new packages or creating your own collection of packages. But more on that further down, we need to install it first!

How to install it?

The brew website contains an easy to use short CLI command that downloads and install brew on your machine.

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

Terminology

As you might have noticed, the logo and name of the tool, are beverage-inspired. So it's only natural, that other things in the brew universe are also related to drinks.

Formula

Formulas! What do they mean in the context of beverages? Well, they tell you how to make something. And it's the same for brew. Formulas allow you to built (read brew 😉) your packages from its sources. Normally, those are smaller programs that do one thing, but do it well.

Bottle

Don't want to build things from their sources? Maybe there is a Bottle for the tools you want to install. A Bottle contains, just like in real-life, a prebuilt (read premade) tool/beverage ready for consumption.

Cask

Next thing, Casks. Casks are big barrels, containing a large amount of liquid. In the brew ecosphere, that's the name we use for bigger application, so desktop-style applications that you would normally install with the well-known drag-and-drop process. Think of your an IDE, a browser, a messenger application. If there's a cask for it you can install it, and if not, you can easily create one.

Keg

Kegs are used to store your Formulas. Just like you need a place to put your freshly brewed beverage, you also need to store the output of a Formula somewhere. There might be different versions of the same Formula, so every Keg gets its own version.

Keg-Only

Some Formulas might be keg-only, that means they are only available inside the Keg folder. Normally, each installed Keg is available without fully specifying their path. That's because they are linked into a folder that's inside your PATH variable (e.g. /usr/local).

Rack

Now that you might have a bunch of Kegs laying around, you want to arrange and sort them. That's where Racks come in. Each Rack can contain multiple different versions of the same Formula, each stored in its own Keg.

Cellar

And where would you put the Racks, certainly not in your living room! Just like racks of wine, Racks belong in the Cellar. Each Rack is named, so you can easily find the tool and version you (or another software) is looking for.

Caskroom

But wait, where are the Casks? Well, they are in the Caskroom. In there you can find your named and installed Casks. Once you install a Cask, it's ready to go, e.g. via Spotlight or the applications folder.

Tap

So, where do all of the packages live? How does brew know what to download, and more importantly, where does it get updates from? That's where Taps come in. Taps allow you to add repositories containing different Formulas or Casks. You can even define your own Taps, if you want to be completely in charge of which versions are available in brew.

Using brew

So now that we've established the terminology, let's take a look at how to use brew. We'll cover the most important commands, like installing, searching for and uninstalling packages. Also we'll see how brew keeps all the things up-to-date.

Installing a package

The command you'll probably used most often is

brew install [--cask] <FORMULA|CASK>

It can be used to install either Formulae or Casks. You can search for Formulae and Casks on the official website.

Terminal showing installation of the package 'no-more-secrets', that simulates the decryption of secret data, commonly used in movies Terminal showing installation of the package 'no-more-secrets', that simulates the decryption of data, commonly used in movies

Searching for packages

Or you can use the CLI to also search for packages. Simply type

brew search <SEARCH_TERM>

and append your search term. You can even surround your search term in two slashes /git/ to use regular expressions, isn't that awesome?

Terminal showing results for the search term 'no-more-secrets' Terminal showing results for the search term 'no-more-secrets'

Getting more information about a package

If you think you found the correct package, you might want to know a little more about it. Maybe two packages are called similarily, so you want to make sure you're going to install the correct one. That's where

brew info <FORMULA>

comes in as a handy helper.

Terminal showing detailed info of the 'no-more-secrets' package Terminal showing detailed info of the 'no-more-secrets' package

See all currenlty installed packages

And if you want to get a look into what has been installed using brew use

brew list

Terminal showing all installed Formuals and Casks Terminal showing all installed Formuals and Casks

Uninstalling packages

Once you are done with a program you can easily remove it from your machine. After impressing some non-technical friends with no-more-secrets I don't need it anymore.

brew uninstall <FORMULA> [<FORMULA> ...]

supports multiple arguments at once, so you can quickly uninstall all no longer needed packages.

Terminal showing deinstallation of 'no-more-secrets' Terminal showing deinstallation of 'no-more-secrets'

Updating packages

Packages receive updates, so how does brew realize there is a newer version? In the beginning I mentioned that Formulas are stored in Taps somewhere. A Tap is, simply said, a git repository that brew clones locally and updates periodically. Before executing almost every command, brew tries to update its local clones. But, you can also force this, with a simple

brew update

This does not upgrade your packages yet, so no harm in doing this frequently.

Terminal showing brew updating the Formulas stored in the Taps Terminal showing brew updating the Formulas stored in the Taps

Upgrading packages

When you really want to get in on a new version, you just run

brew upgrade [<FORMULA> ...]

with the desired Formula. Just like brew uninstall it supports multiple arguments to quickly upgrade more than one package. And if you don't specify any package at all, brew interprets this as upgrading ALL packages.

Terminal showing brew upgrading packages Terminal showing brew upgrading packages

Cleaning up your Cellar

brew tries to clean up after itself, but sometimes old Kegs keep laying around in your Cellar. To execute the equivalent of a spring-cleaning and remove all the old stuff use the

brew cleanup

command. This removes older versions of Formulas, installers or other data that's not needed anymore. brew will also do this periodically and after 30 days of not doing it, there will be a warning message notifying you.

Terminal showing the output of the brew cleanup command. There were no things to clean up, though Terminal showing the output of the brew cleanup command. There were no things to clean up, though

I can't find that package 😢

brew seems to have almost all possible packages already available. But what if there is no package for your favorite todo app? It's quite simple, you just create your own formula. Remember, a formula is just the projects source code that will be downloaded, compiled and installed by brew.

You start by executing the following command

brew create '<LINK_TO_SOURCE_CODE>'

This creates a Ruby file containing a class that describes your package. I won't go into detail on how to write this, as it is a more advanced topic, and there is already a bunch of good tutorials and documentation covering the topic.

What's a Brewfile?

The last big part of brew are Brewfiles. Brewfiles are simple text files that contain your installed Formulas, Casks and Taps. They allow you to export your current configuration and import it somewhere else. Think of it like a package.json in Node applications, or a Gemfile for Ruby applications. Use

brew bundle dump

to generate a Brewfile in the current directory and

brew bundle [install] [--file <PATH_TO_BREWFILE>]

to install packages from an existing Brewfile.

Conclusion

brew has been a very handy addition to my workflow. I can easily keep my machine up-to-date and always know what software is installed. Additionally, if I need to move to another machine, or setup a similar one, I can simply export a Brewfile and import it on the other one. That way, all your programs are simply "just there" without you going on a multiple hour trip finding all the necessary installers somewhere on the Internet.

References

While researching about brew if found these helpful links: