Ubuntu – How to find top-level dependencies in a list of deb packages

aptdebdependenciespackage-management

Suppose I have a list of packages:

foo
libfoo
bar
baz
quux

And suppose foo requires libfoo and bar, and baz requires quux.

I want a way to put in that list, and get out this list:

foo
baz

I.e. – I want the minimal list of packages to install which would – after dependency resolution – cause the entire list of packages to be installed.

Best Answer

I created a script that does exactly what you want: https://gist.github.com/kotarou3/2b311fb7b79ae6b682246b32acf0b7e9

python3-apt and python3-networkx are the dependencies. It uses the apt package cache as the source of package information.

Package names themselves are output one per line to stdout, while any comments about them (such as if they are part of a cycle) are to stderr

Example:

$ ./find-top-level-packages.py nginx nginx-core zlib1g firefox
firefox
nginx
$ ./find-top-level-package.py -h
usage: find-top-level-packages.py [-h] [--root-dir dir]
                                  [--follow-unspecified-packages]
                                  [--no-use-recommends]
                                  [--show-missing-recommends]
                                  [package [package ...]]

Find top-level packages of the dependency graph

positional arguments:
  package               package names to use (default: all installed packages)

optional arguments:
  -h, --help            show this help message and exit
  --root-dir dir        act as if chrooted in the specified directory
  --follow-unspecified-packages
                        follow dependencies of packages not part of the
                        initial input
  --no-use-recommends   don't use recommended packages for the dependency
                        graph
  --show-missing-recommends
                        list missing recommended packages suffixed with a dash