Codementor Events

Understanding Homebrew

Published Nov 26, 2019
Understanding Homebrew

Homebrew is a package manager for Mac OS. It lets you download binaries, packages, and applications with a single command.

In this article, I want to explain how to use Homebrew.

Installing Homebrew

You can install Homebrew with this command:

# Installs Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Updating Homebrew

To update homebrew, you use brew update. This updates Homebrew to its latest version. (You always want Homebrew to be at the latest version before downloading anything).

# updates Homebrew
brew update

Installing a package

Each package is called a formula. To install a formula, you use the install command.

brew install <formula>

For example, to install Node with Homebrew, you use this:

brew install node

If you’re feeling bored, here’s a list of everything you can install with Homebrew. You can also find a list of formulae I use in my Dotfiles repository. (Unsure what dotfiles are? Read this).

Upgrade a package

To upgrade one package, you use this:

# Upgrades one package to the latest version
brew upgrade <formula>

To upgrade all packages, you use brew upgrade without specifying any formulas.

# Upgrades all packages to their latest version
brew upgrade

Downgrade a package

Let’s say you have the latest version of Node installed. You want to downgrade to a previous version.

First, you need to use brew search to find the package.

brew search node

You want to find the possible versions to download from the results. In this example, we can install node v8 or v10.

Brew search.

Install the version you want to use. In this case, lets install Node 10.

brew install [email protected]

Next, we need to unlink our current node version.

# Unlinking node
brew unlink node

Then, we need to link the version we installed. In this case, it’s [email protected] To link a formula, you use the link command.

# Linking node
brew link [email protected]

Linking Node 10.

Sometimes, (like in this case), you need to use --force to link the formula.

brew link [email protected] --force

Linking Node 10 with --force.

Now if you run node -v, you should see that Node v10 is installed.

node -v

Checking for Node version.

Notice its Node is now v10.16.3. Unfortunately, with Homebrew, we don’t get to use Semantic Versioning with Homebrew, so you can’t install specific versions. You can only perform major version upgrade/downgrades.

(But this is enough 99% of the time).

To change back to the latest node version, you repeat the unlink and link steps:

brew unlink [email protected]
brew link node

Switches back from Node 10 to Node 12.

Here’s the funny thing about Node + Homebrew.

When you run brewlink node, [Homebrew doesn’t install npm for you](https://github.com/Homebrew/homebrew-core/issues/34475). So if you want to install any Node version, make sure you runinstallorreinstall` instead.

brew reinstall node

I really shouldn’t have used Node as an example 🤦‍♂️… But I still decided to use it because nobody talked about the npm problem I mentioned above.

Deleting a package

To remove a package, you use the uninstall command.

brew uninstall <formula>

Brew cleanup

Once in a while, you might want to run brew cleanup to remove state and outdated downloads from your computer. (Free up space! 😎).

brew cleanup

Homebrew manages a list of formulae in a place called homebrew/core. When you install files, you install from this list.

“Core” here means folks at Homebrew maintain the formulae list.

Other people can let you install their stuff with Homebrew as well. To do this, they need to create a formulae list. Then, they have to publish this list.

brew tap lets you add a third-party formulae list into Homebrew. Once you added the formulae list, you can use brew install to install the formula.

For example, to install MongoDB with Homebrew, you need to tap into mongodb/brew.

# Required to install MongoDB with Homebrew
brew tap mongodb/brew

Then, you install MongoDB like this:

# Installs MongoDB
brew install mongodb-community

Homebrew contains another formula list called homebrew/cask. This list lets you install GUI applications like Google Chrome, Safari, Atom, and others.

To install a cask, you use the brew cask install command.

# Installs Google Chrome
brew cask install google-chrome

That’s it!


Thanks for reading. This article was originally posted on my blog. Sign up for my newsletter if you want more articles to help you become a better frontend developer.

Discover and read more posts from Zell Liew
get started