The Debian package management tools

What programs does Debian provide for managing its packages?
There are multiple tools that are used to manage Debian packages, from graphic or text-based interfaces to the low level tools used to install packages. All the available tools rely on the lower level tools to properly work and are presented here in decreasing complexity level.
It is important to understand that the higher level package management tools such as aptitude or synaptic rely on apt which, itself, relies on dpkg to manage the packages in the system.
See Chapter 2. Debian package management of the Debian reference for more information about the Debian package management utilities. This document is available in various languages and formats, see the Debian Reference entry in the DDP Users' Manuals overview.

dpkg
This is the main package management program. dpkg can be invoked with many options. Some common uses are:
Find out all the options: dpkg --help.
Print out the control file (and other information) for a specified package: dpkg --info foo_VVV-RRR.deb.
Install a package (including unpacking and configuring) onto the file system of the hard disk: dpkg --install foo_VVV-RRR.deb.
Unpack (but do not configure) a Debian archive into the file system of the hard disk: dpkg --unpack foo_VVV-RRR.deb. Note that this operation does not necessarily leave the package in a usable state; some files may need further customization to run properly. This command removes any already-installed version of the program and runs the preinst (see ) script associated with the package.
Configure a package that already has been unpacked: dpkg --configure foo. Among other things, this action runs the postinst (see ) script associated with the package. It also updates the files listed in the conffiles for this package. Notice that the 'configure' operation takes as its argument a package name (e.g., foo), not the name of a Debian archive file (e.g., foo_VVV-RRR.deb).
Extract a single file named "blurf" (or a group of files named "blurf*") from a Debian archive: dpkg --fsys-tarfile foo_VVV-RRR.deb | tar -xf - 'blurf*'.
Remove a package (but not its configuration files): dpkg --remove foo.
Remove a package (including its configuration files): dpkg --purge foo.
List the installation status of packages containing the string (or regular expression) "foo*": dpkg --list 'foo*'.

APT
APT is the Advanced Package Tool, an advanced interface to the Debian packaging system which provides the apt-get program. It provides commandline tools for searching and managing packages, and for querying information about them, as well as low-level access to all features of the libapt-pkg library. For more information, see the User's Guide in /usr/share/doc/apt-doc/guide.html/index.html (you will have to install the apt-doc package).
Starting with Debian Jessie, some frequently used apt-get and apt-cache commands have an equivalent via the new apt binary. This means some popular commands like apt-get update, apt-get install, apt-get remove, apt-cache search, or apt-cache show now can also be called simply via apt, say apt update, apt install, apt remove, apt search, or apt show. The following is an overview of the old and new commands:

 apt-get update             ->  apt update
 apt-get upgrade            ->  apt upgrade
 apt-get dist-upgrade       ->  apt full-upgrade
 apt-get install package    ->  apt install package
 apt-get remove package     ->  apt remove package
 apt-get autoremove         ->  apt autoremove
 apt-cache search string    ->  apt search string
 apt-cache policy package   ->  apt list -a package
 apt-cache show package     ->  apt show package
 apt-cache showpkg package  ->  apt show -a package

The apt tool merges functionality of apt-get and apt-cache and by default has a fancier colored output format, making it more pleasant for humans. For usage in scripts or advanced use cases, apt-get is still preferable or needed.
apt-get provides a simple way to retrieve and install packages from multiple sources using the command line. Unlike dpkg, apt-get does not understand .deb files, it works with the packages proper name and can only install .deb archives from a source specified in /etc/apt/sources.list. apt-get will call dpkg directly after downloading the .deb archives[1] from the configured sources.
[1] Notice that there are ports that make this tool available with other package management systems, like Red Hat package manager, also known as rpm
Some common ways to use apt-get are:
To update the list of packages known by your system, you can run:

apt update

(you should execute this regularly to update your package lists)
To install the foo package and all its dependencies, run:

apt install foo

To remove the foo package from your system, run:

apt remove foo

To remove the foo package and its configuration files from your system, run:

apt purge foo

To list all packages for which newer versions are available, run:

apt list --upgradable

To upgrade all the packages on your system (without installing extra packages or removing packages), run:

apt upgrade

To upgrade all the packages on your system, and, if needed for a package upgrade, installing extra packages or removing packages, run:

apt full-upgrade

(The command upgrade keeps a package at its installed obsolete version if upgrading would need an extra package to be installed, for a new dependency to be satisfied. The full-upgrade command is less conservative.)
Note that you must be logged in as root to perform any commands that modify packages.
Note that apt-get now also installs recommended packages as default, and thanks to its robustness it's the preferred program for package management from console to perform system installation and major system upgrades.
The apt tool suite also includes the apt-cache tool to query the package lists. You can use it to find packages providing specific functionality through simple text or regular expression queries and through queries of dependencies in the package management system. Some common ways to use apt-cache are:
To find packages whose description contain word:

apt search word

To print the detailed information of a package:

apt show package

To print the packages a given package depends on:

apt-cache depends package

To print detailed information on the versions available for a package and the packages that reverse-depends on it:

apt-cache showpkg package

For more information, install the apt package and read apt 8, apt-get 8, sources.list 5 and install the apt-doc package and read /usr/share/doc/apt-doc/guide.html/index.html.

