How to get recursively list an apt package’s dependencies with their installed versions

aptpackage-management

I would like to list the recursive dependencies of a given package, with the currently installed version of each dependency. I'd also like one entry on each line, so it's sortable and diffable.

Basically, given, say, tcpdump, I would like the output to look like:

libtext-wrapi18n-perl: 0.06-7
perl-base: 5.14.2-21+deb7u2

…etc. The exact format of each line doesn't matter so much, just the ability to diff and sort.

The question List (recursive) dependencies of the installed packages in APT is similar, but doesn't give package versions. Using debfoster -d looks promising, but its output does not lend itself to further processing.

Best Answer

Both answers already provided have their pros and cons.

Starting with debfoster gives a list of packages which is simple to parse, so the following gives the requested result:

apt-cache policy $(debfoster -q -d tcpdump|tail -n +2)|awk '/^[^ ]/ { package=$0 } /  Installed/ { print package " " $2 }'

using tail to skip the first line and awk to process the result in a single operation. (Using a command substitution avoids the need to process newlines.) Starting with debfoster means we can only do this with a package which is already installed, so we can then use dpkg to provide more information:

dpkg -l $(debfoster -q -d tcpdump|tail -n +2)

Starting with apt-rdepends gives a list of packages which is a little harder to process, with duplicates; but it has the advantage of being able to process packages which aren't yet installed:

apt-cache policy $(apt-rdepends -p tcpdump 2>| /dev/null|awk '/Depends/ {print $2}'|sort -u)|awk '/^[^ ]/ { package=$0 } /  Installed/ { print package " " $2 }'

This can also be used with dpkg -l:

dpkg -l $(apt-rdepends -p tcpdump 2>| /dev/null|awk '/Depends/ {print $2}'|sort -u)

but this requires that dpkg know about all the packages involved, which may not be the case if the package being processed isn't installed.

debfoster includes Recommends by default; this can be disabled using --option UseRecommends=no:

debfoster -q --option UseRecommends=no -d tcpdump

apt-rdepends doesn't include Recommends by default; this can be enabled using -f Depends,PreDepends,Recommends -s Depends,PreDepends,Recommends:

apt-rdepends -f Depends,PreDepends,Recommends -s Depends,PreDepends,Recommends -p tcpdump

although it doesn't give all the dependencies debfoster finds in that case. (For example debfoster finds that tcpdump depends on apt via libssl1.0.0, debconf and apt-utils, but apt-rdepends doesn't.)