Home Package management A Beginner’s Guide To Using dpkg Command In Linux

A Beginner’s Guide To Using dpkg Command In Linux

By sk
1.4K views

The dpkg is a medium-level package manager that allows you to install, remove, and configure software packages on your Linux system. In this tutorial, we'll walk you through the basics of using dpkg command in Debian, Ubuntu and its derivatives.

What is dpkg in Linux?

dpkg stands for "Debian Package" and is the underlying tool used by Debian-based Linux distributions like Ubuntu. It's a command-line utility that lets you manage individual .deb packages on your system.

While dpkg is powerful, it's considered a medium-level tool, meaning it doesn't handle dependencies automatically. For that, you might want to use a higher-level package manager like apt.

Basic Syntax

The basic syntax of dpkg command is:

dpkg [options] action

Common Actions:

  • -i, --install <package_file>: Install the specified .deb package.
  • -r, --remove <package_name>: Remove the specified package (but keep its configuration files).
  • -P, --purge <package_name>: Remove the specified package and purge its configuration files.
  • -l, --list [package_name]: List packages that match the given pattern. If no pattern is provided, it lists all installed packages.
  • -s, --status <package_name>: Display detailed status information about the specified package.
  • -L, --listfiles <package_name>: List files installed by the specified package.
  • -S, --search <filename_pattern>: Search for a filename from installed packages.
  • -c, --contents <package_file>: List the contents of the specified .deb package file.

Important Options:

  • -b, --build <directory>: Build a .deb package from the specified directory.
  • -A, --record-avail <package_file>: Update the package availability information with the information from the specified .deb file.
  • -E, --skip-same-version: Skip installation of a package if the same version is already installed.
  • -G, --refuse-downgrade: Refuse to install a package if a newer version is already installed.
  • -R, --recursive: Process all files that are directories, recursively.
  • -V, --verify <package_name>: Verify the integrity of the specified package by checking its files against the recorded checksums.
  • --configure <package_name>: Configure a package that has been unpacked but not yet configured.
  • --unpack <package_file>: Unpack the specified .deb package file, but do not configure it.
  • --no-act, --dry-run, --simulate: Perform a simulation of the actions that would be taken, but do not actually change the system.

Common dpkg Command Examples

Here are some of the most common dpkg commands we will use for day-to-day tasks.

1. Install a Package

To install a .deb package file, use the following command:

dpkg -i package.deb

Example:

sudo dpkg -i google-chrome-stable_current_amd64.deb

This command installs the Google Chrome browser on your Debian system.

2. Remove a Package

To remove a package, use the following command:

dpkg -r package_name

Example:

sudo dpkg -r firefox-esr

This command removes the Firefox browser from your system.

3. Purge a Package

If you want to remove a package along with its configuration files, use the following command:

dpkg -P package_name

Example:

sudo dpkg -P linux-image-5.10.0-11-amd64

This command removes the specified Linux kernel image along with its configuration files.

4. List Installed Packages

To list all installed packages on your system, use the following command:

dpkg -l

You can also filter the list by using grep:

dpkg -l | grep -i chrome

This command lists all installed packages that contain "chrome" in their name.

Sample Output:

ii  google-chrome-stable                                    129.0.6668.70-1                                      amd64        The web browser from Google

5. List files installed by a package

To list all files installed by a specific package, run:

dpkg -L package_name

Example:

dpkg -L google-chrome-stable

Sample Output:

/.
/etc
/etc/cron.daily
/opt
/opt/google
/opt/google/chrome
/opt/google/chrome/CHROME_VERSION_EXTRA
/opt/google/chrome/MEIPreload
/opt/google/chrome/MEIPreload/manifest.json
/opt/google/chrome/MEIPreload/preloaded_data.pb
/opt/google/chrome/PrivacySandboxAttestationsPreloaded
/opt/google/chrome/PrivacySandboxAttestationsPreloaded/manifest.json
/opt/google/chrome/PrivacySandboxAttestationsPreloaded/privacy-sandbox-attestations.dat
/opt/google/chrome/WidevineCdm
/opt/google/chrome/WidevineCdm/LICENSE
[...]

6. Find out which Package a File belongs to

You can search for a file and find out which package it belongs to using command:

dpkg -S /path/to/file

For example, to search packages that the /bin/uname file belongs to, use the following command:

dpkg -S /bin/uname

Sample Output:

coreutils: /bin/uname

As you can see, the /bin/uname file belongs to coreutils package.

Similar Read: How To Find The Package That Provides A Specific File In Linux

7. Show Package Status

To check the status of a specific package, use the following command:

dpkg -s package_name

Example:

dpkg -s google-chrome-stable

This command shows the status of the Google Chrome package, including whether it's installed, configured, or not installed.

Package: google-chrome-stable
Status: install ok installed
Priority: optional
Section: web
Installed-Size: 356732
Maintainer: Chrome Linux Team <chromium-dev@chromium.org>
Architecture: amd64
Version: 129.0.6668.70-1
Provides: www-browser
Depends: ca-certificates, fonts-liberation, libasound2 (>= 1.0.17), libatk-bridge2.0-0 (>= 2.5.3), libatk1.0-0 (>= 2.11.90), libatspi2.0-0 (>= 2.9.90), libc6 (>= 2.17), libcairo2 (>= 1.6.0), libcups2 (>= 1.7.0), libcurl3-gnutls | libcurl3-nss | libcurl4 | libcurl3, libdbus-1-3 (>= 1.9.14), libdrm2 (>= 2.4.75), libexpat1 (>= 2.1~beta3), libgbm1 (>= 17.1.0~rc2), libglib2.0-0 (>= 2.39.4), libgtk-3-0 (>= 3.9.10) | libgtk-4-1, libnspr4 (>= 2:4.9-2~), libnss3 (>= 2:3.35), libpango-1.0-0 (>= 1.14.0), libudev1 (>= 183), libvulkan1, libx11-6 (>= 2:1.4.99.1), libxcb1 (>= 1.9.2), libxcomposite1 (>= 1:0.4.4-1), libxdamage1 (>= 1:1.1), libxext6, libxfixes3, libxkbcommon0 (>= 0.5.0), libxrandr2, wget, xdg-utils (>= 1.0.2)
Pre-Depends: dpkg (>= 1.14.0)
Description: The web browser from Google
Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier.

These are some of the most commonly used dpkg commands. Of course, there are many, but this should give you a good starting point for using dpkg on Debian-based systems. For more details, refer the manual pages:

man dpkg

Conclusion

dpkg is a command line package manager for managing software packages on Debian-based Linux systems. While it doesn't handle dependencies, it helps you to manually install, remove, and configure packages.

As stated already, dpkg is a medium-level tool and does not handle dependencies automatically. For dependency resolution, you typically use apt or apt-get, which are higher-level front-ends to dpkg.

When installing or removing packages, it's often better to use apt or apt-get to ensure that dependencies are handled correctly.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More