aptitude
aptitude is a package manager for systems that provides a frontend to the apt package management infrastructure. aptitude is a text-based interface using the curses library. Actions may be performed from a visual interface or from the command-line.
aptitude can be used to perform management tasks in a fast and easy way. It allows the user to view the list of packages and to perform package management tasks such as installing, upgrading, and removing packages.
aptitude provides the functionality of apt-get, as well as many additional features:
aptitude offers easy access to all versions of a package.
aptitude makes it easy to keep track of obsolete software by listing it under "Obsolete and Locally Created Packages".
aptitude includes a fairly powerful system for searching particular packages and limiting the package display. Users familiar with mutt will pick up quickly, as mutt was the inspiration for the expression syntax.
aptitude can be used to install the predefined tasks available. For more information see tasksel.
aptitude in full screen mode has su functionality embedded and can be run by a normal user. It will call su (and ask for the root password, if any) when you really need administrative privileges.
You can use aptitude through a visual interface (simply run aptitude) or directly from the command line. The command line syntax used is very similar to the one used in apt-get. For example, to install the foo package, you can run aptitude install foo.
Note that aptitude is the preferred program for daily package management from the console.
For more information, read the manual page aptitude 8 and install the aptitude-doc package.

synaptic
synaptic is a graphical package manager. It enables you to install, upgrade and remove software packages in a user friendly way. Along with most of the features offered by aptitude, it also has a feature for editing the list of used repositories, and supports browsing all available documentation related to a package. See the Synaptic Website for more information.

tasksel
When you want to perform a specific task it might be difficult to find the appropriate suite of packages that fill your need. The Debian developers have defined tasks, a task is a collection of several individual Debian packages all related to a specific activity. Tasks can be installed through the tasksel program or through aptitude.
Typically, the Debian installer will automatically install the task associated with a standard system and a desktop environment. The specific desktop environment installed will depend on the CD/DVD media used, most commonly it will be the GNOME desktop (gnome-desktop task). Also, depending on your selections throughout the installation process, tasks might be automatically installed in your system. For example, if you selected a language other than English, the task associated with it will be installed automatically too.

Other package management tools

dpkg-deb
This program manipulates Debian archive (.deb) files. Some common uses are:
Find out all the options: dpkg-deb --help.
Determine what files are contained in a Debian archive file: dpkg-deb --contents foo_VVV-RRR.deb
Extract the files contained in a named Debian archive into a user specified directory: dpkg-deb --extract foo_VVV-RRR.deb tmp extracts each of the files in foo_VVV-RRR.deb into the directory tmp/. This is convenient for examining the contents of a package in a localized directory, without installing the package into the root file system.
Extract the control information files from a package: dpkg-deb --control foo_VVV-RRR.deb tmp.
Note that any packages that were merely unpacked using dpkg-deb --extract will be incorrectly installed, you should use dpkg --install instead.
More information is given in the manual page dpkg-deb 1.

Debian claims to be able to update a running program; how is this accomplished?
The kernel (file system) in systems supports replacing files even while they're being used.
We also provide a program called start-stop-daemon which is used to start daemons at boot time or to stop daemons when the runlevel is changed (e.g., from multi-user to single-user or to halt). The same program is used by installation scripts when a new package containing a daemon is installed, to stop running daemons, and restart them as necessary.

How can I tell what packages are already installed on a Debian system?
To learn the status of all the packages installed on a Debian system, execute the command

dpkg --list

This prints out a one-line summary for each package, giving a 2-letter status symbol (explained in the header), the package name, the version which is installed, and a brief description.
To learn the status of packages whose names match any pattern beginning with "foo", run the command:

dpkg --list 'foo*'

To get a more verbose report for a particular package, execute the command:

dpkg --status packagename

How do I display the files of an installed package?
To list all the files provided by the installed package foo execute the command

dpkg --listfiles foo

Note that the files created by the installation scripts aren't displayed.

How can I find out what package produced a particular file?
To identify the package that produced the file named foo execute either:
dpkg --search foo
This searches for foo in installed packages. (This is (currently) equivalent to searching all of the files having the file extension of .list in the directory /var/lib/dpkg/info/, and adjusting the output to print the names of all the packages containing it, and diversions.)
A faster alternative to this is the dlocate tool.

dlocate -S foo

zgrep foo Contents-ARCH.gz
This searches for files which contain the substring foo in their full path names. The files Contents-ARCH.gz (where ARCH represents the wanted architecture) reside in the major package directories (main, non-free, contrib) at a Debian archive site (i.e. under /debian/dists/). A Contents file refers only to the packages in the subdirectory tree where it resides. Therefore, a user might have to search more than one Contents files to find the package containing the file foo.
This method has the advantage over dpkg --search in that it will find files in packages that are not currently installed on your system.
apt-file search foo
If you install the apt-file package, similar to the above, it searches files which contain the substring or regular expression foo in their full path names. The advantage over the example above is that there is no need to retrieve the Contents-ARCH.gz files as it will do this automatically for all the sources defined in /etc/apt/sources.list when you run (as root) apt-file update.

Why is "foo-data" not removed when I uninstall "foo"? How do I make sure old unused library-packages get purged?
Some packages are split in program ("foo") and data ("foo-data") (or in "foo" and "foo-doc"). This is true for many games, multimedia applications and dictionaries in Debian and has been introduced since some users might want to access the raw data without installing the program or because the program can be run without the data itself, making "foo-data" optional.
Similar situations occur when dealing with libraries: generally these get installed since packages containing applications depend on them. When the application-package is purged, the library-package might stay on the system. Or: when the application-package no longer depends upon e.g. libdb4.2, but upon libdb4.3, the libdb4.2 package might stay when the application-package is upgraded.
In these cases, "foo-data" doesn't depend on "foo", so when you remove the "foo" package it will not get automatically removed by most package management tools. The same holds true for the library packages. This is necessary to avoid circular dependencies. However, if you use apt-get (see APT) or aptitude (see aptitude) as your package management tool, they will track automatically installed packages and give the possibility to remove them, when no packages making use of them remain in your system.