dpkg - Linux


Overview

dpkg is a package management command used with Debian and its derivatives such as Ubuntu. It is utilized to install, remove, and manage Debian software packages, but does not handle dependency resolution automatically. dpkg is most effectively used for low-level package management, troubleshooting, and for scenarios where precise control over packages is required.

Syntax

The basic syntax for dpkg is as follows:

dpkg [option...] action

Where action can be one of the tasks dpkg can perform (e.g., installation, removal) and option… consists of various flags that modify the behavior of the command.

Options/Flags

Here are some commonly used options in dpkg:

  • -i or --install: Install a package .deb file.
  • -r or --remove: Remove an installed package (leaves configuration files).
  • --purge: Remove an installed package including configuration files.
  • -l or --list: List all installed packages, optionally filtered by a pattern.
  • -s or --status: Show package status and information.
  • -L or --listfiles: List files installed to your system from package.
  • -C or --audit: Check for partially installed packages.
  • -S or --search: Search for a filename from installed packages.

Examples

Installing a Package

Install a package from a .deb file:

dpkg -i package-file.deb

Removing a Package

Remove a package by name:

dpkg -r package-name

Listing Installed Packages

List all installed packages containing the word “nginx”:

dpkg -l '*nginx*'

Checking Package Information

Check information of an installed package:

dpkg -s package-name

Finding Which Package a File Belongs To

Search for which installed package a specific file belongs to:

dpkg -S /bin/ls

Common Issues

  • Dependency errors: When installing packages that have unmet dependencies, dpkg will refuse to install the package. Using tools like apt or apt-get which automatically handle dependencies is recommended.
  • Configuration files not removed: When removing a package using -r or --remove, configuration files remain on the system. Use --purge to completely remove the package including its configuration files.

Integration

dpkg is often combined with shell scripting to automate installation tasks or integrated into recovery routines. Here’s an example where dpkg is used to reinstall all currently installed packages:

dpkg --get-selections | grep -w 'install' | cut -f1 | xargs sudo dpkg --reinstall
  • apt-get: Advanced package handling utility that handles downloading and installing packages with dependency management.
  • apt-cache: Interface to search and query available information about installed and installable packages.

For further reading, consult the official Debian dpkg manual by visiting the Debian documentation or the dpkg man page (man dpkg).