Untangling Your Homebrew Dependencies

December 29, 2016

Does this look familiar?

$ brew ls | wc -l
217

Wait, what? When did this happen?!

You can try looking at the brew ls output and try to remember what you installed when. Your next reflex is probably to search “homebrew dependencies” because there must be an easy solution…

The Basics

What are vim’s dependencies?

$ brew deps vim
perl
python
ruby

Hmmm… what depends on perl?

$ brew uses perl --installed     # --installed is important, otherwise ALL packages are considered
vim

How can I see all dependencies?

$ brew deps --installed
apg:
bash-completion:
cairo: fontconfig freetype glib libpng pixman
coreutils:
dirmngr: libassuan libgcrypt libgpg-error libksba pth
dnsmasq:
docker:
dos2unix:
ffmpeg: lame x264 xvid
findutils:
## truncated ##

The Real Question: What Can I Uninstall?

You can uninstall packages no other packages depend on. Thankfully, there’s a command for that:

$ brew leaves
apg
bash-completion
coreutils
dnsmasq
docker
dos2unix
ffmpeg
findutils
fswatch
gawk
## truncated ##

but here’s some bad news… brew leaves is broken!

$ brew leaves | grep perl
perl

but, if you remember from above:

$ brew uses perl --installed
vim
# or
$ brew deps --installed | grep perl
perl:
vim: perl python ruby

brew leaves tells you nobody uses perl, but brew uses confirms that vim uses perl…

Detour: Dependencies and Requirements

Let’s look at vim info:

brew info vim

There are two sections: “dependencies” and “requirements” … so, what’s the difference?

dependency:  a "real" package you depend on  
requirement: an "alias" for one of multiple substitute packages

For example: vim needs perl, but it’s not picky about which perl is installed.
If you brew search perl, you’ll find you have many options; all of which satisfy vim’s “requirement”.

Brew Leaves is Broken

To make a long story short, brew leaves only lists dependencies (but not requirements).

There are a bunch of homemade solutions out there. I went through homebrew’s code and brew deps --installed is the complete source of truth.

$ brew deps --installed
apg:
bash-completion:
cairo: fontconfig freetype glib libpng pixman
coreutils:
dirmngr: libassuan libgcrypt libgpg-error libksba pth
dnsmasq:
docker:
dos2unix:
ffmpeg: lame x264 xvid
findutils:
## truncated ##

A leaf is a package that never shows up on the right side of the colon.

brew-graph

If you feel that the output of brew deps --installed is not friendly, you’re not alone.

brew-graph is a ruby script that uses the output of brew deps --installed and generates a graph, in GraphViz or GraphML format. After you install brew-graph, you can visualize your dependencies:

$ brew install graphviz
$ brew graph --installed | dot -Tpng -ograph.png
$ open graph.png

I think you can get better results by using fdp, instead of dot, to generate the image. fdp is already installed if dot is installed; they are both part of GraphViz. The man page says:

… fdp draws undirected graphs using a ‘‘spring’’ model. It relies on a force-directed approach in the spirit of Fruchterman and Reingold …

I would also recommend the new --highlight-leaves option to color (in gray) packages that can be uninstalled:

$ brew graph --installed --highlight-leaves | fdp -Tpng -ograph.png
$ open graph.png

brew deps as graph w/ highlights

Discuss on Twitter