How to list/find installed packages that are not available from any of the configured APT repositories

During upgrades between releases of a Debian (or Ubuntu) system you can end up with a lot of trash ... packages left over from earlier releases. Using the following command you can find packages that are not available from any of the APT repositories you've configured in /etc/apt/sources.list (or /etc/apt/sources.list.d/*):
apt-cache policy '.*' | awk '/^[a-zA-Z0-9]+/ { if (m == 1 && vt == 3) print str; str = $0; m = 0; vt = 0; next } /^ *Version table:/ { vt = 1; next } { if (vt > 0) vt++; str = str "\n" $0; if (match($0, "^[ \t]*\\*\\*\\*")) { getline; vt++; str = str "\n" $0; if (index($0, "/var/lib/dpkg/status")) m = 1 } } END { if (m == 1 && vt == 2) print str }'

These packages might have been manually installed or are remnants of a previous OS release. You should review the list and decide for yourself whether to keep them or purge them.

This command does not show packages for which the current version was manually installed from a DEB, but the package is available from a repository too.

For a more thorough list (packages for which the currently installed version is not from a repository ... including packages for which an update is available from the respective repository), try this:
apt-cache policy '.*' | awk '/^[a-z0-9]/{pkg = substr($1, 1, length($1) - 1)}/ *\*\*\*/ {getline; if (index($0, "/var/lib/dpkg/status")) print pkg}' | sort
(Don't forget to upgrade all packages to the current latest version or they'll be listed too.)

I've tested the above commands in Ubuntu 10.10 (Maverick), but I see no reason why it should not work on other Ubuntu or Debian systems as well.

Update: as a matter of fact, the apt-cache policy '.*' command does not work (yet) in Debian Lenny. It accepts only package names and not regular expressions (as the more recent apt-cache in Ubuntu). You can use the following workaround:
apt-cache pkgnames | sort | xargs apt-cache policy | awk '/^[a-z0-9]/{pkg = substr($1, 1, length($1) - 1)}/ *\*\*\*/ {getline; if (index($0, "/var/lib/dpkg/status")) print pkg}'

PS: the idea for looking in /var/lib/apt/lists came from this post.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

alternative methods

I was wondering the same thing a few days ago. The easiest solution is: aptitude search '~o'.

More answers on http://askubuntu.com/questions/98223/how-do-i-get-a-list-of-obsolete-packages

Re: alternative methods

Wow! Thanks a lot. This is the best solution for this problem so far. Smile
I got used to the apt-* commands and didn't even think of looking into aptitude. It's a pity that it's not fully documented in the manpage (there's a separate package containing the full docs, but manpages are more accessible ... or one can just look it up in Google Smile).