DEV Community

Cover image for Another cheat sheet for Homebrew (2022)
jmau111
jmau111

Posted on • Updated on • Originally published at blog.julien-maury.dev

Another cheat sheet for Homebrew (2022)

Essential concepts

What's Homebrew?

Homebrew is a powerful package manager. It can ease your dev significantly, so do no hesitate to try it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Once it's done, run brew and you should get the help menu to learn the various built-in commands of Homebrew. If so, you can now use a sets of command lines to manage packages on your machine.

Is Homebrew a Mac-only software?

Not at all, even if there are special configs and extensions for macOS.

Homebrew is available for Linux too. Windows users can use it but only with WSL (the Subsystem for Linux).

Homebrew uses symlinks

Behind the scene, Homebrew makes symlinks, which allows using third-party commands from packages in the terminal easily.

What are Homebrew Formulae?

A formula is a Ruby script that "provides instructions and metadata for Homebrew to install a piece of software".

Technically, Homebrew formulae are subclasses of Formula. Homebrew uses them for package definitions.

What is "cask"?

You may have seen commands like brew install --cask tor-browser. Homebrew uses cask to deal with various licences and interact with graphical applications.

It's particularly helpful to install apps from the terminal without having to drag and drop them into the /Applications/ directory.

Technically, cask is an extension of Homebrew but you don't have to install it manually, it's already packed with Homebrew.

What is "tap"?

Taps are useful to install formulae from alternative sources such as GitHub. When you use brew tap <FORMULA>, Homebrew considers it comes from GitHub by default but you can specify other locations:

brew tap [options] [user/repo] [URL]
Enter fullscreen mode Exit fullscreen mode

What are bundles?

Bundles handle non-Ruby dependencies from Homebrew, Homebrew Cask, Mac App Store and Whalebrew. You can quickly list all bundles with:

brew bundle list
Enter fullscreen mode Exit fullscreen mode

or:

less ~/Brewfile
Enter fullscreen mode Exit fullscreen mode

Technically, bundle is an extension of Homebrew but you don't have to install it manually, it's already packed with Homebrew.

It's a pretty elegant way to install softwares in macOS for engineers.

Why does homebrew force updates?

It's not uncommon Homebrew updates some packages while you did not run brew update or brew upgrade. For example, when you install a new package, Homebrew analyzes libraries to add/remove required/outdated dependencies and keep things consistent.

Basic commands

Display all available commands

brew commands
Enter fullscreen mode Exit fullscreen mode

Pretty cool to learn the tool!

Install a formula

brew install <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Uninstall a formula

brew uninstall <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Display information for a specific formula

brew info <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Display the homepage of a given formula

brew home <FORMULA>
Enter fullscreen mode Exit fullscreen mode

The homepage URL is the one specified in the package's metadata, for example:

class Git < Formula
  desc "Distributed revision control system"
  homepage "https://git-scm.com"
Enter fullscreen mode Exit fullscreen mode

Search packages

brew search <KEYWORD>
Enter fullscreen mode Exit fullscreen mode

Pretty self-explanatory but you can also use regex and interesting options such as --archlinux to restrict your search to a given database.

List all formulae including casks

brew list
Enter fullscreen mode Exit fullscreen mode

You can use brew list --casks to list casks only.

List available updates

brew outdated
Enter fullscreen mode Exit fullscreen mode

Update vs. upgrade

brew update updates everything, including Homebrew itself, whereas brew upgrade only updates installed formulae.

Note that you can also upgrade only a specific formula with brew upgrade <FORMULA>.

Cleaning stuff

You can clean old downloads with the following command:

brew cleanup 
Enter fullscreen mode Exit fullscreen mode

Remove stale lock files and outdated downloads for all formulae and casks, and remove old versions of installed formulae.

It will remove all downloads more than 120 days old by default.

Most of the time, the command is safe to run, but you can use the --dry-run option if you're not sure to visualize what will be removed instead of running it Yolo.

You can also clean for a specific formula only:

brew cleanup <FORMULA>
Enter fullscreen mode Exit fullscreen mode

A bit more advanced usages

Pin / unpin

pin is used to prevent Homebrew from upgrading a package automatically when running a general update.

brew pin <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Use unpin to remove this exception:

brew unpin <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Link, unlink, or relink

brew link <FORMULA> allows you to customize symlinks. Use it only when necessary. To delete an existing symlink created with Homebrew, run brew unlink <FORMULA>.

You can also use brew unlink <FORMULA> && brew link <FORMULA> to relink a formula.

List all taps

brew tap
Enter fullscreen mode Exit fullscreen mode

Remove taps

brew untap --force <TAP>
Enter fullscreen mode Exit fullscreen mode

Locate the cache directory

brew --cache
Enter fullscreen mode Exit fullscreen mode

Force cache delete manually

rm -rf "$(brew --cache)"
Enter fullscreen mode Exit fullscreen mode

Troubleshooting: Config vs. doctor

brew doctor runs a quick diagnosis to spot errors whereas brew config displays Homebrew and system configuration info useful for debugging.

Expert mode 🥷🏻

Install Bundles

brew bundle install
Enter fullscreen mode Exit fullscreen mode

Export the list of all brew entries

brew bundle dump
Enter fullscreen mode Exit fullscreen mode

The above command generates a Brewfile you can reuse on other machines. By default, the file is located at the root of the $USER folder but you can specify a custom directory with the --file option.

mas

mas allows installing Mac apps from the App Store directly from the terminal:

brew install mas
Enter fullscreen mode Exit fullscreen mode

All specific mas commands start with mas. All apps installed with mas are listed in the Brewfile.

Print a formula

brew cat <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Edit a formula

brew edit <FORMULA>
Enter fullscreen mode Exit fullscreen mode

Enhance display for brew info <FORMULA>

brew info --json=v1 <FORMULA> | jq .
Enter fullscreen mode Exit fullscreen mode

Create custom formulae

You can cook your own formulae:

class MyFormula < Formula
  desc "My Formula"
  homepage "https://mywebsite.com"
  url "https://example.com/foo-0.1.tar.gz"
  sha256 "85cc828a96735bdafcf29eb6291ca91bac846579bcef7308536e0c875d6c81d7"
  license "MIT"

  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install"
  end

  test do
    system "false"
  end
end
Enter fullscreen mode Exit fullscreen mode

Discussion (0)