Have you ever broken apt? For seasoned Linux professionals, it is fairly common (about every six months) to use apt. Knowing a few commands in dpkg can often solve the problem more easily! Find out how.
What are apt and dpkg?
If you are using a Debian based distribution such as Ubuntu or Mint, use your computer apt to handle packages. Apt stands for Advanced packaging tool, which allows you to install, remove, clean, and manage Debian packages.
apt (used on the command line), your computer probably has a higher level / overarching Desktop-based package manager, which simply uses
apt, or perhaps
dpkg, on the background.
The package manager for Debian
dpkg is an alternative to managing packages on Debian-based installations. It is much lower than
apt, which gives you more freedom, but also a greater risk of breaking things.
This is why (unless you are an experienced DevOps engineer) I recommend you to
apt at any time, complete with
dpkg as soon as you run into problems.
By taking this step-by-step approach, you get the best of both worlds: the stability and ease of use of the
apt package manager, with the power and granularity of the
dpkg Debian package manager, once you need it.
For example, let̵
apt. Or maybe the installation of a package was accidentally interrupted.
Whatever the cause,
apt is now considered broken and you have already tried things like
sudo apt update --fix-missing and
sudo apt install --fix-broken in vain. If so, then …
Welcome to dpkg
apt is broken, it is no fun. If it is really broken, it is even less fun. Even if someone knows what they are doing, it sometimes takes a certain level of art and certainly skill to fix a broken packaging system.
The term ‘art’ is very appropriate, as you get a sense or feeling as you go through several troubleshooting steps – especially if you’ve done this many times – whether something will work out during the reboot.
The risk is always that the packaging system is so broken that if you reboot, your Linux instance may stop appearing and require further and possibly even more complex troubleshooting.
I’d like to say at this point that all of this can be easily avoided by running a few simple commands – but unfortunately that’s not the case. The fact is, the more you know, the easier it will be to fix it, and the more confident you will be in finding out whether you are ready to reboot the system or not.
This is where we are coming
dpkg program gives us much more granular control over all installed packages, and while there is more risk of something going wrong, there is also a lot more power to change a system in exactly the way we want. Let’s dive in.
If you’re interested in Linux in general, you may also want to read our three-part series of Bash Automation & Scripting.
Basic dpkg Use
We can see the packages currently on the system by simply running
dpkg -l. Since the output is likely to be very comprehensive (although normally paginated), you can use a
grep to limit the results.
dpkg -l | head -n5 && dpkg -l | grep 'gnome-calculator' dpkg -l | grep 'gnome-calculator'
The first command is actually a combination of two commands (separated by
&&which literally means: only run the second command if the first is successful).
In the first of these two assignments, we will take the first five lines (using a pipe
head -n5), and then, in the second command, we try to list the output we are looking for. In this case we are looking for the text
The secondary line / command below shows a version that we would normally use. Once someone is familiar with the syntax of the
dpkg output, the first five lines are often unnecessary. Note that the first column refers to (visual)
Desired=, where the second column refers to
Status=, and the third column to
Err?=. If you follow the lines visually, you will see how quickly they connect with each other.
gnome-calculator has been installed successfully with no current errors, no third column code underneath
Err?=. We notice that here too
uppercase=badis written in the brief info pertaining to the third column. As mentioned earlier, in our case it is blank.
The first column shows the desired status and the second column shows the current status. Both show as
Inst[alled] respectively, and this (
ii) is the most common series you want to see. It basically means that
everything is fine, that the package is installed, and that there are no issues.
Also note that, in the column descriptions between the parentheses, the capital letter indicates the relationship between a possible letter being seen and the relevant term. For example, consider how
f refers to
halF-conf, with capital letters
F in the same.
Now let’s take a look at another output to compare. This time, we’ll omit the first five lines and you can get a little more used to reading the output, even without the headers.
dpkg -l | grep 'mintwelcome'
Here we see the interesting result
ii used to be for the
gnome-calculator. If you are looking for it
r in the first
Desired= column description we saw above, you can see it stands for
Remove[d], and that
c in the second column represents
Ah! This (
mintwelcome, a welcome package on Mint) is a package that we removed at some point, but the configuration files for the same persist. Let’s clear it!
dpkg --purge 'mintwelcome' sudo dpkg --purge 'mintwelcome' dpkg -l | grep 'mintwelcome'
Note that you can also use the
-P shorthand instead of
In the first command we can try to purge
mintwelcome without using
sudo. However, this will not work. You need super user / root level privileges to uninstall (or install or uninstall) packages. We can run with again
sudo, and our package will be purified.
Note that the term Purification is used by both
apt as well as by
dpkg to indicate that we want to completely clean a package instead of just removing it, leaving all configuration files behind.
I trust you will come to appreciate its power, control and detail
dpkg can provide you with the management of packages on your Debian-based distribution, such as Ubuntu or, in this case, Mint. For example, it would be easy to scan for leftover configuration files from previously deleted packages simply by running:
dpkg -l | grep '^rc'
Before removing such configuration files, make sure no packages with a similar name are installed. In this case we can perform for example:
Notice how we get the second part of
Safe to remove or not? You can see how great power comes with great responsibility, as always in life. The decision to remove it or not is yours. There are generally three things you should always keep in mind when working with them
First, you want to check, double-check, and triple-check to be sure. For example, in the first output above, we may have gone ahead and casually removed it
Also note that really nothing is broken here. It’s okay to leave the configuration files for this seemingly deleted package. The old adage “If it ain’t broke, don’t fix it” certainly seems to apply in this case. It’s a good one to keep in mind when using
second, it’s important to remember that
apt is a package manager that keeps all connections (ie versions and dependencies) between packages in mind when installing, removing, or cleaning packages.
This is not the case for that
dpkg, which is much more package-based. There is more granularity, but also more risk of breaking things. If a package depends on someone else or on a library, someone will definitely break things by simply removing the main package with
Thirdly, most Linux packages have a lot of dependencies. So it is not recommended to just use
dpkg for standard package management, repeating our original advice to use
apt default and switch to
dpkg when it becomes necessary.
To remove / uninstall a package instead of removing it (i.e. leaving the configuration files), you can use the
-r) option to
dpkg. Use to verify a package
-C) option against a package performs database sanity and consistency checks for the package (or for all packages if no package is specified).
See for more options
man dpkg executed from your terminal. There are also specific, more complex commands that can be used for various packaging system problems. For this, it is usually best to use your favorite search engine for the specific problem or situation that arises at the moment it occurs. Often times, someone else has already encountered it and the details documented. If you discover something new, take a few minutes to post your findings to a forum or relevant discussion thread.
In this article, we started exploring
dpkg and how it can help us with detailed package management as opposed to
apt, which handles packages and all their dependencies. Remember, in these complex package management troubleshooting problems, great strength comes with great responsibility.
We also looked at how to take a more risk-averse approach, while realizing that deep debugging in package management is similar to art, and the better a painter is, the more successful the painting – ahem, the reboot will be.