'Homebrew: List only installed top level formulas

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.



Solution 1:[1]

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

Solution 2:[2]

$ 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?

Solution 3:[3]

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

Solution 4:[4]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Adrian Frühwirth
Solution 3
Solution 4 Koray Tugay