So, Is Homebrew Magic?
If you’re a macOS power user, or have done any kind of software development on a Mac, chances are you’ve heard of Homebrew. It’s “the missing package manager for macOS” that lets you run a simple command like
brew install youtube-dl
and within seconds you have access to a new application that you can use in your Terminal.
To new users this might seem like some form of computer-magic, but it’s pretty simple to understand once the steps are broken down.
What is a package manager?
First, it’s important to understand what a package manager actually is. You can think of them as open source app stores, housing software packages in “repositories” and providing users with an organized system for searching and installing a myriad of tools. Almost all Unix-like operating systems include some package management tools. CentOS has yum, Ubuntu has apt-get, and with Homebrew macOS can have brew.
So how does Homebrew get the software onto my computer?
As a package manager, Homebrew keeps track of…you guessed it…packages. A package is a compressed file that will typically contain things like information about the software (metadata), a list of dependencies necessary for the software to run properly, and of course the source code for the software itself. In order to run properly, most open source software has to be compiled from source code into an executable file called a binary, and thankfully Homebrew does all that work for us.
When you run:
brew install youtube-dl
Homebrew is actually running through the code listed in the youtube-dl Formula. A Homebrew Formula is a Ruby class that downloads the most up to date version of the software, helps get the source code compiled, and then installs it to your system.
Here’s an example of a typical Homebrew Formula:
One thing to note here is that Homebrew, following the Separation of Concerns (SoC) design principal, doesn’t actually include a compiler. Instead it relies on tools like make and gcc, which become available after installing Apple Xcode Command Line Tools. This way it can just focus on being a really good package manager.
Okay cool…
But I still don’t know where Homebrew puts the software on my Mac…
The final step in Homebrew’s installation process makes sure that you have access to your newly installed software via the Terminal. To do this, Homebrew first installs everything to the default location of:
/usr/local/Cellar/
It then creates symlinks to each package in:
/usr/local/opt/ and /usr/local/bin
If you’ve been successfully using HomeBrew you can run:
echo $PATH
in your Terminal and you should see that /usr/local/bin or /usr/local/opt/ are in your PATH environment variable. The PATH is basically a roadmap that tells the shell where to find the software you are calling.
Try running this command to see the list of Homebrew apps you have installed, and where they are symlinked from.
ls -l /usr/local/opt
Further Reading:
To learn more about Homebrew and other package managers, check out some of these links:
https://opensource.com/article/18/7/evolution-package-managers
https://www.digitalocean.com/community/tutorials/package-management-basics-apt-yum-dnf-pkg
I have been using Homebrew for a few years, but the understanding that Homebrew Formulae are simple Ruby classes just clicked for me after my recent studies at Flatiron School. I hope this post has encouraged you to learn more about Homebrew as well, or has at least given you some approximate knowledge of it.