Suppose I have a list of packages:


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:


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.

  • apt-cache depends <packagename> shows the package along with it's dependencies. – Avinash Raj Feb 18 '14 at 18:06
  • How do you make the first list? – DK Bose Feb 18 '14 at 18:08
  • may this help you dmd askubuntu.com/questions/128524/… – rɑːdʒɑ Feb 18 '14 at 18:13
  • The initial list is the result of my predecessor being dumb and installing a bunch of packages we need for a product, but then giving me a list of the diffs between a base ubuntu install and the install after he did those package installs. He no longer knows which were specifically selected. – dmd Feb 18 '14 at 18:14
  • This is super useful for backing up and/or upgrading Ubuntu, especially when you're looking to have a prune of your packages. Thanks! – CJxD Dec 22 '17 at 9:35

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


$ ./find-top-level-packages.py nginx nginx-core zlib1g firefox
$ ./find-top-level-package.py -h
usage: find-top-level-packages.py [-h] [--root-dir dir]
                                  [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 dependencies of packages not part of the
                        initial input
  --no-use-recommends   don't use recommended packages for the dependency
                        list missing recommended packages suffixed with a dash

I dont have enough reputation to add a comment, hence forced to write a separate answer. But this is additional information for the answer provided by 小太郎

His script works awesome. make sure you install the correct version of networkx for it to work correctly.

sudo pip3 install networkx==1.11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.