How to use Homebrew package manager on Linux

18 December 2020
5 min read

Package Managers are essentially software applications that help users to: Search, Download, Install, Remove and Update software applications on their computer operating system. These can be either Command Line tools or a complete Graphical User Interface application. In What is a Linux Package Manager we discussed package managers from a Linux Perspective and in How To Use Package Management In JavaScript Projects learned more about them from a Javascript perspective.

What is Homebrew

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple's macOS operating system and Linux. The name is intended to suggest the idea of building software on the Mac depending on the user's taste.

Homebrew is billed as the missing package manager for Mac OSX. It simplifies the task of installing terminal utilities and graphical applications using the terminal window. It is designed to look and act like a typical *nix package manager.

Homebrew has spawned several sub-projects such as Linuxbrew, a Linux port now officially merged into Homebrew; Homebrew Cask, which builds upon Homebrew and focuses on the installation of GUI applications and "taps" dedicated to specific areas or programming languages and Frameworks like PHP & NodeJS etc.

Why use Linuxbrew

There are a few reasons you might want to opt to use Linuxbrew instead of your system’s standard package manager.

First, there is no need to use sudo to install packages. You don’t even necessarily need root privileges to install Linuxbrew itself.

Packages are installed in either your $HOME directory or a Linuxbrew-specific home directory.

How to install Linuxbrew

In order to install Linuxbrew, you will need to ensure that your system as the following dependencies are installed:

To ensure all these packages are installed use

sudo apt-get install build-essential curl file git

Once all dependencies have been installed, we can now install Linuxbrew.

Linuxbrew is not actually available in any Distro package managers and therefore we will need to make use of bash and curl to install the application. It is well worth taking the time to visit the Linuxbrew GitHub repository to check whether the instructions have changed.

At the time of writing the instructions were as follows:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

Once the command has completed then we need to add the following to our ~/.bashrc which can be done using any text editor. Personally I tend to make use of nano , so typically will use nano ~/.bashrc to open the file for editing and paste the code below, at the bottom of the file.

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

Save and exit the file, then we can simply refresh the terminal window using either . ~/.bashrc or closing and open a terminal window

Using Homebrew

Homebrew makes it really easy to install, update and maintain various packages on your system which is really useful for developers.

A common use case for developers is to install various compilers or frameworks. For Node.JS developers they may need to install different versions of the Node to support a number of different projects. For this purpose, Node Version Manager (nvm) helps them to easily install and switch to different versions libraries on there machine.

Homebrew helps developers to easily install nvm to their machines. To install nvm using Homebrew use

brew install nvm

Once complete you will be prompted to edit your ~/.bashrc file to add the following

 export NVM_DIR="$HOME/.nvm"
    [ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && . "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
    [ -s "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" ] && . "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

Save and Exit your ~/.bashrc file and then refresh your current terminal window using . ~/.bashrc and nvm is ready for use. You can check your version of nvm using nvm --version

Search for packages

Homebrew has loads of useful packages. You can view the list of packages available using

brew formulae

This will display a long and overwhelming list of packages that are available brew formulae

The list can also be viewed at Homebrew Formulae

If you would like to search for a package via description you can use

    brew search --desc "manage multiple node"

This will return all the packages that will help to manage multiple node versions. To get more information about a particular package, in our case we want to learn more about nvm we can use.

brew info nvm 

If you want a list of commands that are available for use in Homebrew, you can simply use

brew --help

In addition to the help command-line switch, Linuxbrew also has a detailed instruction manual available. To view it use

man brew

Conclusion

Homebrew is a tool that makes it even easier to install applications on Linux. It is not intended to replace the traditional package managers, but rather works as a complement to them.