Introduction

The package manager for Debian and distros derived from Debian is called dpkg or Debian package.
As users, we can use dpkg directly or use higher level programs like apt(advanced package management tool) which gives us a few more features.
Utilities such as dpkg-deb and dpkg-query use dpkg as a front-end to perform some actions.

In this article, we will demonstrate how you can use the dpkg package manager to list, install, uninstall and query package on an Ubuntu 16.04 system.

Example 1: Display dpkg version
To display the version of the dpkg package manager installed on your system use the dpkg command with the –version option.

root@linuxnix:~# dpkg --version
Debian 'dpkg' package management program version 1.18.4 (amd64).
This is free software; see the GNU General Public License version 2 or
later for copying conditions. There is NO warranty.
root@linuxnix:~#

 

Example 2: List all installed packages
Using the dpkg command with the -l option lists all packages currently installed on the system.

root@linuxnix:~# dpkg -l
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 accountsservice 0.6.40-2ubuntu11.1 amd64 query and manipulate user account information
ii adduser 3.113+nmu3ubuntu4 all add and remove users and groups
ii apparmor 2.10.95-0ubuntu2 amd64 user-space parser utility for AppArmor
ii apt 1.2.12~ubuntu16.04.1 amd64 commandline package manager
ii apt-transport-https 1.2.12~ubuntu16.04.1 amd64 https download transport for APT
-----------------------------------------------------------output truncated for brevity

As you may observe from the above output, the dpkg -l command displays the package name, it’s version, the system architecture the package is intended to work on along with a brief description of the package.

Example 3: Check if a particular package is installed
To check if a given package is installed on the system we use the dpkg -l command followed by the package name.
For example, to check if the wget package has been installed on the system, we would use the following command:

root@linuxnix:~# dpkg -l wget
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 wget 1.17.1-1ubuntu1.1 amd64 retrieves files from the web
root@linuxnix:~#

Example 4: Display files installed by a package
To display files copied on a system as part of a package installation, use the dpkg command with the -L option followed by the package name.

root@linuxnix:~# dpkg -L wget
/.
/etc
/etc/wgetrc
/usr
/usr/bin
/usr/bin/wget
/usr/share
/usr/share/info
/usr/share/info/wget.info.gz
/usr/share/doc
/usr/share/doc/wget
/usr/share/doc/wget/MAILING-LIST
/usr/share/doc/wget/NEWS.gz
/usr/share/doc/wget/AUTHORS
/usr/share/doc/wget/copyright
/usr/share/doc/wget/changelog.Debian.gz
/usr/share/doc/wget/README
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/wget.1.gz

Example 5: Display files contained in a .deb package file
In our previous example, we showed you how you could view files that had been installed by a package.
To view files that are part of a .deb package file and would be moved to various locations on the system after we installed the package, use the dpkg command with the -c option followed by the package name.

root@linuxnix:~# dpkg -c nano_2.7.4-1_amd64.deb 
drwxr-xr-x root/root 0 2017-01-11 07:40 ./
drwxr-xr-x root/root 0 2017-01-11 07:40 ./bin/
-rwxr-xr-x root/root 225320 2017-01-11 07:40 ./bin/nano
drwxr-xr-x root/root 0 2017-01-11 07:40 ./etc/
-rw-r--r-- root/root 8769 2017-01-11 07:40 ./etc/nanorc
drwxr-xr-x root/root 0 2017-01-11 07:40 ./usr/
drwxr-xr-x root/root 0 2017-01-11 07:40 ./usr/share/
drwxr-xr-x root/root 0 2017-01-11 07:40 ./usr/share/doc/
drwxr-xr-x root/root 0 2017-01-11 07:40 ./usr/share/doc/nano/
-rw-r--r-- root/root 2372 2016-12-31 22:11 ./usr/share/doc/nano/AUTHORS
---------------------------------------output truncated for brevity

Example 7: Install a package
To install a dpkg package on the system, use the dpkg command with -i option followed by the package name.

root@linuxnix:~# dpkg -i nano_2.7.4-1_amd64.deb
(Reading database ... 62478 files and directories currently installed.)
Preparing to unpack nano_2.7.4-1_amd64.deb ...
Unpacking nano (2.7.4-1) over (2.5.3-2) ...
Setting up nano (2.7.4-1) ...
Installing new version of config file /etc/nanorc ...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
Processing triggers for man-db (2.7.5-1) ...
root@linuxnix:~#

Example 8: Remove a package
To uninstall a package use the dpkg command with the -r option followed by the package name.

root@linuxnix:~# dpkg -r nano
(Reading database ... 62514 files and directories currently installed.)
Removing nano (2.7.4-1) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in auto mode
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
root@linuxnix:~#

Note that while uninstalling the package we only needed to specify the package name and not the entire .deb file name.

Example 9: Purge a package
Purging a package implies that we remove the binaries installed by the package while still maintaining it’s configuration file.
We use the -P option with the dpkg command to purge a package.
Let’s demonstrate it by purging the nano package.

root@linuxnix:~# dpkg -P nano
(Reading database ... 62514 files and directories currently installed.)
Removing nano (2.7.4-1) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in auto mode
Purging configuration files for nano (2.7.4-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
root@linuxnix:~#

Example 10: Display information about a package
To query information about a package, use the -s option with the dpkg command.
In the below example, we query some information about the wget package.

root@linuxnix:~# dpkg -s wget
Package: wget
Status: install ok installed
Priority: important
Section: web
Installed-Size: 880
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 1.17.1-1ubuntu1.1
Depends: libc6 (>= 2.17), libidn11 (>= 1.13), libpcre3, libssl1.0.0 (>= 1.0.1), libuuid1 (>= 2.16), zlib1g (>= 1:1.1.4)
Recommends: ca-certificates
Conflicts: wget-ssl
Conffiles:
/etc/wgetrc c43064699caf6109f4b3da0405c06ebb
Description: retrieves files from the web
Wget is a network utility to retrieve files from the web
using HTTP(S) and FTP, the two most widely used internet
protocols. It works non-interactively, so it will work in
the background, after having logged off. The program supports
recursive retrieval of web-authoring pages as well as FTP
sites -- you can use Wget to make mirrors of archives and
home pages or to travel the web like a WWW robot.
.
Wget works particularly well with slow or unstable connections
by continuing to retrieve a document until the document is fully
downloaded. Re-getting files from where it left off works on
servers (both HTTP and FTP) that support it. Both HTTP and FTP
retrievals can be time stamped, so Wget can see if the remote
file has changed since the last retrieval and automatically
retrieve the new version if it has.
.
Wget supports proxy servers; this can lighten the network load,
speed up retrieval, and provide access behind firewalls.
Homepage: https://www.gnu.org/software/wget/
Original-Maintainer: Noël Köthe <noel@debian.org>
root@linuxnix:~#

Example 11: Check which package owns a file
To check which package a file belongs to, we use the -S option with the dpkg command followed by the file name.
For example, let’s check which package provides the ssh command.

root@linuxnix:~# dpkg -S /usr/bin/ssh
openssh-client: /usr/bin/ssh
root@linuxnix:~#

As you may observe from the above output, the ssh command is provided by the openssh-client package.

Example 12: Install all packages in a directory
We can recursively install all .deb package files in a directory by using the -R option in combination with the -i option while invoking the dpkg command.
In the below example, I’ve used the dpkg command to recursively install all .deb package files available in the /root directory.

root@linuxnix:~# dpkg -R -i /root
Selecting previously unselected package nano.
(Reading database ... 62414 files and directories currently installed.)
Preparing to unpack /root/nano_2.7.4-1_amd64.deb ...
Unpacking nano (2.7.4-1) ...
Setting up nano (2.7.4-1) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
Processing triggers for man-db (2.7.5-1) ...
root@linuxnix:~#

Conclusion

In this article, we covered the most frequently used features of the dpkg package manager.
Continuing with our ongoing series of articles based on various package managers, we will be covering apt in our next article.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.