156

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.

  • `brew graph`??? I get `Error: Unknown command: graph`. No such command. – iconoclast Feb 13 '19 at 02:58
  • 2
    `brew graph` appears to be a package you can install for this https://github.com/martido/brew-graph, see also https://blog.jpalardy.com/posts/untangling-your-homebrew-dependencies/ – netweb Apr 04 '19 at 12:36

4 Answers4

307

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

Denis Barushev
  • 3,577
  • 1
  • 17
  • 14
  • 2
    Thanks! However this does show `mysql` as if it's not required by anything while it's actually required on my system by `mysql-connector-c++`. Do you happen to know if this is intentional (like if `brew` is keeping which formulae is installed directly not only if it's a depedency or not)? – Haralan Dobrev Apr 01 '14 at 19:36
  • 2
    Oh, this exists! FWIW this can be found in `Library/Homebrew/cmd/leaves.rb` and basically does what my solution does with the exception of handling of optional/recommended dependencies (`deps << dep.name if tab.with?(dep.name)`). @HaralanDobrev This most certainly explains the behaviour with regards to `mysql` on your system and why the output differs from my solution, but you can easily adapt `leaves.rb` to your liking. – Adrian Frühwirth Apr 02 '14 at 09:02
  • 1
    Great find. I use `brew ls --versions $( brew leaves )brew ls --versions $( brew leaves )` to also dump the versions. – Mike D Aug 15 '16 at 18:10
  • 6
    `brew leaves -r` to be exact since the OP asked which one he/she installed manually. – GusDeCooL Nov 02 '21 at 12:40
25
$ 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?

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • Thank you for your script, but I've found the answer from Denis to be better. 1) It's built-in. `brew` used to not have such a command, but now that it does it is better to use the built-in. 2) You script does show handle dupes well. For example if I have installed `php55` which installs `homebrew/dupes/zlib` it will show `zlib` like it's not a dependency which is not true. Cheers! – Haralan Dobrev Apr 01 '14 at 19:34
  • Why not just use `cut` on the output? Here's a one liner: `brew deps --installed | cut -d: -f1` – mattmc3 Jan 22 '19 at 13:46
  • @mattmc3 Because that doesn't do the same thing and doesn't answer the question/yield the ouput which OP asked. – Adrian Frühwirth Jan 22 '19 at 15:32
  • 3
    This should be the accepted answer. I didn't find `brew leaves` to be useful. – Danyal Aytekin Mar 29 '19 at 13:45
12

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.
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
jaklan
  • 121
  • 1
  • 5
8

this shows installed formulas as a tree.

brew deps --installed --tree

only show dependencies one level down

brew deps --1 --installed --tree

only show installed php formula

brew deps --installed --tree php 

opens a website for visualization

brew deps --installed --graph php
ozdemir
  • 379
  • 5
  • 8