1. Overview
dpkg is the core package manager for Debian-based Linux systems, streamlining software package installation, removal, and administration. Moreover, the tool can conduct essential tasks such as unpacking, configuring, and maintaining a package database.
In this tutorial, we’ll examine the functions of the dpkg command and how to utilize it efficiently. Additionally, we’ll offer some examples to get acquainted with this tool under Linux.
2. Syntax
Understanding the basic operations of dpkg is essential for any Linux user. This is especially so since this command forms the basis for managing software packages in Linux.
The basic syntax of dpkg comprises two parameters:
dpkg [options] [package.deb|package-name]
Now that we’ve seen the syntax, let’s understand these parameters:
- [options]: flags to alter its behavior and output
- [package.deb|package-name]: packages to be applied to the command
Let’s see a breakdown of the most commonly used commands and their functions:
Options
Description
-i
Install a package
-r
Remove a package
-l
list of installed packages
-s
Show information about a specific installed package
-C
Check for broken dependencies
-L
List files installed by a package
-P
Purge package configuration files
-A
Configure all unpacked but unconfigured packages
Next, we proceed to apply this command to deb packages in practice.
3. Basic Usage
As we already saw, the dpkg command has many options with different functions.
3.1. The -i Option
The -i option instructs the dpkg command to install a deb software package.
For example, we can install the jed programmers editor via its deb package:
$ sudo dpkg -i jed_0.99.20\~pre.181+dfsg-1_amd64.deb
[sudo] password for kali:
(Reading database ... 518829 files and directories currently installed.)
Preparing to unpack jed_0.99.20~pre.181+dfsg-1_amd64.deb ...
Unpacking jed (1:0.99.20~pre.181+dfsg-1) over (1:0.99.20~pre.178+dfsg-6) ...
Setting up jed (1:0.99.20~pre.181+dfsg-1) ...
Updating precompiled files... done
Processing triggers for kali-menu (2023.4.7) ...
Processing triggers for desktop-file-utils (0.27-1) ...
Processing triggers for mailcap (3.70+nmu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for man-db (2.12.0-3) ...
From the above result, we can see that the dpkg -i command initiated and guided the installation of the software.
3.2. The -r Option
Next, the -r option prompts the dpkg command to remove specified packages from the operating system.
Before removing the jed package we just installed*,* it’s good to check the dpkg.log file and identify installed packages on the Linux system. For example, we can check the most recent installed packages:
$ grep "install" /var/log/dpkg.log | tail
2024-05-13 18:54:24 status installed hicolor-icon-theme:all 0.17-2
2024-05-13 18:54:25 status installed man-db:amd64 2.12.0-3
2024-05-13 18:59:47 startup archives install
2024-05-13 18:59:48 status half-installed jed:amd64 1:0.99.20~pre.178+dfsg-6
2024-05-13 18:59:48 status installed jed:amd64 1:0.99.20~pre.181+dfsg-1
2024-05-13 18:59:48 status installed kali-menu:all 2023.4.7
2024-05-13 18:59:48 status installed desktop-file-utils:amd64 0.27-1
2024-05-13 18:59:48 status installed mailcap:all 3.70+nmu1
2024-05-13 18:59:48 status installed hicolor-icon-theme:all 0.17-2
2024-05-13 18:59:49 status installed man-db:amd64 2.12.0-3
The log shows part of all the installed packages; the grep command matches the pattern install within strings inside the dpkg.log file. Next, the tail command reveals ten lines of information at the bottom of the log, which are the latest installed packages.
Now that we have an idea of the installed packages and dependencies with the jed package, let’s remove the previously installed package:
$ sudo dpkg -r jed
[sudo] password for kali:
(Reading database ... 518829 files and directories currently installed.)
Removing jed (1:0.99.20~pre.181+dfsg-1) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
Processing triggers for man-db (2.12.0-3) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for desktop-file-utils (0.27-1) ...
Processing triggers for mailcap (3.70+nmu1) ...
Processing triggers for kali-menu (2023.4.7) ...
As a result of the -r option with the dpkg command, the jed package was removed from the system. We can check for removed packages by checking the dpkg.log file:
$ grep "remove" /var/log/dpkg.log | tail
2024-05-13 18:50:43 startup packages remove
2024-05-13 18:50:44 remove jed:amd64 1:0.99.20~pre.178+dfsg-6 <none>
2024-05-14 16:41:42 startup packages remove
2024-05-14 16:41:44 remove jed:amd64 1:0.99.20~pre.181+dfsg-1 <none>
Similar to the previous operation of checking recently installed packages, the command above shows the latest removed packages with the dpkg command. As shown in the result, the jed command was removed.
3.3. The -l Option
The -l option prompts the dpkg command to list all installed packages:
$ dpkg -l > instPacks.txt
So, we saved the list of installed packages into a file named instPacks.txt. Subsequently, we can view the content using the cat command:
$ cat instPack.txt
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-===========================================================-==========================================================
ii 7zip 23.01+dfsg-8 amd64 7-Zip file archiver with a high compression ratio
ii accountsservice 23.13.9-6 amd64 query and manipulate user account information
ii acl 2.3.2-1 amd64 access control list - utilities
ii adduser 3.137 all add and remove users and groups
ii adwaita-icon-theme 46.0-1 all default icon theme of GNOME
ii amass 4.2.0-0kali1 amd64 In-depth DNS Enumeration and Network Mapping
...
From the result, we can see that the dpkg -l command not only outputs the installed packages but also adds a few other pieces of information:
- Desired
- Status
- Err?
These additions provide more details about the package. For example, we previously filtered the dpkg.log using two of the possible Desired values: install and remove.
3.4. The -s Option
Furthermore, we can show information about specific packages using the -s option of the dpkg command.
Let’s examine one of the installed packages from the previous list:
$ dpkg -s 7zip
Package: 7zip
Status: install ok installed
Priority: optional
...
Homepage: https://www.7-zip.org/
The output here is very long and contains detailed data about many aspects of the selected package. For example, package dependencies, who manages the package, its size, and many more were all included.
3.5. The -P Option
The -P option prompts the dpkg command to purge the package of all related files that were installed along with it:
$ sudo dpkg -P jed
[sudo] password for kali:
(Reading database ... 518818 files and directories currently installed.)
Purging configuration files for jed (1:0.99.20~pre.181+dfsg-1) ...
The command syntax above evaluates all installed packages before finally purging the jed configuration files.
4. Advanced Options
Now, we can advance the basic knowledge by looking at options that force, audit, and unpack packages.
4.1. Using Force Long Flags
Let’s use the force options with dpkg:
$ sudo dpkg --force-architecture --install jed_0.99.20.181_amd64.deb
[sudo] password for kali:
Selecting previously unselected package jed.
....
Preparing to unpack jed_0.99.20.181_amd64.deb ...
Unpacking jed (1:0.99.20~pre.181+dfsg-1) ...
...
Updating precompiled files... done
...
The –force-architecture makes dpkg force the installation of packages even if the architecture doesn’t match.
We can take a look at another force option that deals with versions:
$ sudo dpkg --install --force-confnew jed_0.99.20.181_amd64.deb
Selecting previously unselected package jed.
(Reading database ... 518818 files and directories currently installed.)
Preparing to unpack jed_0.99.20.181_amd64.deb ...
The above long flag, –force-confnew, forces dpkg to always use the new version of a configuration file during installation.
4.2. Using Audit Long Flags
The –audit option is used to check all installed packages for issues. These issues may include security vulnerabilities, dependency problems, file integrity, configuration, and consistency checks.
Let’s examine the usage of this option:
$ dpkg --audit
Notably, the command returned no output, meaning no issue was found.
4.3. Unpacking Packages
Finally, let’s understand how to unpack packages to get to their files and directories:
$ sudo dpkg --unpack jed_0.99.20.181_amd64.deb
(Reading database ... 518829 files and directories currently installed.)
Preparing to unpack jed_0.99.20.181_amd64.deb ...
Unpacking jed (1:0.99.20~pre.181+dfsg-1) over (1:0.99.20~pre.181+dfsg-1) ...
...
Processing triggers for man-db (2.12.0-3) ...
The command unpacked the jed deb package as shown above. Thus, we gained more insight into the various other dependencies that the package would install as well as the contents of its files and directories.
5. Conclusion
In the article, we discussed the basic syntax of using the dpkg command and its various common flags.
By adding flags, we modified the behavior of the dpkg command, thus affecting its output. Furthermore, we also demonstrated more use cases with long flags.