Debian: Finding Package Dependencies of a Binary


There are times when I build an executable from its source to be deployed on a different machine. That different machine is a Debian system where I need to have the possible minimum of installed packages.

To simplify achievement of that goal, I use the following script that takes the list of shared objects that the executable in question depends on and searches the Debian database for packages that provide these shared objects.

This script outputs three tab-separated columns: the name of a shared object, the path to the shared object provided by a package, and the name of the package.

Beware that installing all packages reported by this script without any prior examination or processing of the output of the script may not produce the expected result, because:

If you are wondering why would I need such a script, please take into account that there are binaries with many dependencies, for instance, on one of my systems, Emacs depends on 92 shared objects. In cases like that, it is very nice to have much of the work done for you by a machine, even considering the limitations mentioned above.

Please note that you may need to change the value of the variable ARCH at the beginning of the script.

ARCH=amd64
URL="https://packages.debian.org/search?searchon=contents&mode=exactfilename&suite=stable&arch=${ARCH}"

usage ()
{
    echo 'Usage: sh debdeps.sh file'
    exit 0
}

[ ${#} -ne 1 ] && usage

for lib in $(ldd "${1}" | grep '=>' | cut -b2- | cut -f1 '-d '); do
    w3m -dump "${URL}&keywords=${lib}" \
	| grep "^/.*${lib}" \
	| xargs printf "${lib}\t%s\t%s\n"
done

Vadim Penzin, September 1st, 2015


I hereby place this article into the public domain.
You are welcome to contact me by writing to howto at this domain.
I publish this information in the hope that it will be useful, but without ANY WARRANTY.
You are responsible for any and all consequences that may arise as the result of using this information.