Homebrew is a popular package manager for the macOS operating system. It downloads and installs binaries, applications, and utilities using your terminal.
In this article, you will learn how to install and use Homebrew on your macOS machine.
Prerequisites
Homebrew requires the following:
- A 64-bit Intel CPU
- macOS Mojave (10.14) (or higher)
- Command Line Tools (CLT) for Xcode or Xcode (click here to download)
- A Bourne-compatible shell for installation (e.g.
bash
orzsh
)
Installing Homebrew
Before we install Homebrew, you need to install Xcode's command-line tools package. Xcode is not required to use Homebrew, but some other packages will need Xcode's command-line tools.
Execute the following command in your terminal to install Xcode's command-line tools package:
$ xcode-select --install
Follow the instructions on the screen to complete the installation of Xcode's command-line tools.
Now you can install Homebrew with the following command:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
The above command executes the installer script provided by the Homebrew team. Follow the instructions printed on the screen to finish the installation.
Updating Homebrew
To update Homebrew to its latest version, run the following command in your terminal:
$ brew update
Make sure you update Homebrew before installing or updating any software.
Installing Packages
Each package in Homebrew is called a formula. To install new formula, you use the following command:
$ brew install <formula>
For example, to install the Node.js default version with Homebrew, you execute the following command:
$ brew install node
Homebrew also provides an online package browser to browse all the available packages for macOS and Linux.
Upgrading Packages
To upgrade an already installed package (formula), just execute the following command:
$ brew upgrade <formula>
To upgrade all packages, run the upgrade
command without specifying any package name, as shown below:
$ brew upgrade
Downgrading Packages
Downgrading a package is a little tricky with Homebrew.
First of all, you need to use the search
command to find the lower version of the package that you want to downgrade:
$ brew search node
==> Formulae
libbitcoin-node node-build node@12 nodebrew
llnode node-sass node@14 nodeenv
node node@10 node_exporter nodenv
==> Casks
nodebox nodeclipse
Let us say you want to downgrade Node.js from version 14 to 12. Run the following command:
$ brew install node@12
Next, you need to unlink the currently installed Node.js version:
$ brew unlink node
Finally, you need to link the newly installed version of Node.js:
$ brew link node@12 --force
That's all you need to do. To verify that Node.js downgraded version is linked successfully, use the following command:
$ node -v
Removing Packages
You use the uninstall
command to completely remove a package:
$ brew uninstall <formula>
For example, to remove Node.js (don't do it unless required), execute the following command:
$ brew uninstall node
To clean up all the outdated downloads from your computer, use the following command:
$ brew cleanup
Installing Desktop Applications
Homebrew is not just limited to command-line tools. You can use Homebrew Cask to download and install desktop applications like Google Chrome, VS Code, Atom, and more.
Cask is already included in Homebrew, so you don't need to install anything.
To install a desktop application (cask), you use the brew cask install
command:
# install Visual Studio Code
$ brew cask install visual-studio-code
To uninstall a cask, execute the following command:
$ brew cask uninstall visual-studio-code
Brew Tap
Homebrew, by default, contains two lists of formulae: homebrew/core
and homebrew/cask
. The homebrew/core
includes a list of command-line packages that you can install. All the GUI applications are included in homebrew/cask
.
Homebrew also lets you install 3rd-party software. To do so, you first need to add the 3rd-party formulae to Homebrew and then use brew install
to install the formula.
For example, to install MongoDB with Homebrew, you need to tap into the mongodb/brew
formula:
$ brew tap mongodb/brew
Then you can install MongoDB like this:
$ brew install mongodb-community
To remove an already tapped repository, use the untap
command:
$ brew untap mongodb/brew
To view all tapped repositories, you use the following:
$ brew tap
Uninstalling Homebrew
If you no longer need Homebrew, remove it with the following command:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Conclusion
That's all! In this quick article, we learned how to install and use Homebrew on a macOS machine.
Homebrew is a package manager for macOS and Linux. It lets you download and install command line tools, desktop applications, and utilities.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.