122
votes

I'm looking for a way to show only the formulas I installed without the installed dependencies. I want to have a list of all the programs I actually installed, without all noise of the dependencies.

I do know about brew list which lists all installed formulas. I also know that brew graph gives me a dependency graph in the graphviz

Or in other words: I want to have the minimal set of formulas to reinstall my system.

4
brew graph??? I get Error: Unknown command: graph. No such command.iconoclast
brew graph appears to be a package you can install for this github.com/martido/brew-graph, see also blog.jpalardy.com/posts/untangling-your-homebrew-dependenciesnetweb

4 Answers

230
votes

Use brew leaves: show installed formulae that are not dependencies of another installed formula.

23
votes
$ brew deps --installed
tmux: pkg-config libevent
q:
gdbm:
libxml2:
asciidoc: docbook
libevent:
pkg-config:
pcre:
docbook:
zsh: gdbm pcre
readline:
emacs: pkg-config

This seems to give us a list of all installed formulae including their dependencies. We can build a list of all formulae and a list of all dependencies and subtract the dependencies from the list of formulae, this should give us a list of formulae which are not dependencies of other formulae:

$ cat brew-root-formulae.sh
#!/bin/sh

brew deps --installed | \
    awk -F'[: ]+' \
    '{
        packages[$1]++
        for (i = 2; i <= NF; i++)
            dependencies[$i]++
    }
    END {
        for (package in packages)
            if (!(package in dependencies))
                print package
    }'

.

$ ./brew-root-formulae.sh
zsh
asciidoc
libxml2
readline
tmux
q
emacs

Is this the output you are after?

4
votes

this shows installed formulas as a tree.

brew deps --installed --tree

2
votes

The question is quite old, but actually only this answer resolves the issue. However, it's more like a workaround. But there's one more solution available out-of-the-box in brew:

brew bundle dump --file -

From docs:

brew bundle dump:
    Write all installed casks/formulae/images/taps into a Brewfile in the
current directory.

and the flag:

--file 
Read the Brewfile from this location. 
Use --file=- to pipe to stdin/stdout.

As a result we get e.g.:

tap "homebrew/bundle"
tap "homebrew/cask"
tap "homebrew/cask-fonts"
tap "homebrew/core"
tap "homebrew/services"
tap "jesseduffield/lazydocker"
tap "jesseduffield/lazygit"
brew "lazydocker"
brew "lazygit"
cask "font-sauce-code-pro-nerd-font"

If you e.g. need a pure list of formulae and casks, without taps, you can just run:

brew bundle dump --file - | grep '^brew\|^cask' | sed 's/.* "\(.*\)".*$/\1/'

and get:

lazydocker
lazygit
font-sauce-code-pro-nerd-font

P.S. If you actually save the output to the file (with brew bundle dump or brew bundle dump --file PATH_TO_FILE), you can easily install all the dependencies from it with brew bundle install:

brew bundle [install]:
    Install and upgrade (by default) all dependencies from the Brewfile.

You can specify the Brewfile location using --file or by setting the
HOMEBREW_BUNDLE_FILE environment variable.