dpkg Command in Linux: Syntax, Options & Practical Examples (Debian/Ubuntu)

dpkg is the low-level package manager for .deb archives on Debian and Ubuntu. It installs, removes, configures, and inspects packages in the local dpkg database without resolving repository dependencies.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

dpkg Command in Linux: Syntax, Options & Practical Examples (Debian/Ubuntu)
About dpkg is the low-level package manager for .deb archives on Debian and Ubuntu. It installs, removes, configures, and inspects packages in the local dpkg database without resolving repository dependencies.
Tested on Ubuntu 25.04 (Plucky Puffin); dpkg 1.22.18; kernel 7.0.0-27-generic
Package dpkg
Man page dpkg(1)
Privilege root / sudo for install/remove
Distros

Debian, Ubuntu, Linux Mint, and other Debian derivatives.

RHEL and Fedora: rpm with dnf.

Related guide

dpkg — quick reference

Install and remove

Work directly with .deb files and installed package names.

When to use Command
Install a downloaded .deb package sudo dpkg -i package.deb
Reinstall from a .deb file sudo dpkg -i --force-reinstall package.deb
Remove a package but keep config files sudo dpkg -r package_name
Purge package and configuration files sudo dpkg -P package_name
Unpack without running configure scripts sudo dpkg --unpack package.deb
Configure unpacked packages sudo dpkg --configure package_name
Configure every pending package sudo dpkg --configure -a

Query installed packages

When to use Command
List installed packages (optionally filter) dpkg -l 'pattern*'
Show status and metadata for a package dpkg -s package_name
List files installed by a package dpkg -L package_name
Find which package owns a file path dpkg -S /path/to/file
Audit broken or incomplete packages sudo dpkg -C

Inspect .deb archives

When to use Command
Show control metadata from a .deb file dpkg -I package.deb
List files inside a .deb archive dpkg -c package.deb

Architecture and selections

When to use Command
Print native package architecture dpkg --print-architecture
List enabled foreign architectures dpkg --print-foreign-architectures
Export package selection states dpkg --get-selections
Compare two version strings dpkg --compare-versions 2.0 gt 1.0

Help and version

When to use Command
Show built-in usage text dpkg --help
Show dpkg version dpkg --version

dpkg — command syntax

Synopsis from dpkg --help on Ubuntu 25.04 (dpkg 1.22.18):

text
Usage: dpkg [<option>...] <command>

Common commands include -i|--install, -r|--remove, -P|--purge, -l|--list, -s|--status, -L|--listfiles, and -S|--search. Archive inspection uses -I and -c (implemented via dpkg-deb).

dpkg updates /var/lib/dpkg/. Install, remove, and purge need sudo.


dpkg — command examples

Essential List and inspect an installed package

Before changing packages, confirm the name, version, and state in the dpkg database.

Run the commands:

bash
dpkg -l bash
dpkg -s bash | head -12

Sample output:

text
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  bash           5.2.37-1.1ubuntu1 amd64        GNU Bourne Again SHell
Package: bash
Essential: yes
Status: install ok installed
Priority: required
Section: shells
Installed-Size: 2188
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Multi-Arch: foreign
Version: 5.2.37-1.1ubuntu1

The ii state means installed and configured. Essential packages like bash should not be removed.

Essential Install a local .deb file

Use dpkg -i when you already downloaded a vendor .deb. It does not fetch missing dependencies from repositories.

Build a tiny test package for this example:

bash
mkdir -p /tmp/dpkgdemo/DEBIAN /tmp/dpkgdemo/usr/local/bin
echo '#!/bin/sh' > /tmp/dpkgdemo/usr/local/bin/dpkgdemo
echo 'echo demo' >> /tmp/dpkgdemo/usr/local/bin/dpkgdemo
chmod 755 /tmp/dpkgdemo/usr/local/bin/dpkgdemo
printf 'Package: dpkgdemo\nVersion: 1.0\nArchitecture: amd64\nMaintainer: lab\nDescription: test\n' > /tmp/dpkgdemo/DEBIAN/control
dpkg-deb --build /tmp/dpkgdemo /tmp/dpkgdemo_1.0_amd64.deb

