If you plan to install packages on a macOS, the process can be challenging if you’re unfamiliar with the command line interface. The good news is that a package manager can help seamlessly install packages on your computer. So why not install Homebrew? Homebrew lets you painlessly install the stuff you need on your machine.
In this tutorial, you’ll learn to install and use Homebrew on macOS to install packages without hassle.
Read on to take your management skills to the next level!
Table of Contents
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have a macOS computer handy. This tutorial uses Mac M1.
Installing Xcode Command Line Tools
Before installing Homebrew, you must have the Xcode Command Line Tools installed on your computer. The Xcode Command Line Tools are a suite of software tools that allow you to interact with your computer using the command line environment.
Other tools, like PuTTY or Telnet, also need the Xcode Command Line Tools to be installed to function correctly.
In most cases, the Xcode Command Line Tools are installed automatically when you install Xcode. But if you’ve never installed Xcode on your computer before, or if you’ve uninstalled it, you may need to install the Xcode Command Line Tools manually.
To install the Homebrew CLI:
1. Open your terminal, and run the below xcode-select command in the terminal to check if the Xcode Command Line Tools is already installed.
This command prints the path of the active developer directory.
You will see the following output if the Xcode Command Line Tools are installed on your macOS. This output tells you the Xcode Command Line Tools are installed in the /Library/Developer/CommandLineTools directory.
2. Next, run the echo $? command to check the exit status of the xcode-select -p command you ran in step one.
The output below indicates the xcode-select -p command was successful, and the Xcode Command Line Tools are installed on your macOS machine.
If the command was successful, the exit status is 0, so you can skip to the “Installing Homebrew” section. But if you get an exit status of 1, you must install the Xcode Command Line Tools.
3. Run the below command to –install the Xcode Command Line Tools. This command installs the necessary Xcode Command Line Tools package and all its dependencies.
You can install the Xcode Command Line Tools from the App Store, but the terminal method is recommended. Why? The terminal method is more efficient and lightweight since you don’t need to install the entire Xcode IDE — a hefty 13GB download.
4. Now, click Install on the pop-up dialog box to confirm installing the Xcode Command Line Tools.
5. Click Agree to accept the terms and conditions of the software license agreement and start the installation, which may take a few minutes to complete.
6. Finally, run the below commands to verify that the Xcode Command Line Tools are installed on your computer.
ggc -v make -v xcode-select -p
Installing Homebrew on macOS
Now that you have the Xcode Command Line Tools installed, it’s time to install Homebrew on your macOS. Some Homebrew components dependent on the Xcode Command Line Tools package should work correctly at this point.
1. Run the following command to install Homebrew on your macOS.
This command downloads the Homebrew installation script (install.sh) from the Homebrew GitHub repository and runs it on your computer.
-coption tells the terminal to run the command that follows it.
- The -fsSL flag tells the curl command to fetch the URL with no error, follow any redirects, and show the progress bar during the download.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Enter your user password when prompted, and press Enter.
3. Now, press Enter when prompted to proceed with the installation, as shown below.
You’ll see the following output once the installation completes.
Configuring Homebrew’s PATH Environment Variables
Homebrew is now installed on your computer but is not configured correctly. When you run a command in the terminal, the terminal looks for that command in the directories specified by the PATH environment variable.
The PATH environment variables tell the terminal where to look for executables. So before you can use Homebrew, you need to configure it with the correct PATH environment variables. If you don’t, you’ll need to type the full path of the executable every time you run it.
These steps apply to the new macOS running Apple Silicon processors. You can skip this section if you’re using an older macOS running Intel processors.
1. Run the which brew command below to print out the full path of the brew executable.
If you are a long-time user of Homebrew, you can see that the path of the brew executable has changed from /usr/local/bin/brew to /opt/homebrew/bin/brew.
Apple has changed the default installation directory because:
- The /opt directory is intended for the installation of third-party software.
- Too much software in the /usr/local directory can cause conflict.
- There are security threats if the /usr/local directory is writable by non-privileged users.
2. Next, open the ~/.zshrc file in your preferred text editor. This file stores the PATH environment variable for the zsh shell.
If you’re using a different shell, open the corresponding file instead.
3. Add the following line at the end of the .zshrc file, save the changes and close the file.
This line creates Homebrew’s correct PATH environment variable whenever you open a new terminal session.
eval "$(/opt/homebrew/bin/brew shellenv)"
4. Now, run the source command below, which doesn’t provide output but applies the changes you made to the ~/.zshrc file.
5. After sourcing, run the below brew command to check if the PATH environment variable (–env) is set correctly.
As you can see below, the PATH environment variable is correctly set and includes the path of the brew executable.
6. Lastly, run the following brew doctor command to check if your system is ready for brewing.
After every significant change to your system (such as after installing or updating Homebrew), running this command is an excellent way to check for potential problems.
Installing Packages Using Homebrew
After installing and configuring Homebrew, it’s time to explore how to manage packages with Homebrew. The most essential task you can perform with Homebrew is installing a package.
Below is the syntax for installing packages with Homebrew.
brew install <package_name>
Suppose you are looking for a utility to download files from the web. One such utility is wget, which you can install with Homebrew.
1. Run the following commands to update the Homebrew package repository to include the latest package versions, and verify your machine is ready for brewing.
# Updates package repository brew update # Verify your machine is ready for brewing brew doctor
2. Next, run the brew install command below to install wget on your system.
brew install wget
3. Now run the wget command below, passing a URL to wget to download the corresponding file. This command lets you test if Homebrew successfully installed wget on your system.
Upgrading Existing Packages
Developers regularly release new software package versions, and Homebrew lets you upgrade all installed packages on your system. Upgrading Homebrew and the installed packages at least once a week is recommended.
Run the below command to check for
outdated packages first before performing updates.
The output below indicates there are no outdated packages on the system.
If you get the same one below, upgrading is unnecessary. But if you get a list of outdated packages, jump to the following step.
Now, run the
brew upgrade command followed by the package name to upgrade (
brew upgrade wget
Perhaps you’d like to go ballistic and upgrade all outdated packages. If so, run the brew update command instead. Note that this command takes no formula names or arguments.
Removing Unnecessary Packages
Suppose you no longer need some packages on your system. In that case, you can remove them with Homebrew.
Below is the syntax for removing packages from your system.
brew remove <package_name>
1. Run the brew remove command below to remove the wget package.
brew remove wget
2. Next, run the following brew deps command to check all package dependencies (wget).
Other packages on your system may use a package’s dependencies. In such cases, you can’t delete those dependencies. Homebrew will give you an error if you try to delete them.
brew deps wget --tree --installed
3. Now, run the below command to –ignore-dependencies and remove the package.
Take caution if you choose to run this command. Doing so can potentially break your system. Uninstalling a package along with its dependencies is always a better option.
brew remove --ignore-dependencies wget
Managing packages in your system shouldn’t be a complex task. And in this tutorial, you’ve learned how to install Homebrew on macOS for quick package management. You never have to worry about missing out on the newest features of your packages. Homebrew lets you update existing packages and remove the ones you no longer need.
At this point, you should have a sound understanding of how to install and manage packages using Homebrew. With this newfound knowledge, why not create a custom Homebrew Formulae to install your favorite developer tools on a new Mac?