I'm available for challenging remote roles. Let's get in touch.

How to install and use Homebrew on macOS

Learn how to install Homebrew in four easy steps.

Flavio Silva
Flavio SilvaJune 13, 2023
How to install and use Homebrew on macOS
Image by wavebreakmedia_micro on Freepik

I last tested these instructions on macOS Ventura 13.3.1.

Introduction

In this step-by-step guide, you'll learn how to install Homebrew in four easy steps.

What is Homebrew?

Homebrew is a popular package manager for macOS that simplifies the installation and management of software packages and libraries. It allows users to easily install, update, and uninstall a wide range of command-line tools and applications from a vast repository of packages.

With Homebrew, you can easily install tools like Git, Node.js, Python, Ruby, ffmpeg, and many more.

What are Homebrew formulas?

Homebrew installs packages via formulas. A Homebrew formula is a package definition written in Ruby specifying how to install and manage a particular software package.

These formulas sit on Homebrew's official repositories, but you can opt-in on third-party repos to find less-known tools. Homebrew supports that feature using the tap command, discussed in the Taps (Third-Party Repositories) section.

So without further ado, let's get it installed.

Do not type the $ sign you see in the command examples in this article. That's just an indicator that you should run the command that follows it in your command line tool.

Step 1. Make sure you don't have Homebrew installed already

Before proceeding, make sure you don't have Homebrew installed already by running the following command:


$ brew --version

If you see something like Homebrew 1.6.2, you already have it installed! Then, you can head to the What are some of the most popular commands to use with Homebrew? section to learn more about how to use it.

Otherwise, you should see something like:


zsh: command not found: brew

In that case, let's get it installed.

Step 2. Install Homebrew on macOS

To install Homebrew, you only need to run a single command:


/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

That command will download and run the official Homebrew installation script.

You'll have to type in your user's password.

You should see an output including the following:


"==> Installation successful!"

If it doesn't work, you can look at its website, as they might have changed how to install it after I published this post.

Step 3. Add Homebrew to your PATH

You should also see instructions to run two more commands necessary to add Homebrew to your PATH in the ==> Next steps: section of the output message.

Before going ahead, let's understand why we need to do that.

If you try to run any brew commands now, you'll still get that same error as when you first tried it before installing Homebrew. Try it:


$ brew --version

You should get that same error message:


zsh: command not found: brew

That's because we haven't set our PATH variable yet.

The PATH is an environment variable that specifies a list of directories where macOS looks for executable files or commands when you enter a command in the Terminal (brew in this case).

By running the first command, we'll create a new .zprofile file (if one doesn't exist) and add an eval command to it that adds Homebrew's executable directory to our PATH. That way, macOS can find Homebrew's executable brew command.

That command will also set up the PATH variable so that tools installed with Homebrew will take precedence over tools already installed in the system or installed in other ways.

For example, when we install the Command Line Developer Tools, we get Git installed as part of the bundle. But that installation is not easy to update. So it's advised to install a new Git version with Homebrew, making it very easy to update. But then we ended up with two Git versions installed. Since our PATH is correctly set, macOS will resolve to the Homebrew-installed one when we run git.

So let's go ahead and run the first command:


(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> ~/.zprofile

Now, we have our PATH variable properly set up, but we still get that error message if we try to run any $ brew commands.

Zsh reads the ~/.zprofile file automatically, but only when we open a new Terminal window or tab. If you open a new Terminal window or tab, your $ brew commands will work.

But to make it work in your current Zsh instance, we must run the second command that Homebrew instructions tell us. Let's do that:


eval "$(/opt/homebrew/bin/brew shellenv)"

Step 4. Check the installation

Now run the following command to make sure it worked:


$ brew --version

You should see something like Homebrew 4.0.18.

Awesome! We finally have it working! Congrats!

A bit about the .zprofile file

Zsh automatically reads some files at certain moments. The .zprofile file is one of them, and Zsh only evaluates it when you log in to your Mac user account.

We should use such files to add our custom configurations to Zsh as we did for Homebrew.

You can read more about those files and how to use them in this StackExchange topic.

Now let's see how to use Homebrew with some of its most popular commands.

How to update Homebrew

You can easily update Homebrew itself by running the following command:


$ brew update

How to uninstall Homebrew

You can easily uninstall Homebrew itself by running the following command:


$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

How to install a package using Homebrew

You can easily install any available package by running a command with the following format:


$ brew install <package_name>

A real-world example is:


$ brew install wget

How to update a package installed with Homebrew

You can easily update any package installed with Homebrew by running a command with the following format:


$ brew upgrade <package_name>

A real-world example is:


$ brew upgrade wget

Alternatively, you can upgrade everything outdated at once by running:


$ brew upgrade

How to uninstall a package installed with Homebrew

You can easily uninstall any package installed with Homebrew by running a command with the following format:


$ brew uninstall <package_name> --force

A real-world example is:


$ brew uninstall wget --force

How to find out which packages are outdated

You can easily find out which packages installed with Homebrew are outdated by running the following:


$ brew outdated

That way, you can easily update them.

How to see all packages installed with Homebrew

To see all packages you have installed with Homebrew, run the following command:


$ brew list

How to search for packages

You can easily search for packages using keywords by running a command with the following format:


$ brew search <keyword>

A real-world example is:


$ brew search wget

To see a list with all available packages, just run the following:


$ brew search

How to get info about a package

You can get a piece of short information about any package by running a command with the following format:


$ brew desc <package_name>

A real-world example is:


$ brew desc wget

To get more info, you can run the following:


$ brew info <package_name>

A real-world example is:


$ brew info wget

How to check for problems

Homebrew has a special command to check for issues with your installed packages.

You can run $ brew doctor to check for issues with your installed packages. The command will exit with a non-zero status if it finds any problems.

Taps (Third-Party Repositories)

When you try to install a package with Homebrew, it looks into its official repository to see if it can find the package you're trying to install. But sometimes the package you want is outside the official repo, so you need to add an unofficial repo for Homebrew to look into too. Just be careful, as you might install malware.

The $ brew tap command adds more repositories to the list of formulae that Homebrew tracks, updates, and installs from. By default, tap assumes that the repositories come from GitHub, but the command isn't limited to any location.

Without arguments, the $ brew tap command lists all currently tapped repositories.

To tap into a repo, you run a command with the following format:


$ brew tap <user/repo>

Head to the official Tap page to read more about the tap feature.

How to get some help

You can run $ brew help or $ man brew to get helpful information about Homebrew itself on your Terminal.

More about Homebrew

For more information about Homebrew, visit the project's official website and its GitHub repository.

Conclusion

And that's it for this guide. I hope you enjoyed it!

Thank you for reading, and let me know if you have any issues or suggestions in the comments below.

How to set up your Mac for software development
How to install Git with Homebrew on macOS
How to install and use Antigen and Oh My Zsh on macOS
How to install and run Node.js with nvm on macOS
How to generate and use SSH keys on macOS
How to sign Git commits with SSH keys on macOS
How to install Command Line Developer Tools on macOS

How to install and use Homebrew on macOS by Flavio Silva is licensed under a Creative Commons Attribution 4.0 International License.

Leave a comment using your GitHub account

© 2024 Flavio Silva