mac

Understanding Homebrew

zellwk

Zell Liew 🤗

Posted on November 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)"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Installing a package

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

brew install <formula>
Enter fullscreen mode Exit fullscreen mode

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

brew install node
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

# Upgrades all packages to their latest version
brew upgrade
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 node@10
Enter fullscreen mode Exit fullscreen mode

Next, we need to unlink our current node version.

# Unlinking node
brew unlink node
Enter fullscreen mode Exit fullscreen mode

Then, we need to link the version we installed. In this case, it's Node@10. To link a formula, you use the link command.

# Linking node
brew link node@10
Enter fullscreen mode Exit fullscreen mode

Linking Node 10.

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

brew link node@10 --force
Enter fullscreen mode Exit fullscreen mode

Linking Node 10 with --force.

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

node -v
Enter fullscreen mode Exit fullscreen mode

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 node@10
brew link node
Enter fullscreen mode Exit fullscreen mode

Switches back from Node 10 to Node 12.

Here's the funny thing about Node + Homebrew.

When you run brew `link node, Homebrew doesn't install npm for you. So if you want to install any Node version, make sure you run install or reinstall instead.

brew reinstall node
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Brew tap

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
Enter fullscreen mode Exit fullscreen mode

Then, you install MongoDB like this:

# Installs MongoDB
brew install mongodb-community
Enter fullscreen mode Exit fullscreen mode

Brew Cask

Homebrew contains another formulae 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
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
zellwk
Zell Liew 🤗

Posted on November 26, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related