Tutorial

How To Install and Use Homebrew on Linux

Published on March 9, 2022

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 our products

About the authors
Default avatar

Senior DevOps Technical Writer

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


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!

KFSys
Site Moderator
Site Moderator badge
November 9, 2024

For anyone wondering if they should give it a try

Homebrew on Linux can be a great addition, especially if you’re used to its simplicity on macOS or if you want an easy way to manage dependencies and tools. Here’s an overview of why you might want to use Homebrew on Linux, and a guide on how to install it.

Why Use Homebrew on Linux?

  1. Ease of Use: Homebrew makes it simple to install, manage, and update packages with a single command (brew). If you’re already familiar with Homebrew, this can make the transition to Linux smoother.
  2. Access to Additional Packages: Homebrew has a large repository of packages, and some of them may be newer or slightly easier to install compared to standard Linux package managers.
  3. Dependency Management: Homebrew handles dependencies automatically, often making installation easier than manually managing dependencies.
  4. Non-Root Installation: You can install Homebrew in your home directory without needing root access, which is helpful on systems where you don’t have admin privileges.

Potential Downsides

  1. Additional Overhead: If your Linux distribution already has a robust package manager (e.g., apt on Ubuntu or dnf on Fedora), adding Homebrew may create redundancy.
  2. Disk Space Usage: Homebrew installs packages independently from your system’s package manager, which can lead to additional disk usage.
  3. Potential Conflicts: Rarely, packages from Homebrew might conflict with those installed by the system’s package manager, especially if paths overlap.

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!

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.