Managing Specific Versions of Packages with Homebrew

Introduction

Homebrew is a popular package manager for macOS and Linux, simplifying software installation. However, situations arise where you need to install specific versions of a formula rather than the latest one. This tutorial explains how to manage different versions of packages using Homebrew.

Understanding Formulae in Homebrew

A formula in Homebrew is essentially a script that tells Homebrew how to install a package. Versions of these formulae are stored in versioned directories, allowing users to switch between them if needed.

Installing Specific Versions

There are multiple methods to install specific versions of packages using Homebrew:

1. Using Taps for Versioning

For many popular software projects, older versions may be available via taps. A tap is a third-party repository containing additional formulae not in the main Homebrew repository.

Steps:

  • Check for available versions by searching with a wildcard:

    brew search postgresql@
    
  • Install a specific version using its unique identifier, e.g., PostgreSQL 9.5:

    brew install [email protected]
    

This method is straightforward when older versions are maintained by the community.

2. Checking and Activating Previously Installed Versions

If you’ve previously installed a specific version but switched to another, it might still be available on your system:

  • Check Installed Versions:

    brew info postgresql
    
  • Switch Between Installed Versions:
    If the required version is present, use:

    brew switch postgresql <version>
    

This command changes symbolic links to point to the desired version. Note that this method requires dependencies of the older version to be available.

3. Manually Searching and Installing Older Versions

For versions not maintained in taps or already installed, you might need to manually search through Homebrew’s repositories:

  • Search Historical Commits:
    cd $(brew --prefix)/Library/Taps/homebrew/homebrew-core
    git log -S'<version>' -- Formula/<formula>.rb
    

This will show commits where the version was added or removed. You can then checkout a specific commit:

  • Checkout and Install:
    git checkout <commit-id> Library/Taps/homebrew/homebrew-core/Formula/<formula>.rb
    brew install <formula>
    

After installation, return to your latest Homebrew state:

git checkout master

4. Writing a Custom Formula

If none of the above methods work, you can create a custom formula for the specific version and manage it manually.

Pinning Versions

To prevent a package from being updated during routine upgrades (brew update and brew upgrade), pinning is useful:

  • Pin a Formula:

    brew pin <formula>
    
  • Unpin to Allow Updates:

    brew unpin <formula>
    

Pinned formulae are stored in /usr/local/Library/PinnedKegs/.

Conclusion

Managing specific versions of software with Homebrew involves using taps, switching between installed versions, or manually searching for older commits. Pinning is a helpful feature to maintain consistency across environments. By understanding these techniques, you can tailor your development environment to your exact needs.

Leave a Reply

Your email address will not be published. Required fields are marked *