Image credit: Nicholas Moreau at WikiMedia

Apr 5, 2010

"aptitude search" and not "dpkg -l" or "dpkg-query -l"

 I ran into this little gotcha near the end of last month, but didn't get around to logging it yet.

As part of my contribution to Acire, I needed to obtain a list of UNinstalled Python library packages (primarily under Ubuntu; I've been coding for days now trying to build in an abstraction layer behind which other contributors can add code for other distros). Rather than deal with the python-apt library at that time, I decided to use the subprocess module to run a command-line query. Yes, when you've used a hammer for years, it tends to become the tool you use for pretty much everything :-(

dpkg was the first command I looked at. Its man page redirected me (for the -l option) to dpkg-query, whose man page blandly states:


-l, --list package-name-pattern...
List  packages  matching  given  pattern.  If no package-name-pattern is given, list all packages in /var/lib/dpkg/status, excluding the ones marked with state purge. Normal shell wildchars are allowed in package-name-pattern. Please note you will probably have to quote package-name-pattern to prevent the  shell  from performing filename expansion. For example this will list all package names starting with “libc6”:
                dpkg-query -l 'libc6*'


Perhaps the quiet "all packages in /var/lib/dpkg/status" carries a world of hidden meaning - I still don't know. What I did find out, during testing of my first-cut code, was that packages that Synaptic Package Manager showed as available were not shown as available by dpkg-query -l!!! From cursory investigation, it appears that dpkg-query only shows packages from Ubuntu's universe and not from other origins. And it shows installed as well as non-installed packages, but just from this one origin.


Later, I found that aptitude search [package-name-pattern] did indeed give me what I wanted, and switched to using that instead. Curiosity led me to get onto the Ubuntu IRC channel, since a Google search for bugs did not yield anything about this behavior of dpkg-query. Folks on IRC said that dpkg only looked at installed packages, which didn't seem to match up with what I'd found.

So I'm still wondering what the correct explanation is, but now I know that if I want to list uninstalled packages from the command line, I use aptitude search and not dpkg-query -l...