Cleaning up an Ubuntu system

Ubuntu uses quite a nifty package manager. It keeps track of which file belongs to which package. Not only that - it also keeps track of which packages you requested, and which were installed as a dependency.

The package manager can remove unused dependencies automatically. And this is what we’ll be using to clean up your Ubuntu system. You see, you can mark packages as installed automatically, even if at some point you requested them yourself. By marking them as “automatically installed” you can be sure that they are only removed when they really aren’t needed any more. Here’s how it’s done:

  1. make a list of manually installed packages:

    aptitude search '~i!~E' -F '%M %100p' | grep -v ^A > manual_to_auto
  2. edit the file and remove from the file the packages you want to keep.

  3. read through the manual_to_auto file again to see whether you can afford to remove the packages that are in there.

  4. mark all packages that you left in the file as “automatic”. This will show a list of packages that will be removed and give you a chance to review the changes and bail out before anything harmful happens. Feel free to abort, re-edit the manual_to_auto file and try again until you’re happy

    sudo aptitude markauto $(< manual_to_auto)
  5. Be happy with the free space. I’ve freed up 1 GB on one machine and 500 MB on another with this method.

I suggest you remove at least the following packages from the manual_to_auto file:

  • linux-generic
  • linux-image-generic
  • linux-restricted-modules-generic
  • ubuntu-standard
  • ubuntu-minimal
  • ubuntu-keyring
  • the ….-desktop packages

Be careful; if you’re in doubt, remove the package from the list. Everything that you remove from the file will be kept as “manually installed”.

If you find out that you removed too many packages, you can simply reinstall the ones you need. Since we haven’t purged the configuration files (see below) they should simply work as before when you reinstall them.

I’ve used information from various sources. However, most of them fail on long package names like linux-restricted-modules-2.6.24-23-generic, because by default Aptitude only shows the first 30 characters of the package name. I also feel more comfortable having a file that I can edit and review before actually marking any packages.

Another cleanup step can be performed by removing all configuration files that belong to uninstalled packages:

dpkg -l | grep ^r | awk '{ print $2 }' | sudo xargs dpkg --purge

Be warned: this command does not ask for any confirmation. Don’t come crying to me when it runs off with your girlfriend.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer