Visual Audit Homebrew Programs
Tags: homebrew, macos
Published: 2020-05-10 15:04:42

I use a Mac computer as my primary development machine. My personal one is where I spend a lot of time experimenting, self-learning, trying things out.
One awesome way to install software on macOS is to use [Homebrew](https://brew.sh/), which boils down the process of downloading, compiling, installing software (and any associated dependencies) to a simpler process, often with a single command, e.g. `brew install httpie` to get the [HTTPie](https://httpie.org/) command line client.

However, since it's so easy to install a variety of packages and programs, oftentimes I'll have installed a program to try something out, and never need it again. The total count of packages installed via Homebrew can be seen like this:

```shell
$ brew list | wc -l
325
```

I definitely don't recall installing all of these - and over time that becomes a heavier burden to carry. Each package can use up some disk space, but more importantly, every time a `brew upgrade` is run, it may install new versions of software you don't use, so that takes up more time - which is even more valuable than disk space. If you're running out of disk space, read: {% link clpsplug/seriously-periodically-do-brew-cleanup-36bi %}

But since I want to actually figure out if there's programs I've installed and no longer use or need, I _could_ read the output of `brew list` and `brew uninstall <package name>` for each one, but I'm bound to overlook some packages that are dependencies, and may no longer be needed.

I find it best to read visual "maps" that diagram the relationships between packages, so that if I uninstall a package that as a dependency on another package, and nobody else depends on that package, that I uninstall them both, thereby freeing up more resources.

A nice helper program for Homebrew is this extension called `brew-graph`: 

{% github martido/homebrew-graph %}

Once installed (read the `README`), I can create an image, along with the `graphviz` package's `dot` command:

```shell
brew graph --installed | dot -Tsvg -o homebrew-installed.svg
```

I could replace the `svg` part with `png` to get a static image - I like the [SVG file](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) format, since it opens in Google Chrome and I can run a "Find" function for any names, and could also change some of the variables if I wanted to via text editor, or with an SVG Editor 

The image output is similar to the one in the header of this post - some items are isolated and appear at the top row - these packages have no dependencies, and nothing depends on them - often a tool to do a specific thing. Audit these for tools you use directly, and uninstall those that you don't recall using. You can always reinstall them later!

Now for the more complex ones - like `ffmpeg` for example. This package has a _lot_ of direct dependencies. So if I wanted to create an image that is scoped only to that package, I could run:

```shell
brew graph ffmpeg | dot -Tpng -o homebrew-ffmpeg.png
```
And that looks like:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ikcbkhyp1vajl7a744ey.png)

Which isn't easy to see, but it can be helpful to see the dependencies of a single package.
So if I wanted to uninstall all of the packages safely, I could run each one and if I get a warning that the package is in use, not remove it, or I could use another helpful external command - `rmtree`:

{% github beeftornado/homebrew-rmtree %}

This provides a useful `--dry-run` parameter to observe what might happen if we wanted to remove a package, and see for ourselves what we would be removing. Example:

```shell
$ brew rmtree --dry-run ffmpeg
This is a dry-run, nothing will be deleted
==> Examining installed formulae required by ffmpeg...
 -  61 / 62

Can safely be removed
----------------------
ffmpeg
aom
lame
libbluray
...
```
This goes on for a while, so run it yourself and see what your computer can safely remove!

**Reminder:** As always, take these notes with caution. The risk of installing someone else's software on systems that may lead to undue exposure. This post and other software are things lot of us write, but there's nothing preventing malicious actors from doing nasty things over similar channels, so make sure you have other protections in place - like firewalls, malware/virus scanners, network monitoring/alerting, etc - and if it's open source code, you can read it too, and form your own opinions!

Hopefully this post has provided you with a couple of tools in your toolbox to help keep your computer in good shape, extending its lifespan and utility to keep learning and trying out new things!