Debian Package Management

apt tools (apt, apt-get, apt-cache and etc.) are fast, aptitude has some additional useful features.

Update Database

Update information about the packages from apt sources (/etc/apt/sources.list):

# apt update
# aptitude update

Install

Simulate package installation:

# apt -s install ⟨package⟩

Note:
apt-get asks for confirmation only when it needs to install packages which weren't specified on the command line.

Install a package without installing recommended packages:

# apt install --no-install-recommends ⟨package⟩

List Updates

List available updates:

$ apt list --upgradable   or
$ aptitude search '~U'   or
$ apt-show-versions -u

Upgrade a Specific Package

If the package was installed automatically, # apt install ⟨package⟩ marks the package as installed manually. To upgrade the package and do not change the status (auto/manual):

# apt install --upgrade

Restart Daemons after Library Upgrades

Package needrestart checks which daemons need to be restarted after library upgrades

Changelog

$ apt changelog ⟨package⟩

List Bugs

To list critical bugs (critical, grave and serious):

$ apt-listbugs list ⟨package⟩

Find Package

$ apt list --installed ⟨pattern⟩

$ dpkg --get-selections ⟨pattern⟩

Look for packages with following ⟨pattern⟩ in package names:
$ dpkg --list ⟨pattern⟩

Find packages named file :
$ aptitude search '?exact-name(file)'

Find installed packages containing the keyword in their description:
$ aptitude search ~i~d⟨keyword⟩

Look for obsolete packages (installed packages that are not included in any repository):
$ aptitude search ~o

List manually installed packages:
$ aptitude search '?installed ?not(?automatic)'

Find installed packages from contrib and non-free repositories:
$ aptitude search '~i ~scontrib/.*' '~i ~snon-free/.*'

If you installed several multimedia packages from deb-multimedia.org (unofficial) repository, to list that packages:
$ aptitude search ~i"?origin(multimedia)"

Show Dependencies

$ aptitude why ⟨package⟩
search for packages that require the given package.

$ apt-cache --installed --important rdepends ⟨package⟩

Note:
apt-cache depends/rdepends without options lists dependencies of any package (installed and not installed). The packages are listed without showing information about their state.

For recursive dependencies:
$ apt-rdepends -r --state-follow=Installed --state-show=Installed ⟨package⟩

To find the dependencies of installed package:
$ apt-cache --installed depends ⟨package⟩

Show Versions

$ apt-show-versions -a ⟨package⟩
list available versions.

List Content of Package

Show list of files (installed packages only):
$ dpkg --listfiles ⟨package⟩

List content of any package:
$ apt-file list ⟨package⟩

List files of local deb-file:
$ dpkg -c ⟨path-to-deb-file⟩

Find File Owner

Search for installed package owning file:
$ dpkg -S ⟨file⟩

Searching for Files

To update information about the contents of all available packages:
$ apt-file update

Search for any known file:
$ apt-file search ⟨file⟩

Count Installed Packages

$ dpkg --get-selections | wc -l

List Installed Packages Sorted by Size

$ dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1n

aptitude Pending Actions

To check if aptitude has some pending actions:
$ aptitude search '?or(?action(install), ?action(upgrade), ?action(downgrade), ?action(remove), ?action(purge))'

To cancel all pending actions:
# aptitude keep-all

To remove only some packages from the pending list:
# aptitude keep ⟨packages⟩

Install History

/var/log/apt/history.log

Configuration Files of Removed Packages

Removing packages (apt remove) keeps configuration files, that is useful for the case: if you reinstall them, you do not need to redo the configuration again. If you prefer to remove configuration files, to find all packages in the "config-state":

# aptitude search ~c

Or, if you have installed dctrl-tool:

$ grep-status -n -sPackage -FStatus config-files

To list configuration files of package :

$ dpkg-query --showformat='${Conffiles}\n' --show ⟨package⟩

To remove all configuration files:

# aptitude purge ~c

Get rid of useless configuration files

Clean Local Cache

apt maintains a local cache of downloaded/installed .deb packages at /var/lib/apt/cache/. To remove the packages which can no longer be downloaded from the cache:

# apt autoclean

Delete cached package files - Debian Wiki

Download Package Source

To be able to download the package sources, a repository including sources has to be added into /etc/apt/sources.list:

deb-src http://ftp.debian.org/debian/ unstable main contrib non-free

To download the source:

$ apt-get --download-only source ⟨package-name⟩

Info on Locally Saved DEB-File

To view information on locally saved deb-file:
$ dpkg -I ⟨path-to-deb-file⟩

Manually Download an Older Package

Some older versions of packages can be manually downloaded from Debian Pool.

Essential Packages

The packages having priority essential are absolutely necessary for the proper functioning of the system. They can not be removed [Debian FAQ]. To list them:

$ dpkg-query -Wf '${Package;-40}${Essential}\n' | grep yes

Additional Info