Introduction
Homebrew is a popular package manager for macOS and Linux that simplifies the process of installing, updating, and managing software. While it’s easy to install the latest version of a package using Homebrew, sometimes developers require a specific version due to dependencies or compatibility issues with other software. In this tutorial, you’ll learn various methods to install a specific version of a package using Homebrew.
Understanding Homebrew Versioning
Before proceeding with installation, it’s important to understand how Homebrew handles versions. Typically, Homebrew formulae in the main homebrew/core
repository only provide the latest stable version of software. To install older versions, you’ll often need to work with homebrew/cask
or third-party ‘tap’ repositories that can contain multiple versions of a package.
Basic Installation with Homebrew
Let’s begin by covering how to install the latest version of a package:
$ brew install <package-name>
For example, to install Node.js:
$ brew install node
Searching for Available Versions
Next, if you need a specific version of a package, start by searching for available versions:
$ brew search <package-name>
This command will list both the main and versioned formulae available for installation. For instance:
$ brew search node
Checking Version Details
Once you find the specific version you need, you might want to get more information about that version:
$ brew info <formula>
For example:
$ brew info node@14
Installing a Specific Version
Now, to the main focus: installing a particular version of a package. This is done via the following command:
$ brew install <formula>
Here’s how you install Node.js version 14:
$ brew install node@14
Linking the Installed Version
After the installation, you might need to link the specific version if it isn’t already done:
$ brew link --force --overwrite node@14
Using Older Versions from Taps
If the required version isn’t available in the main repositories, you’ll need to use a tap. A tap is an external repository of formulae. First, you tap the repository:
$ brew tap homebrew/cask-versions
Then you can install the desired version from the tap:
$ brew install <tap/formula>
For instance:
$ brew install homebrew/cask-versions/firefox-esr
More Advanced Homebrew Commands
Formulating Installation Commands
Often, you might find yourself needing to automate installation or scripting the process. Here are the basic blocks:
$ brew tap <tap-name> && brew install <tap/formula>
Locking a Version
Down the road, you may wish to prevent a formula from updating to the latest version during a brew upgrade
. This is done by ‘pinning’ the formula:
$ brew pin <formula>
To unpin later:
$ brew unpin <formula>
For example, to pin the Node.js version you installed:
$ brew pin node@14
Handling Issues with Specific Versions
Dependencies and Post-installation Setup
Some packages might require additional steps or dependencies. Always refer to the documentation that comes with the versioned formula to ensure you’ve met all additional requirements. Ensure you install any dependencies before trying to install the main package.
Using Homebrew Services
For packages that include services (like databases or servers), you can manage them using the brew services
command. For example, to start a specific version of PostgreSQL:
$ brew services start postgresql@10
Cleanup
Finally, you might want to clean up older versions or cache:
$ brew cleanup <formula>
For example:
$ brew cleanup node
Conclusion
In this tutorial, we’ve gone over several methods to install specific versions of packages using Homebrew. We’ve also covered how to link and pin different versions, manage services, and clean up after installations. Homebrew’s flexibility with versioning ensures that you can maintain your development environment just the way you need it, stable and consistent.