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

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 install 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar

Senior DevOps Technical Writer

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel