Mastering the Linux dpkg Command: A Beginner’s Guide with 8 Practical Examples

If you are using a Debian or Debian-based system, like Ubuntu, you have likely encountered .deb packages. These Debian packages can be managed effectively via the Linux command line using built-in commands and tools. One such essential tool is dpkg, which we will explore in this tutorial.

Note that all examples in this tutorial were tested on an Ubuntu 24.04 LTS machine.

Understanding the Linux dpkg Command

The dpkg tool serves as a package manager for Debian/Debian-based systems. Its basic syntax is as follows:

dpkg ACTIONS

OR

dpkg [options] filename

According to the man page:

       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.

Q1. How to install a package using dpkg?

To install a package, use the -i command line option.

dpkg -i [package-name]

For example:

dpkg -i google-chrome-stable_current_amd64.deb

The installation process involves several steps:

       1. Extract the control files of the new package.
       2. Execute prerm script of the old package if another version was installed.
       3. Run preinst script, if provided by the package.
       4. Unpack the new files, backing up old files as a safeguard.
       5. Postrm script of the old package is executed.
       6. Configure the package. See --configure for detailed information.

Q2. How to remove an already installed package using dpkg?

Use the -r command line option to remove a package.

dpkg -r [package-name]

For example:

dpkg -r googler_3.3.0-1_all.deb

According to the man page, removing a package involves these steps:

       1. Run prerm script.
       2. Remove the installed files.
       3. Run postrm script.

Q3. How to list all installed packages on the system?

List all installed packages using the -l command line option.

dpkg -l

Here is what this command might produce:

How to list all installed packages in the system

Q4. How to make dpkg list contents of a package?

This can be achieved using the --contents flag.

dpkg --contents [package name]

For example:

How to make dpkg list contents of a package

Q5. How to just unpack a package using dpkg?

To just unpack the package without configuring it, use the --unpack option.

dpkg --unpack [package-name]

Later, you can configure the package using the --configure option.

dpkg --configure [package-name]

According to the man page:

       Configuring involves the following steps:
       1. Unpack the conffiles, backing up old conffiles.
       2. Run postinst script, if provided by the package.

Q6. How to check if a package is installed?

Use the -s command line option for this check.

dpkg -s [package-name]

For example:

How to check if a .deb package is installed or not

Q7. How to print the architecture of installed packages?

Use the --print-architecture command line option to find this information.

dpkg --print-architecture

For example, this command outputted the following on my system:

amd64

Q8. How to purge a package using dpkg?

To purge a package, use the -P command line option, which will remove everything, including configuration files.

dpkg -P [package-name]

The man page explains:

       Some configuration files might be unknown to dpkg because
       they are created and handled separately through the configuration
       scripts. In that case, dpkg won't remove them by itself,
       but the package's postrm script has to take care of
       their removal during purge.

       Purging involves the following steps:
       1. Remove the package if not already removed.
       2. Run postrm script.

Conclusion

The dpkg command provides an array of options. This guide covers the basic options to help you start using dpkg. Once comfortable, refer to the command’s man page for advanced information.

Frequently Asked Questions (FAQs)

  • Is dpkg only applicable to Debian-based systems?
    Yes, dpkg is designed specifically for Debian-based systems such as Ubuntu.
  • Can I install a package with dependencies using dpkg?
    dpkg does not automatically handle dependencies. You might need to use apt-get after dpkg to resolve any dependencies.
  • What is the difference between dpkg -r and dpkg -P?
    -r removes the package but leaves configuration files, while -P purges the package completely, including configuration files.
  • How can I reconfigure a package?
    Use the command dpkg-reconfigure [package-name] to reconfigure an installed package.
  • What should I do if a dpkg operation fails?
    Check the output for errors and consult the system logs or man page for further assistance.