Install it:

bash
sudo dpkg -i /tmp/dpkgdemo_1.0_amd64.deb

Sample output:

text
Selecting previously unselected package dpkgdemo.
(Reading database ... 232077 files and directories currently installed.)
Preparing to unpack /tmp/dpkgdemo_1.0_amd64.deb ...
Unpacking dpkgdemo (1.0) ...
Setting up dpkgdemo (1.0) ...

Verify:

bash
dpkg -l dpkgdemo

Sample output:

text
ii  dpkgdemo       1.0          amd64        test
Essential Fix dependency errors after dpkg -i

When a .deb needs packages that are not installed, dpkg leaves the new package unconfigured.

If install fails with dependency errors, run:

bash
sudo apt-get install -f

That asks apt to install missing dependencies and finish configuration. Prefer sudo apt install ./package.deb on Ubuntu 16.04+ when you want dependency resolution in one step.

Common Remove versus purge

-r removes binaries but keeps configuration under /etc. -P deletes config files too.

Remove the test package but keep configs (none for this demo):

bash
sudo dpkg -r dpkgdemo
dpkg -l dpkgdemo 2>&1 || true

Reinstall, then purge:

bash
sudo dpkg -i /tmp/dpkgdemo_1.0_amd64.deb
sudo dpkg -P dpkgdemo
dpkg -l dpkgdemo 2>&1

Sample output after purge:

text
dpkg-query: no packages found matching dpkgdemo

Use purge when you want a clean uninstall; use remove when you might reinstall later and keep /etc settings.

Common List files and find package owners

Locate binaries from a package name, or find which package installed a path.

Run the commands:

bash
dpkg -L bash | head -6
dpkg -S /usr/bin/bash

Sample output:

text
/.
/etc
/etc/bash.bashrc
/etc/skel
/etc/skel/.bash_logout
/etc/skel/.bashrc
bash: /usr/bin/bash

On Ubuntu, bash lives at /usr/bin/bash, not /bin/bash — adjust the path you pass to -S.

Common Read metadata from a .deb without installing

Inspect vendor packages before you install them on production.

Run the commands:

bash
dpkg -I /tmp/dpkgdemo_1.0_amd64.deb | head -10
dpkg -c /tmp/dpkgdemo_1.0_amd64.deb

Sample output:

text
new Debian package, version 2.0.
 size 534 bytes: control archive=186 bytes.
     109 bytes,     5 lines      control
 Package: dpkgdemo
 Version: 1.0
 Architecture: amd64
 Maintainer: lab
 Description: test
drwxr-xr-x root/root         0 2026-07-01 15:36 ./
drwxr-xr-x root/root         0 2026-07-01 15:36 ./usr/
drwxr-xr-x root/root         0 2026-07-01 15:36 ./usr/local/
drwxr-xr-x root/root         0 2026-07-01 15:36 ./usr/local/bin/
-rwxr-xr-x root/root        20 2026-07-01 15:36 ./usr/local/bin/dpkgdemo
Common Finish interrupted configuration with --configure -a

Power loss or a broken script can leave packages half-configured. This command runs pending configure triggers for all unpacked packages.

Run the command:

bash
sudo dpkg --configure -a

When nothing is pending, dpkg returns quickly with no output. Pair with sudo apt-get install -f if dependencies are still broken.

Advanced Check architecture and version compare

Pick the correct .deb arch before copying packages between hosts.

Run the commands:

bash
dpkg --print-architecture
dpkg --print-foreign-architectures
dpkg --compare-versions 2.0 gt 1.0 && echo '2.0 is newer than 1.0'

Sample output:

text
amd64
2.0 is newer than 1.0

Foreign architectures appear only when you enabled multi-arch (for example i386 for 32-bit libraries).

Advanced Audit broken packages with -C

-C (or --audit) reports packages that are broken or need attention.

Run the command:

bash
sudo dpkg -C

