Installing Homebrew on Linux: Debian, Ubuntu, and RedHat

how to install homebrew on Linux

* This page contains promotional content

Homebrew has become pretty much the standard on macOS, but these days you can install Homebrew on Linux too.
On Debian and RedHat you can manage packages with apt or dnf (yum), but Homebrew makes it easier to install and update programs that are not in those packages and are not published as a repository.

For example, tools I use often such as Hugo, Docker’s docker-compose, and lazydocker have to be downloaded from the official site and swapped out every time there is an update, but this can now be simplified to something like brew update hugo, just as on macOS.

Updates also arrive faster than in the Linux package management, so you can often use newer versions of programs.
(Packages on RedHat-based systems in particular are frequently on old versions, so I think the benefit of installing Homebrew is large)

Installing Homebrew

Before installing, install the programs each Linux needs

Debian/Ubuntu

$ sudo apt-get install build-essential procps curl file git

Redhat/CentOS

$ sudo yum groupinstall 'Development Tools'
$ sudo yum install procps-ng curl file git
$ sudo yum install libxcrypt-compat

On CentOS 7 you need to raise the versions of Git and curl, as in the article below

Upgrade git & curl(CentOS7)

  • Raise the version of git
$ sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
$ sudo yum update
$ sudo yum install git236
  • Raise the version of curl
$ sudo curl https://github.com/moparisthebest/static-curl/releases/download/v7.85.0/curl-amd64 -Lo /usr/local/bin/curl
$ sudo chmod +x /usr/local/bin/curl
$ sudo ln -s /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/ssl/certs/ca-certificates.crt

Installing homebrew

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

$ bash install.sh
- Run these three commands in your terminal to add Homebrew to your PATH:
    echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /root/.profile
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
    sudo apt-get install build-essential
  For more information, see:
    https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
    brew install gcc
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

When you run install.sh the way the official documentation describes, the libraries and executables end up inside /home/linuxbrew/.linuxbrew/.

Environment settings

Because I worked as root it ended up as /root/.profile, but if you use it as a normal user, change it to ~/.profile.

    echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /root/.profile
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Checking

$ brew doctor
Your system is ready to brew.

Installing packages

After that, install just like brew on macOS

$ brew install hugo tree lazydocker

Packages installed with brew are comparatively newer than packages installed with apt or yum inside the system, so if you want to run a newer version of a program, brew looks like the better choice.
It is also nice that you can use it without polluting the Linux system.

That completes the installation. The rest is for reference.

Individual installation

If you want to place it under your own environment instead of the /home/linuxbrew directory, this is a way to install it individually without using install.sh.
I used this individual installation for a while, but some programs could not be installed, so it is wiser to use the official installation

$ git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew
$ mkdir ~/.linuxbrew/bin
$ ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin
$ eval $(~/.linuxbrew/bin/brew shellenv)
  • Environment settings Since I worked as root in this article, adjust the /root/.profile file to match your own environment
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile

## When you installed it under your own home directory
$ echo 'eval "$(~/.linuxbrew/bin/brew shellenv)"' >> ~/.profile

$ exec $HSELL -l
$ brew -v
Homebrew 3.6.5
Homebrew/homebrew-core (git revision 534720b84e9; last commit 2022-10-14)

In practice I use my own user name and zshrc

If you install it under your own directory, brew doctor warns that the prefix is different and that builds may fail for some formulae

Warning: Your Homebrew's prefix is not /home/linuxbrew/.linuxbrew.
Some of Homebrew's bottles (binary packages) can only be used with the default
prefix (/home/linuxbrew/.linuxbrew).
You will encounter build failures with some formulae.

Just as the warning said, some programs (curl, rsync, and so on) could not be built and installed.

Trouble

If you have finished the installation and the environment settings but brew itself cannot be used, in almost all cases the PATH is not set or the PATH priority is wrong, so check it again.
Also, if a program installed with brew does not work, reloading the shell should solve it in most cases.

Small tips

  • Updating brew itself
    It is git cloned, so you just run git pull
  • Removing
    If you want to remove homebrew itself, wiping out /home/linuxbrew or $HOME/.linuxbre with rm -rf deletes everything

See also