1. Overview

Linux systems usually provide package managers for managing software packages. The dpkg package manager handles .deb or Debian files on Ubuntu and other Debian systems.

For a system administrator, a common task is to add new packages or remove obsolete ones.

In this tutorial, we’ll specifically discuss how to uninstall a .deb package installed with dpkg. Also, we’ll see how to install .deb files as well as handle dependencies, and post-uninstallation cleanup.

2. Installing a .deb Package With dpkg

To start with, let’s first install a .deb file with dpkg. For this tutorial, we’ll install the Anydesk remote desktop software for Ubuntu. Moreover, we’ve manually downloaded the *.*deb file for Anydesk on our system.

Let’s install it with the dpkg command followed by the -i option:

$ sudo dpkg -i anydesk_6.3.0-1_amd64.deb 
(Reading database ... 444578 files and directories currently installed.)
Preparing to unpack anydesk_6.3.0-1_amd64.deb 
...

The dpkg command requires sudo privileges to perform the installation task. Moreover, the installation comes with several config files:

$ ls /etc/anydesk
connection_trace.txt  service.conf  system.conf

Notably, sometimes dpkg installation errors may be caused by unmet dependencies. This is because dpkg doesn’t take care of dependencies. In such cases, we need to explicitly install them.

3. Listing Installed .deb Packages

Before removing a package, it’s usually easiest to do so by name or part of its name.

So, let’s first identify the name of a Debian package we want to remove. Notably, the dpkg tool uses the –list or -l option to list the available packages:

$ dpkg -l
Desired=Unknown/Install/Remove/Purge/Hold
...
||/ Name    Version   Architecture Description
ii  acc...  22.07.4    amd64        query and manipulate ...
ii  acl     2.3.1-1    amd64        access control list  ...
ii  acp...  0.144      amd64        scripts for handling ...
...

This command shows all the packages on the system. Further, we can also use the grep command to specifically select the package of our interest:

$ dpkg -l | grep anydesk
ii  anydesk    6.3.0   amd64  The fastest remote ...

Alternatively, the dpkg command with its -S option can leverage the command path to get the package name:

$ dpkg -S /usr/bin/anydesk
anydesk: /usr/bin/anydesk

Thus, we can see the anydesk package name specific to the Anydesk software.

4. Uninstalling *.*deb Packages

Now that we know the package name, we can uninstall it with either dpkg or apt.

Notably, dpkg is the main Debian packaging tool. However, the dpkg utility is lower-level, and apt is a front-end of dpkg.

Primarily, apt has multiple applications:

  • installs and manages .deb packages, and uses other subcommands to offer more functionalities
  • provides efficient dependency management during package installation or removal
  • searches and retrieves packages from repositories

Let’s see the use of each of the tools.

5. Using dpkg

dpkg provides two primary methods for uninstalling packages:

  • dpkg -r <package_name>: removes the package and its associated files, but leaves configuration files
  • dpkg –purge <package_name>: not only removes the package but also purges its configuration files and leftover data

As we’ll see, there is a significant difference between these two options.

5.1. The -r or –remove Option

First, let’s use the -r option approach to remove a package:

$ sudo dpkg -r anydesk
(Reading database ... 444577 ....
Removing anydesk (6.3.0) ...
Removed /etc/systemd/system/....
...

However, this way still leaves behind some files:

$ ls /etc/anydesk
connection_trace.txt  service.conf  system.conf

Some parts of the package are also left behind:

$ dpkg -l | grep anydesk
rc  anydesk  6.3.0   amd64  The fastest remote desktop software on the market.

Hence, to fully uninstall a package, dpkg uses the purge option.

5.2. The —**purge Option

Let’s now use the purge option to uninstall the .deb package:

$  sudo dpkg --purge anydesk
(Reading database ... 
Removing anydesk (6.3.0) ...
Removed /etc/systemd/system/...
Purging configuration files for anydesk (6.3.0) ...
...

Next, we again check the packages left related to Anydesk:

$ dpkg -l | grep anydesk

The blank output shows all the Anydesk-specific files are now absent. Similarly, related config files are also removed from the /etc directory:

$ ls /etc/anydesk
ls: cannot access '/etc/anydesk': No such file or directory

Thus, this option removes all the related directories and configuration files.

6. Using apt

APT, or Advanced Package Tool is a feature-rich package management system. It comprises many command-line tools including apt.

Technically, apt* builds upon the base of *dpkg by internally calling upon dpkg to add or remove packages**. The apt command can install packages from repositories as well as manual downloads.

Let’s see how it handles uninstallation.

6.1. The remove Option

Removing packages with apt is fairly straightforward. We just use the apt remove command followed by the package name:

$ sudo apt remove anydesk
Reading package lists... Done
Reading state information... Done
The following packages were ...
  ieee-data...
...

Again, the remove subcommand leaves behind some Anydesk-related packages:

$ dpkg -l | grep anydesk
rc  anydesk     6.3.0      amd64        The fastest remote desktop software on the market.

Similarly, there are leftover files as well:

$ ls /etc/anydesk
connection_trace.txt  service.conf  system.conf

Thus, apt remove is similar to the dpkg –remove command.

6.2. The purge Option

Just like dpkg –purge command, apt uses the purge option to remove the spare config files and directories:

$ sudo apt purge anydesk
Reading package lists... Done
Building dependency tree... Done
...
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  anydesk*
...
Do you want to continue? [Y/n]

Again, let’s check for any left Anydesk-related packages or files:

$ dpkg -l | grep anydesk
$ ls /etc/anydesk
ls: cannot access '/etc/anydesk': No such file or directory

Finally, all the Anydesk files and packages are absent now.

7. Post-Uninstallation Cleanup

After removing any .deb package, we can use the autoremove subcommand to remove unnecessary dependencies:

$ sudo apt autoremove

This command removes the packages that were automatically installed but aren’t required anymore.

Furthermore, sometimes the /var/log directory holds the leftover log files. We can thus also remove them using the rm command:

$ sudo rm /var/log/anydesk.trace

By running these commands, we can free up more storage space.

8. Conclusion

In this article, we’ve learned how to uninstall a .deb package installed with dpkg.

First, we saw how to install a .deb file using dpkg. Next, we used the dpkg and apt commands to remove the installed packages. Finally, we removed spare files and unnecessary dependencies. This helps reclaim disk space from unused applications.