Empty output means dpkg did not find broken packages. If lines appear, read the package name and follow with sudo dpkg --configure -a and sudo apt-get install -f.

Advanced Export package selections for migration

Save which packages are installed so you can replay selections on another host (package list only — not /etc configs).

Run the command:

bash
dpkg --get-selections | head -5

Sample output:

text
accountsservice					install
acl						install
acpid						install
adduser						install
adwaita-icon-theme				install

Restore on a new machine with dpkg --set-selections < file followed by sudo apt-get dselect-upgrade — a full migration workflow, not a single dpkg flag.


dpkg — when to use / when not

Use dpkg when Use something else when
  • You have a local .deb file and need direct install, remove, or query
  • You need low-level inspection: dpkg -L, dpkg -S, dpkg -s
  • You are recovering from a half-finished install (--configure -a, -C)
  • You are scripting package state export (--get-selections)
  • You want automatic dependency resolution from repos → apt
  • You only search remote package indexes → apt-cache
  • You manage .rpm packages → rpm or dnf
  • You need a guided desktop installer → GUI software center

dpkg vs apt

dpkg apt
Input .deb files and package names Repositories and .deb paths
Dependencies Does not resolve repos Resolves and installs deps
Best for Manual .deb, queries, recovery Everyday installs and upgrades

See the apt command for repository workflows.


Command One line
dpkg Low-level .deb tool (this page)

Browse the full index in our Linux commands reference.


dpkg — interview corner

What is dpkg?

dpkg is the core Debian package tool. It installs .deb archives, removes or purges packages, configures unpacked payloads, and answers questions about what is installed on the system.

It does not download from the internet or resolve repository dependencies — that is apt's job.

A strong answer is:

"dpkg is the low-level .deb package manager on Debian and Ubuntu — install, remove, query, configure. apt sits on top and resolves dependencies from repositories."

What does dpkg --configure -a do?

It runs the configure step for every package that is unpacked but not fully configured — common after an interrupted apt upgrade or failed postinst script.

It does not magically install missing dependencies; pair it with apt-get install -f when apt reports broken deps.

A strong answer is:

"--configure -a finishes pending package configuration for all unpacked packages — I use it after interrupted installs, then apt-get install -f if dependencies are still broken."

What is the difference between dpkg -r and dpkg -P?

-r (remove) deletes program files but keeps configuration files in /etc.

-P (purge) removes the package and its config files — a clean uninstall.

A strong answer is:

"dpkg -r removes the package but keeps config; dpkg -P purges configs too. I purge when I want no trace left in /etc."

Why does dpkg fail on dependencies?

dpkg only understands the declared Depends inside the .deb you passed. If another package is missing, it stops and may leave the new package unconfigured.

Fix by installing dependencies (apt-get install -f) or using apt install ./package.deb which pulls deps from repos.

A strong answer is:

"dpkg doesn't resolve repos — missing Depends stop the install. I run apt-get install -f or apt install ./file.deb to pull dependencies."

When do you use dpkg instead of apt?

Use dpkg for direct .deb manipulation, file ownership queries (dpkg -S), listing installed files (dpkg -L), auditing broken state (dpkg -C), or automation with --get-selections.

Use apt for normal installs from configured repositories where you want dependency resolution and safer upgrades.

A strong answer is:

"apt for everyday repo installs; dpkg for inspecting local debs, querying what's installed, and recovery commands like --configure -a."


Troubleshooting

When dpkg reports a lock file, find the holding process with ps aux | grep apt; the ps command covers quick PID discovery.

Symptom Likely cause Fix
dependency problems after -i Missing packages sudo apt-get install -f
package is in a very bad inconsistent state Interrupted install sudo dpkg --configure -a; then apt-get install -f
could not open file /var/lib/dpkg/lock Another apt/dpkg running Wait; ps aux | grep apt
no path found matching pattern with -S Wrong path or file not from a package dpkg -S with full path (e.g. /usr/bin/bash)
dpkg-query: no packages found Package not installed or purged dpkg -l | grep name
Unknown option Flag not in your dpkg build dpkg --help on the host

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.