Homebrew: How to install a package from GitHub

Updated: January 28, 2024 By: Guest Contributor Post a comment

Overview

Homebrew is a popular package manager for macOS and Linux that enables users to install, manage, and uninstall software through the command line. One of the advanced features of Homebrew is the ability to install packages directly from GitHub repositories, which is particularly useful for fetching the latest version of a tool or software that might not yet be available in the main Homebrew repositories. This tutorial will walk you through the process of installing a package from GitHub using Homebrew, assuming that you already have Homebrew installed on your system.

Prerequisites

Before proceeding, ensure you have the following:

  • macOS or Linux based system
  • Internet connection
  • Homebrew installed (Install by running /bin/bash -c "
    $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    in your terminal)

Basic Installation Steps

If you just want to install a known package from GitHub which has already been prepared as a Homebrew formula, you can directly install it using the brew install command along with the full GitHub repository path:

brew install user/repo/formula

For example, to install the ‘hello’ formula from the user ‘octocat’ on GitHub:

brew install octocat/hello/hello

Homebrew will fetch the formula file from the repository and install the package on your system.

Tapping a Custom Repository

For repositories with multiple formulas, or if you plan on installing more than one program from a repository, you’d start by ‘tapping’ into the repository:

brew tap user/repo

For example, to tap into the Homebrew versions of multiple programs from ‘octocat’:

brew tap octocat/homebrew-tools

Once tapped, you can then proceed to install formulas without specifying the full repository path:

brew install formula

Dependencies and Conflicts

Be mindful of dependencies or potential conflicts. Homebrew will normally alert you if additional software is needed or if there are conflicting paths/names with existing software.

Installing from a ‘HEAD’

To install the latest commit from the master branch (the ‘head’ of the repository), use the following syntax:

brew install --HEAD user/repo/formula

This is useful when newer changes have been made that are not yet part of a formal release.

Advanced Usage: Creating Your Own Formula

For more control or when looking to install software that is not available as a pre-made formula, you can create your own Homebrew formula:

  1. Write a formula using Ruby syntax and save it in a file (e.g., mytool.rb).
  2. Host this file in a GitHub repository, making sure the raw URL is accessible.
  3. Install the formula from the raw URL with brew install:
brew install --build-from-source https://raw.githubusercontent.com/user/repo/branch/path/to/formula.rb

This method provides a high level of customization for installing software from GitHub via Homebrew including setting flags, defining options, specifying dependencies, and more.

Post-Installation

After installation, run the command:

brew info formula

This will provide you the information about the installed package such as version, dependencies, and the installation location.

Troubleshooting

If you encounter errors, refer to Homebrew’s logs and documentation. You can check logs by running:

brew gist-logs formula

And for help:

brew help

Conclusion

Understanding how to install packages from GitHub via Homebrew expands your toolset and allows for greater flexibility in managing your development environment. The ways to tap into repositories, deal with dependencies, and even craft your own formulas open up a world of possibilities for macOS and Linux users.