// Tutorial //

How To Install and Use Homebrew on Linux

Published on March 8, 2022
Default avatar
By Alex Garnett
Senior DevOps Technical Writer
How To Install and Use Homebrew on Linux

Introduction

Homebrew is a package manager that was originally developed for macOS to let you install free and open-source software using your terminal. Linux systems all make use of their own built-in package managers, such as apt on Debian, Ubuntu, and derivatives, and dnf on Red Hat, Fedora, and Rocky Linux, to install programs and tools from trusted and maintained package repositories.

However, it is not always practical to install all software via apt or dnf. For example, some programming languages prefer to use their own package managers, such as Python’s pip, or Node.js’ npm to install additional scripts or libraries that are localized to your own user account.

More recently, Homebrew has added native support for Linux. While Homebrew was originally created to install Linux tools on macOS, many Homebrew packages are better maintained or more convenient to use than the equivalent packages available in Linux repositories. Also, since Homebrew packages are designed to only provide per-user functionality, Homebrew can be used alongside your system package manager without creating conflicts.

In this tutorial you’ll install and use Homebrew in a Linux environment. You’ll install system tools and configure your shell environment to use Homebrew from the command line interface.

Prerequisites

If you would prefer to follow this tutorial using a terminal in your browser, click the Launch an Interactive Terminal! button to get started. You’ll be able to experiment with and run all the commands directly in your browser.

Step 1 — Installing a Compiler Environment

Before installing Homebrew, you will need a working compiler so that Homebrew can build packages. While most packages are pre-compiled, some package dependencies will need to be built directly on your machine. Most Linux distributions allow you to install a compiler with a single command, but do not provide one by default.

On Ubuntu, you can install a package called build-essential that will provide all the packages needed for a modern, well-supported compiler environment. Install the package with apt:

  1. sudo apt build-essential

On Rocky Linux, CentOS, or other RedHat derivatives, you can install a group of packages called Development Tools to provide the same compiler functionality. Install the packages with dnf:

  1. dnf groups mark install "Development Tools"
  2. dnf groupinstall "Development Tools"

You can verify that a compiler is available by checking for the existence of the make command on your system. In order to do that, use the which command:

  1. which make
Output
/usr/bin/make

Now that you have a working compiler, you can proceed to install Homebrew.

Step 2 — Installing and Setting Up Homebrew

To install Homebrew, you’ll download an installation script and then execute the script.

First, download the script to your local machine:

  1. curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh

The command uses curl to download the Homebrew installation script from Homebrew’s Git repository on GitHub.

Let’s walk through the flags that are associated with the curl command:

  • The -f or --fail flag tells the shell to give no HTML document output on server errors.
  • The -s or --silent flag mutes curl so that it does not show the progress meter, and combined with the -S or --show-error flag it will ensure that curl shows an error message if it fails.
  • The -L or --location flag will tell curl to handle redirects. If the server reports that the requested page has moved to a different location, it’ll automatically execute the request again using the new location.
  • The -o switch specifies a local filename for the file. Rather than displaying the contents to the screen, the -o switch saves the contents into the file you specify.

Before running a script you’ve downloaded from the Internet, you should review its contents so you know what the script will do. Use the less command to review the installation script so you understand what it will do.

  1. less install.sh

Once you’re comfortable with the contents of the script, execute the script with the bash command:

  1. /bin/bash install.sh

The installation script will explain what it will do and will prompt you to confirm that you want to do it. This lets you know exactly what Homebrew is going to do to your system before you let it proceed. It also ensures you have the prerequisites in place before it continues.

You’ll be prompted to enter your password during the process. If you do not have sudo privileges, you can press Ctrl+D instead to bypass this prompt, and Homebrew will be installed with more restrictive permissions. You can review this option in Homebrew’s documentation.

Press the letter y for “yes” whenever you are prompted to confirm the installation.

When complete, Homebrew’s installer output will also include Next steps in order to configure your shell environment for working with Homebrew packages. This configuration ensures that Homebrew’s tools will be used in favor of the tools provided by the system package manager. Copy and paste commands from your output, which will detect the correct configuration paths on your system. The below example is from bash:

Output
==> Next steps: - Run these two commands in your terminal to add Homebrew to your PATH: echo 'eval "$(/home/sammy/.linuxbrew/bin/brew shellenv)"' >> /home/sammy/.profile eval "$(/home/sammy/.linuxbrew/bin/brew shellenv)"

