GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS

Written by thoughtbot

brew leaves

No, it’s not about tea. We’re continuing our rundown of lesser-known Homebrew features with brew leaves. Let’s check the man brew page:

leaves   Show installed formulae that are not dependencies of another installed formula.

Or, in more computer science-y terms, it shows you the leaves of the Homebrew dependency graph.

When to use it

brew leaves shows you programs that you can safely uninstall. If you want to clean house, just run brew leaves and happily uninstall:

$ brew leaves | wc -l
45
$ brew leaves
...
leiningen
...
pngcrush
...

We have 45 leaves. We haven’t used leiningen in a while, and forgot pngcrush was even installed. Let’s uninstall:

$ brew uninstall pngcrush leiningen
$ brew leaves | wc -l
43

We now have 2 fewer leaves. If pngcrush or leiningen were the only things that depended on a third package foo, then uninstalling those two packages would make foo a new leaf, since now nothing depends on foo.

Easily create a Brewfile

Brewfiles are an easy way to install frequently-used Homebrew packages on a new machine. We can easily create a Brewfile using brew leaves:

$ brew leaves | sed 's/^/install /' > Brewfile
$ wc -l Brewfile
42
$ head -3 Brewfile
install aspell
install bison
install colordiff

Now all 42 packages we depend on are neatly listed. One possible concern is that a package will be left out - for example, we use rbenv but it’s not in the Brewfile. This is because we also have rbenv-gem-rehash installed, which depends on rbenv, making rbenv not a leaf. Since rbenv-gem-rehash depends on rbenv, installing it will also install rbenv. We’re safe.

What’s next?

You can learn how to start and stop background services in Homebrew. You can also take a deep dive into graph theory.