Ubuntu – How to “autoremove” packages with aptitude package manager

aptitudeUbuntu

Is it possible to perform an equivalent of apt-get autoremove with aptitude? If yes, how?

The Debian Administrator's Handbook says that aptitude "autoremoves" packages automatically, but this is not true (not always true). In my case (Ubuntu GNOME 16.04), I have a linux kernel which apt-get autoremove suggests to get me rid of:

§ sudo apt-get autoremove
[sudo] password for alexey: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-4.4.0-31 linux-headers-4.4.0-31-generic
  linux-image-4.4.0-31-generic linux-image-extra-4.4.0-31-generic
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 295 MB disk space will be freed.
Do you want to continue? [Y/n]

aptitude, however, does not care about it.


Here is the output of aptitude why linux-image-4.4.0-31-generic:

i   ubuntu-gnome-desktop         Depends    gdm3                                                      
i A gdm3                         Recommends xserver-xorg                                              
c   xserver-xorg                 Recommends xserver-xorg-video-all | xorg-driver-video                
p   virtualbox-guest-x11         Provides   xorg-driver-video                                         
p   virtualbox-guest-x11         Depends    virtualbox-guest-utils (= 5.0.32-dfsg-0ubuntu1.16.04.2)   
p   virtualbox-guest-utils       Recommends virtualbox-guest-dkms (= 5.0.32-dfsg-0ubuntu1.16.04.2) | v
                                            irtualbox-guest-source (= 5.0.32-dfsg-0ubuntu1.16.04.2) | 
                                            virtualbox-guest-modules                                  
i A linux-image-4.4.0-31-generic Provides   virtualbox-guest-modules

Here is the output of aptitude search '~i linux.*4.4.0-31' -F '%c%a%M %p':

i A linux-headers-4.4.0-31
i A linux-headers-4.4.0-31-generic
i A linux-image-4.4.0-31-generic
i A linux-image-extra-4.4.0-31-generic

Best Answer

Automatic packages that are no longer used are marked as "id" in aptitude.

I find three ways to remove them.

  1. Aptitude UI

    sudo aptitude

    Press g to preview packages to be installed and removed.

    enter image description here

    You can find those packages under "Packages being removed because they are no longer used". Then press g again, aptitude will remove those packages.

  2. Aptitude command line with grep and xargs

    aptitude search ~i | grep ^id | awk '{print $2}' | xargs sudo aptitude purge -y

  3. Aptitude command line with Aptitude::Delete-Unused option

    sudo aptitude -o Aptitude::Delete-Unused=1 install

    Option: Aptitude::Delete-Unused

    Default: true

    Description: If this option is true, automatically installed packages which are no longer required will be automatically removed. For more information, see the section called “Managing automatically installed packages”.

    I found this solution from http://www.lambdacurry.com/2013/12/aptitude-equivalent-of-apt-get-autoremove/. And the option is described in "Configuration file reference" in the aptitude reference manual. I don't have enough reputation to post more links, so you can search around for the document.

Either of them works for me.

According to 6.2.7. Tracking Automatically Installed Packages:

With this information, when packages are removed, the package managers can compute a list of automatic packages that are no longer needed (because there is no “manually installed” packages depending on them).

and Managing automatically installed packages:

More precisely: they will be removed when there is no path via Depends, PreDepends, or Recommends to them from a manually installed package.

My answer applies only when there is no path of dependency (Depends, PreDepends, or Recommends) from a manually installed package to the target package. Thus there is no means of using aptitude to do the same thing as apt-get autoremove does in the situation described in the question (there exists a dependency path from ubuntu-gnome-desktop).

Related Question