Ubuntu Server 18.04 – Cleaning up orphaned apt packages

How to Create MySQL Users Accounts and Grant Privileges

As you manage packages on your server, you’ll eventually run into a situation where you’ll have packages on your system that are installed, but not needed by anything. This occurs either when removing a package that has dependencies, or the dependencies on an installed package change. As you’ll remember, when you install a package that requires other packages, those dependencies are also installed. But if you remove the package that required them, the dependencies will not be removed automatically.

To illustrate this situation, if I remove the apache2 package from one of my servers, I will see the following extra information if I then try to install something else:

Output with orphaned packages shown

In this example, I removed apache2 (that was done before the screenshot was taken) then I went on to install mariadb-server. The package I was trying to install is arbitrary; the important part is the text you see in the screenshot where it says The following packages were automatically installed and are no longer required. Basically, if you have orphaned packages on your system, Ubuntu will remind you periodically as you use the APT suite of tools. In this case, I removed apache2 so all of the dependencies that were installed to support the apache2 package were no longer needed.

In the screenshot, I’m shown a list of packages that APT doesn’t think I need anymore. It may be right, but this is something we would need to investigate. As instructed in the output, we can use the apt autoremove command as root or with sudo to remove them. This is a great way of keeping our installed packages clean but should be used with care. If you’ve just recently removed a package, it’s probably safe to do the cleanup.

Although we haven’t walked through updating packages (we’ll do that in a later chapter), a situation that may come up later is one in which you have outdated kernels that can be cleaned with the autoremove option. These will appear in the same way as the example orphans I was shown in the previous screenshot, but the names will contain linux-image. Take care with these; you should never remove outdated kernels from your server until you verify that the newly installed kernel is working correctly. Generally, you would probably want to wait at least a week before running apt autoremove when kernel packages are involved. When it comes to other packages, they are generally safe to remove with the apt autoremove command, since the majority of them will be packages that were installed as a dependency of another package that is no longer present on the system. However, double-check that you really do want to remove each of the packages before you do so. Later in the next section, I’ll show you how to inform APT that you’d rather keep a package that is marked as an orphan, in case you run into a situation where APT lists a package for cleanup that you’d rather keep.

Comments are closed.