If you are on a Debian or Debian-based system (like Ubuntu), there are high chances you might have encountered .deb packages. These are Debian packages, and Linux command line offers built-in commands/tools to deal with this kind of packages. One such tool is dpkg, which we will discuss here in this tutorial.
But before we do that, it’s worth mentioning that all examples in this tutorial have been tested on an Ubuntu 16.04LTS machine.
Linux dpkg command
The dpkg tool is basically a package manager for Debian/Debian-based systems. Following is its syntax:
dpkg [options] filename
And here’s how the man page explains it:
dpkg is a tool to install, build, remove and manage Debian packages.
The primary and more user-friendly front-end for dpkg is aptitude(1).
dpkg itself is controlled entirely via command line parameters, which
consist of exactly one action and zero or more options. The action-
parameter tells dpkg what to do and options control the behavior of the
action in some way.
dpkg can also be used as a front-end to dpkg-deb(1) and dpkg-query(1).
The list of supported actions can be found later on in the ACTIONS sec?
tion. If any such action is encountered dpkg just runs dpkg-deb or
dpkg-query with the parameters given to it, but no specific options are
currently passed to them, to use any such option the back-ends need to
be called directly.
Following are some Q&A styled examples that should give you a good basic idea on how dpkg works.
Q1. How to install a package using dpkg?
This you can do using the -i command line option.
dpkg -i [package-name]
dpkg -i google-chrome-stable_current_amd64.deb
Here’s what all steps are involved in the installation process:
1. Extract the control files of the new package.
2. If another version of the same package was installed before
the new installation, execute prerm script of the old package.
3. Run preinst script, if provided by the package.
4. Unpack the new files, and at the same time back up the old
files, so that if something goes wrong, they can be restored.
5. If another version of the same package was installed before
the new installation, execute the postrm script of the old pack?
age. Note that this script is executed after the preinst script
of the new package, because new files are written at the same
time old files are removed.
6. Configure the package. See --configure for detailed informa?
tion about how this is done.
Q2. How to remove an already installed package using dpkg?
This can be done using the -r command line option.
dpkg -r [package-name]
dpkg -r googler_3.3.0-1_all.deb
Here’s what the man page says about this option:
Removing of a package consists of the following steps:
1. Run prerm script
2. Remove the installed files
3. Run postrm script
Q3. How to list all installed packages in the system?
For this, you can use the -l command line option.
For example, here’s the output this command line option produced on my system:
Q4. How to make dpkg list contents of a package?
This can be done using the –contents flag.
dpkg –contents [package name]
Q5. How to just unpack a package using dpkg?
There may be times when you’d want to just unpack the package, not configure it. Well, dpkg provides an option for this as well: –unpack.
dpkg –unpack [package-name]
If later on you’d like to configure an already unpacked package, you can do so using the –configure command line option.
dpkg –configure [package-name]
Following is what the man page says about this option:
Configuring consists of the following steps:
1. Unpack the conffiles, and at the same time back up the old
conffiles, so that they can be restored if something goes wrong.
2. Run postinst script, if provided by the package.
Q6. How to check if a package is installed or not?
Use the -s command line option for this.
dpkg -s [package-name]
Q7. How to print architecture of packages dpkg installs?
This info can be accessed using the –print-architecture command line option.
For example, the output the above command produced on my system was:
Q8. How to purge a package using dpkg?
We already discussed how to remove a package using the dpkg command. You can also purge a package, a process which removes everything, including conffiles. This can be done using the -P command line option.
dpkg -P [package-name]
Here’s what the man page has to say about this option:
Some configuration files might be unknown to dpkg because
they are created and handled separately through the configura?
tion scripts. In that case, dpkg won't remove them by itself,
but the package's postrm script (which is called by dpkg), has
to take care of their removal during purge. Of course, this only
applies to files in system directories, not configuration files
written to individual users' home directories.
Purging of a package consists of the following steps:
1. Remove the package, if not already removed. See --remove for
detailed information about how this is done.
2. Run postrm script.
The dpkg command offers plethora of options. What we’ve discussed here are those options that will help you get started with the tool. Once you’re done practicing these, head to the command’s man page for more information.