How to Uninstall Packages in Ubuntu (apt & dpkg)

How to Uninstall Packages in Ubuntu (apt & dpkg)
To uninstall a package in Ubuntu, type sudo apt remove package-name. It deletes the installed software but keeps the configuration files, in case you'll need to reinstall the program later. This tutorial explains how to remove a package in Ubuntu with apt, purge configuration files, clean the system from unused dependencies, and fall back to using dpkg in cases where apt fails.
How do I uninstall a package in Ubuntu?
Type sudo apt remove package-name. It deletes the installed software but keeps the configuration files, in case you'll need to reinstall the program later.
$ sudo apt remove nginx
According to the apt-get manual, "removing a package leaves its configuration files on the system" — this is why installing back the program that you uninstalled some time ago may bring you back your custom configuration. Usually, this is a feature, not a bug, unless you want a clean installation.
What's the difference between apt remove and apt purge?
apt remove keeps the configuration files of the package. If you want to delete the files along with the software, use purge.
$ sudo apt purge nginx
According to the apt-get manual, purge is "identical to remove except that packages are removed and purged," which means that the configuration files are deleted as well. You can use purge for the package that was already removed via remove without the necessity to have it currently installed.
How do I remove unused dependencies after uninstalling a package?
Type sudo apt autoremove. It deletes all the software that has been installed as dependencies of other packages but is not used anymore.
$ sudo apt autoremove
When using apt remove or apt purge, the installed libraries that have been used for satisfying the dependencies of the package are not touched. However, these libraries are not needed now, and autoremove can help to clear the disk space. It is useful to use autoremove not only in case of removing something but also periodically.
What if apt remove won't uninstall a package cleanly?
Use sudo dpkg --remove package-name directly, or run sudo dpkg --configure -a first if the previous installation or removal was interrupted halfway.
$ sudo dpkg --configure -a
$ sudo dpkg --remove package-name
apt is just a friendly interface above dpkg, and most of the time, the removal failure happens because of the incomplete operation that leaves dpkg package database in the wrong state. dpkg --configure -a allows completing the configuration first, resolving the problem in the majority of cases.
Where does package cleanup matter most on a server?
In cases where you need to limit the attack surface and minimize the disk usage, as every installed package (and not only the used one) is a potential vulnerability target.
Both our VPS plans and Bare Metal servers start from a minimal base image, which allows you to choose which packages to install, so the decision is made not by the provider but by you.
Get started with Orbit Servers
Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.
Get startedRelated products
Written by
Julius