How do you track which packages were installed on Ubuntu (Linux)?

(This question is very similar to 10458. It was suggested that Fedora and Ubuntu/Debian are different enough to warrant different answers.)

As I use any Ubuntu setup I gradually install a number of packages over and above the baseline installation. If I reinstall, or if I need to install a new machine, I usually want to reinstall those specific packages, and I want to do it fast to get back to work with a minimum of hassle. As far as I've seen all of the package managers (apt-get, aptitude and synaptic) can tell me which packages are installed, and they all have logs (albeit different ones for each tool, which is a hassle). But none of them can tell me which packages I've installed, as opposed to their dependencies or system updates. Even the logs are tricky in that I'm not entirely sure what I should be extracting from them, or how to integrate them (in the case of the various apt family tools). This means that each time I re-install, or even just backup, I'm not sure how to re-create that list.

I'm not necessarily expecting any of the tools to do this for me, but if they don't I'm looking for workarounds. Even patterns to grep for, good rules of thumb, or a clear idea of what exactly is being logged, would be useful. There may not be a "best answer" here but good ones would be very helpful.


Most of the answers below provide some approximation of what I am looking for, and are useful to some degree. The chosen one is the one that comes closest to a reasonably automatic way of re-installing my tools on a new system, even with all its caveats.


Solution 1:

On any Debian based machine, this is one common way to duplicate a package set. On the old machine:

dpkg --get-selections "*" > my_favorite_packages

Copy the file my_favorite_packages to the new machine (a thumb drive is a good option, but scp also works fine). Then run this sequence (with root privileges):

apt-get update
dpkg --set-selections < my_favorite_packages
apt-get -u dselect-upgrade

This doesn't get you only the packages you installed. It also gets their dependencies, etc. Also, if the repositories between the two machines are different, all bets are off.

As far as logs, apt-get keeps a log at /var/log/apt/history.log (thanks to Tshepang for updating this in a comment); dpkg does (at /var/log/dpkg.log), but it's famously hard to parse and can only be read with root privileges; aptitude has one at /var/log/aptitude and you can page through it with regular user privileges.

As far as I can tell, you are right that none of these logs track specifically what you installed as opposed to auto-installed dependencies. You can get that information, however, from an aptitude search. Search for all installed packages that were also installed automatically:

aptitude search '~i ~M'

If you want only the ones you installed (not the auto-dependencies), negate the ~M:

aptitude search '~i !~M'

If you want that formatted so that you have only the names of packages and the word "install", aptitude can do that too. This gives you a list ready to feed to dpkg --get-selections:

aptitude search '~i !~M' -F "%p install"

(I've got nothing on RedHat or RedHat-based systems. Sorry. There really is no one answer for Linux per se since package management is a big part of what makes different distros different.)

Solution 2:

Use dpkg -l '*' > jaunty.original to remeber all installed packages on a freshly installed system.

After you have installed all your additional packages do dpkg -l '*' > mysystem.2009017.

The additional packages are just the difference: diff jaunty.original mysystem.2009017