# How to install and use Homebrew on macOS

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 6, 2020, updated Oct 7, 2022. Topics: DevOps, macOS.
Canonical URL: https://attacomsian.com/blog/install-homebrew-macos

> A detailed guide to learning how to install and use the Homebrew package manager on macOS.

[Homebrew](https://brew.sh) is a popular package manager for the macOS operating system. It downloads and installs binaries, applications, and utilities using your terminal.

In this article, you will learn how to install and use Homebrew on your macOS machine.

## Prerequisites

Homebrew [requires](https://docs.brew.sh/Installation#macos-requirements) the following:

* A 64-bit Intel CPU
* macOS Mojave (10.14) (or higher)
* Command Line Tools (CLT) for Xcode or Xcode ([click here](https://developer.apple.com/downloads) to download)
* A Bourne-compatible shell for installation (e.g. `bash` or `zsh`)

## Installing Homebrew

Before we install Homebrew, you need to install Xcode's command-line tools package. Xcode is not required to use Homebrew, but some other packages will need Xcode's command-line tools.

Execute the following command in your terminal to install Xcode's command-line tools package:

```bash
$ xcode-select --install
```

Follow the instructions on the screen to complete the installation of Xcode's command-line tools.

Now you can install Homebrew with the following command:

```bash
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

The above command executes the installer script provided by the Homebrew team. Follow the instructions printed on the screen to finish the installation.

## Updating Homebrew

To update Homebrew to its latest version, run the following command in your terminal:

```bash
$ brew update
```

Make sure you update Homebrew before installing or updating any software.

## Installing Packages

Each package in Homebrew is called a **formula**. To install new formula, you use the following command:

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

For example, to install the Node.js default version with Homebrew, you execute the following command:

```javascript
$ brew install node
```

Homebrew also provides an [online package browser](https://formulae.brew.sh/) to browse all the available packages for macOS and Linux.

## Upgrading Packages

To upgrade an already installed package (formula), just execute the following command:

```bash
$ brew upgrade <formula>
```

To upgrade all packages, run the `upgrade` command without specifying any package name, as shown below:

```bash
$ brew upgrade
```

## Downgrading Packages

Downgrading a package is a little tricky with Homebrew.

First of all, you need to use the `search` command to find the lower version of the package that you want to downgrade:

``` bash
$ brew search node
==> Formulae
libbitcoin-node     node-build          node@12             nodebrew
llnode              node-sass           node@14             nodeenv
node                node@10             node_exporter       nodenv
==> Casks
nodebox                                  nodeclipse
```

Let us say you want to downgrade Node.js from version 14 to 12. Run the following command:

```bash
$ brew install node@12
```

Next, you need to unlink the currently installed Node.js version:

```bash
$ brew unlink node
```

Finally, you need to link the newly installed version of Node.js:

```bash
$ brew link node@12 --force
```

That's all you need to do. To verify that Node.js downgraded version is linked successfully, use the following command:

```bash
$ node -v
```

## Removing Packages

You use the `uninstall` command to completely remove a package:

```bash
$ brew uninstall <formula>
```

For example, to remove Node.js (don't do it unless required), execute the following command:

```bash
$ brew uninstall node
```

To clean up all the outdated downloads from your computer, use the following command:

```bash
$ brew cleanup
```

## Installing Desktop Applications

Homebrew is not just limited to command-line tools. You can use [Homebrew Cask](https://github.com/Homebrew/homebrew-cask) to download and install desktop applications like Google Chrome, VS Code, Atom, and more. 

Cask is already included in Homebrew, so you don't need to install anything.

To install a desktop application (cask), you use the `brew cask install` command:

```bash
# install Visual Studio Code
$ brew cask install visual-studio-code
```

To uninstall a cask, execute the following command:

```bash
$ brew cask uninstall visual-studio-code
```

## Brew Tap

Homebrew, by default, contains two lists of formulae: `homebrew/core` and `homebrew/cask`. The `homebrew/core` includes a list of command-line packages that you can install. All the GUI applications are included in `homebrew/cask`.

Homebrew also lets you install 3rd-party software. To do so, you first need to add the 3rd-party formulae to Homebrew and then use `brew install` to install the formula.

For example, to install MongoDB with Homebrew, you need to tap into the `mongodb/brew` formula:

```bash
$ brew tap mongodb/brew
```

Then you can install MongoDB like this:

```bash
$ brew install mongodb-community
```

To remove an already tapped repository, use the `untap` command:

```bash
$ brew untap mongodb/brew
```

To view all tapped repositories, you use the following:

```bash
$ brew tap
```

## Uninstalling Homebrew

If you no longer need Homebrew, remove it with the following command:

```bash
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
```

## Conclusion

That's all! In this quick article, we learned how to install and use Homebrew on a macOS machine.

Homebrew is a package manager for macOS and Linux. It lets you download and install command line tools, desktop applications, and utilities.
