Apple Technology

How to Install Homebrew on Mac

Homebrew is a free package management system used to install, remove, and update the packages on macOS using terminal commands. Homebrew simplifies the installation of packages on macOS and assists the developers to streamline their work. This tutorial is a guide on how to install and use Homebrew on a Mac. Since Homebrew is a terminal-based package manager you must know the function of the terminal on Mac.

Terminal, also known as command line interface is an app that allows you to interact with the system in a non-graphical way. Terminal can be intimidating for anyone who is new to it but still it has a lot of potential as it lets you automate tasks you perform on your system.

Homebrew commands make it easy to install open-source packages and developer tools on Macs as Macs do not come with the package manager. Let’s find out how to install Homebrew on a Mac:

Prerequisites

The following prerequisites are needed to install Homebrew on a Mac:

  • A system with macOS (Mojave and later)
  • User with administrative privileges

How to install Homebrew on Mac

Installing Homebrew on a Mac is a 2 steps process:

  1. Installing Xcode Command Line Tools
  2. Installing Homebrew

Homebrew functionality depends upon Xcode Command Line tools, it needs to be installed before installing Homebrew. Let’s find out how to install Xcode Command Line Tools Mac.

1: How to install Xcode Command Line Tools on Mac

To install the Homebrew on a Mac you need to have Xcode Command Line Tools installed on your system as Homebrew needs these tools to function.

Xcode is an IDE from Apple to develop iOS, iPadOS and macOS based applications, if it is already installed on your system then you don’t need to install Xcode Command Line Tools. But if it is not installed then you do not need to install the entire Xcode IDE as it would take approximately 10GB of storage, instead install the Xcode Command Line Tools.

First open terminal on your Mac by pressing Command + Space Bar keys, search terminal and then hit Enter to open it:

Type the following command to check if the Xcode Command Line Tools are installed or not:

xcode-select -p

The above command prints the active developer directory, if you get an error, then that means the Xcode Command Line Tools are not installed:

To install Xcode Command Line Tools use the command given below:

xcode-select --install

A dialog box will appear click Install:

Agree the License Agreement:

The downloading will begin:

Click Done when the downloading is finished:

Verify the installation of Xcode Command Line Tools using:

xcode-select -v

To check developer path use:

xcode-select -p

The Xcode Command Lines Tools have been installed on your system and now move towards the next step.

How to Install Homebrew on Mac

There are two main types of Macs are available:

  1. Intel-based Mac (Intel i5, i7)
  2. Apple Silicon based Mac (M1/M2)

The process of Homebrew installation on either type of machines is similar but Macs based on Apple Silicon require an additional step which will be discussed in the following section.

i: How to Install Homebrew on an Intel-based Mac

Execute the following command in the macOS terminal to begin Homebrew package manager installation:

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

The above script will install the Homebrew in the /usr/local/ directory.

ii: How to Install Homebrew on an M1 Mac

The above step will install Homebrew in /opt/homebrew/ directory if the Mac is Apple Silicon based (M1/M2).

On M1 or M2 Macs there is an additional step to follow to install Homebrew. By default, the Homebrew is downloaded in the /opt/homebrew directory. This directory is not part of the environment variable ($PATH).

To make this directory a part of environment variable execute the following command on Apple Silicon based Macs:

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

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

Alternatively, open the zprofile file in the nano editor using command:

sudo nano ~/.zprofile

Now, add the following line in the file and save it:

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

Note: The Homebrew installation guide in this article is for macOS 11.X Big Sur. Homebrew is not officially supported on the older versions of macOS, but it may work on macOS Mojave and Catalina.

To verify the Homebrew installation, use the command given below:

brew --version

To update the Homebrew to the latest version, use:

brew update

For help related to Homebrew, execute:

brew --help

To read the Homebrew man page use:

man brew

How to Use Homebrew on Mac

Homebrew comes with tons of commands to manage packages, some commonly used commands are discussed in the following section:

Installing Package

To install a formula or package, use the following syntax:

brew install <package_name>

Note: For Homebrew packages formula term is used; it provides instructions for Homebrew to install packages.

For example, to install the wget utility replace the <package_name> with the wget in the above command:

brew install wget

Installing Casks

You can also install GUI based apps using Homebrew, for that use the following syntax:

brew install --cask <package_name>

Note: To install the macOS native packages, Homebrew uses cask definition.

For example, to install Firefox browser, use:

brew install --cask firefox

Listing Installed Packages

To check the installed packages, we can list the Homebrew installed packages using:

brew list

Uninstalling Packages

To uninstall the Homebrew package, follow the syntax given-below:

brew uninstall <package_name>

To remove wget package use:

brew uninstall wget

To remove a cask use the following syntax:

brew uninstall --cask <package_name>

To delete Firefox cask replace the <package_name>:

brew uninstall --cask firefox

Checking Package Dependencies

To check the dependencies of a specific package, use the command mentioned below:

brew deps <package_name>

For example, to check the dependencies of wget, execute:

brew deps wget

Updating Packages

To update a specific Homebrew package, use the following syntax:

brew update <package_name>

Listing Outdated Packages

To list the outdated packages, use the following command in the terminal:

brew outdated

Removing Unnecessary Dependencies

To remove unwanted dependencies, use:

brew autoremove --dry-run

Listing Homebrew Errors and Warnings

To list Homebrew errors and issues, use the command given below:

brew doctor

How to Uninstall Homebrew from Mac

If you no longer need a package manager for your Mac running macOS Mojave or later then Homebrew can easily be removed using the below given command:

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

How to Uninstall Xcode Command Line Tools

To uninstall Xcode Command Line Tools run the command given below in the terminal:

sudo rm -rf /Library/Developer/CommandLineTools

Conclusion

macOS does not come with any package manager, most of the software is installed using GUI. Homebrew is a package manager that makes it easy to install packages on macOS through the command line interface. It is quite handy in installing developer tools and even native macOS open-source GUI applications.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.