Ubuntu – How to list all packages that no package depends on

aptdependencies

First, an introduction. I just found a development server with all kinds of GUI packages installed. I'd like to know why. Therefore, I'd like to know which software has been installed that requires X.

I can answer this by answering two closely related questions:

  • How can I list all installed packages that no other package depends on?
  • How can I list all installed packages that no other package depends on, and that, directly or indirectly, depend on a given package? (E.g., x11-common.)

For the first question, apt-mark showmanual is a useful approximation, but it may not be exactly right.

For the second question, what I'm using now postprocesses the apt-rdepends output to list only results for which no dependencies are listed that are listed as results.

Is this correct? Is there an easier way? I notice the result contains quite a few packages that aren't marked as manually installed.

I need this on Ubuntu 14.04, 16.04, and 18.04.

Best Answer

You can for example list all installed packages with dpkg-query and then pipe the package names to apt-cache to see whether they have any reverse depedencies. Here's a one way to do it:

#!/bin/sh

dpkg-query --show --showformat='${Package}\t${Status}\n' | sed --quiet '
    /installed$/{
        s/^/apt-cache rdepends --installed /
        e
        s/\n/ /g                                                        
        /:$/{                                                               
            s/\s.*// 
            p                                   
        }    
    }               
'

if you want to do the reverse and list packages with at least one reverse dependency, just change /:$/ to /:$/!