How to find reason for orphaned packages in apt-get autoremove?

Solution 1:

Automatically:

One of the solutions is to use aptitude, first install it:

sudo apt install aptitude

Now to find out why a package has been installed on your Ubuntu you can run:

aptitude why pkgname 

However note that:

The dependency that aptitude produced in this case is only a suggestion. This is because no package currently installed on this computer depends on or recommends the "pkgname".

Also:

aptitude why does not perform full dependency resolution; it only displays direct relationships between packages.

In conclusion if you can't find a related package, then there is a high chance that this package has been installed as a dependency or recommendation of on of the other packages that is going to be removed using apt autoremove, or somehow is related to this chain.

Manually

First let's produce a situation then try to find out why a package has been installed, I first installed vlc on my system:

sudo apt install vlc

after that I only remove the vlc itself using sudo apt remove vlc, now if I run sudo apt autoremove I'll get:

libqt5x11extras5 vlc-bin vlc-plugin-qt ...

let's first check one of them using aptitude:

$ aptitude why libqt5x11extras5
aptitude why libqt5x11extras5
i   xorg      Depends  xterm | x-terminal-emulator
p   qterminal Provides x-terminal-emulator

however the other one because of a direct dependency chain will work fine:

$ aptitude why vlc-bin
c   vlc      Depends  vlc-bin

pay attention to the c it means that the package was deleted but its configuration files still lives on my system so there is a high chance (in this case 100%) that vlc-bin was installed by vlc.

Now let's back to our job and findout why libqt5x11extras5 lives on our Ubuntu:

$ apt-cache rdepends libqt5x11extras5 | xargs dpkg -l |& grep -e '^i' -e '^rc'

using apt-cache rdepends I'm looking for all packages which has dependency on libqt5x11extras5, then I'm looking for the ones which are installed or was installed on my system using dpkg with the combination of grep, the result is:

ii  libqt5x11extras5
ii  vlc-plugin-qt

see? we found out that this package was related to another package that is in our autoremove list: vlc-plugin-qt, at the end:

$ aptitude why vlc-plugin-qt
c   vlc

and we came back to the vlc.