Lesson 2: Advanced Homebrew Techniques

Tapping external repositories to access additional packages

Homebrew’s default repository contains a wide variety of packages, but sometimes you may need to access additional packages that are not included in the default repository.

This is where external repositories come in. To add an external repository to Homebrew, use the brew tap command followed by the URL of the repository. For example, to add the caskroom/fonts repository for additional fonts, run:

brew tap homebrew/cask-fonts

Once you have added an external repository, you can use the brew search and brew install commands as usual to search for and install packages from the repository.

Creating your own Homebrew package

If you have a piece of software that is not already available as a Homebrew package, you can create your own package using a Homebrew formula.

A formula is a Ruby script that describes how to install and configure a package. Writing a formula can be a bit involved, but it can be very helpful to create a formula for a package you use frequently, or to contribute a formula to the official Homebrew Formula repository. The official Homebrew Formula repository can be found at https://github.com/Homebrew/homebrew-core.

To write your own formula, you can use the Homebrew Formula Cookbook at https://docs.brew.sh/Formula-Cookbook. This provides detailed information on how to write a formula and includes examples of various formulae.

Here’s an example formula for a fictitious package called example:

class Example < Formula
  desc "Example package for Homebrew"
  homepage "https://example.com/"
  url "https://example.com/example-1.0.tar.gz"
  sha256 "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
  def install
    system "./configure", "--prefix=#{prefix}"
    system "make", "install"
  end
end

Tips and tricks for using Homebrew effectively

Here are some additional tips and tricks for using Homebrew effectively:

Use brew doctor to diagnose issues with Homebrew and follow its recommendations to resolve them

The brew doctor command checks for common issues with your Homebrew installation and provides suggestions for fixing them.

Clean up old or unused packages using brew cleanup

The brew cleanup command removes old versions of packages and their dependencies that are no longer needed.

This is necessary for some packages to work properly. For example, if you have installed the python@3.9 package and want to use it as your default Python version, you can link it to the system using:

brew link python@3.9

Conversely, if you want to unlink it and use a different version of Python instead, you can run:

brew unlink python@3.9

Use a Brewfile to manage lists of packages

A Brewfile is a text file that lists the packages you want to install or update using Homebrew. You can generate a Brewfile using the brew bundle dump command, which creates a file listing all the currently installed packages. You can install the packages listed in the file using the brew bundle install command.

Wrap-up

We hope that you have found this Homebrew micro-course helpful and that you are now comfortable using Homebrew to manage packages on your Mac. If you have any feedback, please let us know. Happy brewing!

How did you find this lesson?