Once you run these two commands, the changes you have made to your shell’s PATH environment variable will take effect. They’ll be set correctly when you log in again in the future, as the configuration file for your shell is run automatically when you open a new session.

Now verify that Homebrew is set up correctly. Run this command:

  1. brew doctor

If no updates are required at this time, you’ll receive the following output:

Output
Your system is ready to brew.

Otherwise, you may get a warning to run another command such as brew update to ensure that your installation of Homebrew is up to date. Follow any on-screen instructions to finish configuring your environment before moving on.

Step 3 — Installing, Upgrading, and Removing Packages

Now that Homebrew is installed, use it to download a package. The tree command lets you see a graphical directory tree and is available via Homebrew.

Install tree with the brew install command:

  1. brew install tree

Homebrew will update its list of packages and then download and install the tree command:

Output
. . . ==> Downloading https://ghcr.io/v2/homebrew/core/tree/manifests/2.0.2 ######################################################################## 100.0% ==> Downloading https://ghcr.io/v2/homebrew/core/tree/blobs/sha256:e1d7569f6930271d694e739e93eb026aac1e8b386 ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:e1d7569f6930271d694e739 ######################################################################## 100.0% ==> Pouring tree--2.0.2.x86_64_linux.bottle.tar.gz 🍺 /home/linuxbrew/.linuxbrew/Cellar/tree/2.0.2: 8 files, 162.4KB ==> Running `brew cleanup tree`...

Homebrew installs files to /home/linuxbrew/.linuxbrew/bin/ by default, so they won’t interfere with future Linux updates. Verify that tree is installed by displaying the command’s location with the which command:

  1. which tree

The output shows that tree is located in /home/linuxbrew/.linuxbrew/bin/:

Output
/home/linuxbrew/.linuxbrew/bin/tree

Run the tree command to see the version:

  1. tree --version

The version prints to the screen, indicating it’s installed:

Output
tree v2.0.2 (c) 1996 - 2022 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro

Occasionally, you’ll want to upgrade an existing package. Use the brew upgrade command, followed by the package name:

  1. brew upgrade tree

You can run brew upgrade with no additional arguments to upgrade all programs and packages Homebrew manages.

When you install a new version, Homebrew keeps the older version around. After a while, you might want to reclaim disk space by removing these older copies. Run brew cleanup to remove all old versions of your Homebrew-managed software.

To remove a package you’re no longer using, use brew uninstall. To uninstall the tree command, run this command:

  1. brew uninstall tree

The output shows that the package was removed:

Output
Uninstalling /home/linuxbrew/.linuxbrew/Cellar/tree/2.0.2... (8 files, 162.4KB)

Step 4 — Uninstalling Homebrew

If you no longer need Homebrew, you can use its uninstall script.

Download the uninstall script with curl:

  1. curl -fsSL -o uninstall.sh https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh

As always, review the contents of the script with the less command to verify the script’s contents:

  1. less uninstall.sh

Once you’ve verified the script, execute the script with the --help flag to see the various options you can use:

  1. bash uninstall.sh --help

The options display on the screen:

Output
Homebrew Uninstaller Usage: uninstall.sh [options] -p, --path=PATH Sets Homebrew prefix. Defaults to /usr/local. --skip-cache-and-logs Skips removal of HOMEBREW_CACHE and HOMEBREW_LOGS. -f, --force Uninstall without prompting. -q, --quiet Suppress all output. -d, --dry-run Simulate uninstall but don't remove anything. -h, --help Display this message.

Use the -d flag to see what the script will do:

  1. bash uninstall.sh -d

The script will list everything it will delete:

Output
Warning: This script would remove: /home/linuxbrew/.linuxbrew/Caskroom/ /home/linuxbrew/.linuxbrew/Cellar/ /home/linuxbrew/.linuxbrew/Homebrew/ /home/linuxbrew/.linuxbrew/Homebrew/.dockerignore /home/linuxbrew/.linuxbrew/Homebrew/.editorconfig . . .

When you’re ready to remove everything, run the script without any flags:

  1. bash uninstall.sh

This removes Homebrew and any programs that you’ve installed with it.

Conclusion

In this tutorial you installed and used Homebrew in a Linux environment. You can now use Homebrew to install command line tools, programming languages, and other utilities that you’ll need for software development.

Homebrew has many packages you can install. Visit the official list to search for your favorite programs.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Senior DevOps Technical Writer

Still looking for an answer?

Was this helpful?
Leave a comment