Understanding Homebrew
Tags: mac
Published: 2019-11-26 20:07:54

---
title: Understanding Homebrew
description: How to Homebrew to install, upgrade, downgrade and delete packages on a Mac.
canonical_url: https://zellwk.com/blog/homebrew/
tags: mac
cover_image: https://zellwk.com/images/2019/brew/search.png
published: true
---

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:

```bash
# 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).

```bash
# updates Homebrew
brew update
```

## Installing a package

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

```bash
brew install <formula>
```

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

```bash
brew install node
```

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

## Upgrade a package

To upgrade one package, you use this:

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

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

```bash
# 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.

```bash
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.

<figure role="figure"><img src="https://zellwk.com/images/2019/brew/search.png" alt="Brew search."></figure>

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

```bash
brew install node@10
```

Next, we need to unlink our current node version.

```bash
# Unlinking node
brew unlink node
```

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.

```bash
# Linking node
brew link node@10
```

<figure role="figure"><img src="https://zellwk.com/images/2019/brew/link.png" alt="Linking Node 10."></figure>

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

```bash
brew link node@10 --force
```

<figure role="figure"><img src="https://zellwk.com/images/2019/brew/link-force.png" alt="Linking Node 10 with --force."></figure>

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

```bash
node -v
```

<figure role="figure"><img src="https://zellwk.com/images/2019/brew/version.png" alt="Checking for Node version."></figure>

Notice its Node is now v10.16.3. Unfortunately, with Homebrew, we don't get to use [Semantic Versioning][4] 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:

```bash
brew unlink node@10
brew link node
```

<figure role="figure"><img src="https://zellwk.com/images/2019/brew/undo.png" alt="Switches back from Node 10 to Node 12."></figure>

Here's the funny thing about Node + Homebrew.

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

```bash
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. <div class="kaomoji">¯\_(ツ)\_/¯</div>

## Deleting a package

To remove a package, you use the uninstall command.

```bash
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! 😎).

```bash
brew cleanup
```

# 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`.

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

Then, you install MongoDB like this:

```bash
# Installs MongoDB
brew install mongodb-community
```

# 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.

```bash
# Installs Google Chrome
brew cask install google-chrome
```

That's it!

[1]: https://formulae.brew.sh
[2]: https://github.com/zellwk/dotfiles/blob/master/brew.sh "Zell Dotfiles Brew.sh"
[3]: https://zellwk.com/blog/mac-setup-2/ "Setting up my new Mac (Part 2—the migration process)"
[4]: https://zellwk.com/blog/semantic-versioning/ "Semantic Versioning"
[5]: https://github.com/Homebrew/homebrew-core/issues/34475


<hr> 


Thanks for reading. This article was originally posted on [my blog](https://zellwk.com/blog/homebrew). Sign up for my newsletter if you want more articles to help you become a better frontend developer.