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
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.
A Linux server or desktop environment, and a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in our Initial Server Setup with Ubuntu 20.04 guide.
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
- 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 groups mark install "Development Tools"
- 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 make
Now that you have a working compiler, you can proceed to install Homebrew.
To install Homebrew, you’ll download an installation script and then execute the script.
First, download the script to your local machine:
- 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
--failflag tells the shell to give no HTML document output on server errors.
curlso that it does not show the progress meter, and combined with the
--show-errorflag it will ensure that
curlshows an error message if it fails.
--locationflag will tell
curlto 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.
-oswitch specifies a local filename for the file. Rather than displaying the contents to the screen, the
-oswitch 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.
- less install.sh
Once you’re comfortable with the contents of the script, execute the script with the
- /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
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:
- brew doctor
If no updates are required at this time, you’ll receive the following output:
OutputYour 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.
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.
tree with the
brew install command:
- brew install tree
Homebrew will update its list of packages and then download and install the
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 tree
The output shows that
tree is located in
tree command to see the version:
- tree --version
The version prints to the screen, indicating it’s installed:
Outputtree 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:
- 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:
- brew uninstall tree
The output shows that the package was removed:
OutputUninstalling /home/linuxbrew/.linuxbrew/Cellar/tree/2.0.2... (8 files, 162.4KB)
If you no longer need Homebrew, you can use its uninstall script.
Download the uninstall script with
- 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:
- less uninstall.sh
Once you’ve verified the script, execute the script with the
--help flag to see the various options you can use:
- bash uninstall.sh --help
The options display on the screen:
OutputHomebrew 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.
-d flag to see what the script will do:
- bash uninstall.sh -d
The script will list everything it will delete:
OutputWarning: 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:
- bash uninstall.sh
This removes Homebrew and any programs that you’ve installed with it.
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.