APT is a set of core tools found inside the Debian operating system. It provides utilities for the installation and removal of software packages and dependencies on a system.
apt-get / high level package handling utility
apt-get is the command-line tool for handling packages and provides functions such as installing, removing, and updating packages on a system with a single operation. We’ll cover the following commands for apt-get:
install and --reinstall
purge or --purge
clean and autoclean
apt-cache / high level package query utlity
apt-cache provides an interface to perform read-only operations on the APT package cache. apt-cache does not change the state of the system, but allows the user to extract useful information from package metadata.
We’ll go over the following commands for apt-cache:
dpkg / low level package manager for Debian
dpkg is a tool for installing, removing, and querying individual packages. We’ll investigate some common commands and go over some basic usage of dpkg in a couple of real-world examples.
--list or -l
apt-get high level package handling utility
Installing a Debian package:
apt-get wants you to pass the [package-name] you wish to install, for example:
Removing a Debian package:
The following will remove a package without removing its configuration files:
To remove a package and its configuration files, use purge:
or alternatively, use the --purge flag on the remove command:
Update package index files from sources.list:
When this command is run, all available packages are fetched and re-indexed from the locations specified in /etc/apt/sources.list and /etc/apt/sources.list.d/.
Upgrade all debian system packages:
This command installs all of the latest versions of each package installed on the system and is, generally, not recommended to be run on production systems.
Update / Reinstall a single package:
Once you’ve run apt-get update to update repository metadata, you can update an installed package by running apt-get install
If you need to force reinstall a package, just pass the --reinstall flag
By passing the --reinstall flag, you will effectively force the package to be reinstalled even if it’s already installed and at the latest version. This will completely remove the package from the system* and reinstall it.
*Packages that depend on the [package-name] being reinstalled will not be removed from the system
APT cache files:
APTs cached files are located in:
Clear the APT cache:
The clean command clears out the local repository of downloaded package files. It removes everything except the partials folder and lock file from /var/cache/apt/archives/.
Use apt-get clean to free up disk space when necessary, or as part of regularly scheduled maintenance.
Remove useless files from the APT cache:
autoclean is another method used to clear out the local repository of downloaded package files, just like clean. The difference between clean and autoclean is that the latter only removes package files that can no longer be downloaded from their sources, and are very likely to be useless.
apt-cache high level package query utility
List all available packages:
This command will output a list of available package names for your system:
Searching for a specific debian package:
This is really useful in case you don’t know the exact [package-name], but rather a description of what that package does; for example “Network Security”:
This will return a list of packages containing the string “Network Security” in the package description. Using apt-cache will look in the name, description, and provides fields of the available packages by default.
Show debian package information:
This will show apt metadata for the [package-name] given. This is an example using the “screen” package:
To remove a package using dpkgwithout removing its configuration files:
alternatively, use the -r flag:
To remove a package using dpkgalong with its corresponding configuration files, use the --purge command:
List available system packages:
dpkg -l allows you to list a set of packages on the system and the state of those packages:
You can use a regular expression to list information about all matching package names. For example:
will return all packages starting with the letters “re”:
The first column shows the state of the package. You can learn more about package states by reading the dpkg man page: man 1 dpkg.
If the [package-name-pattern] is omitted from dpkg -l then all packages in /var/lib/dpkg/status will be listed, excluding packages that have been marked not-installed
will output something like:
List files in a package:
dpkg maintains a list of packages that are installed on a system in /var/lib/dpkg.
You can query the files in an installed package using dpkg -L:
So, for example:
returns the following results:
If you’d like to list the files in a debian package that you’ve downloaded (but not installed), you can use the --contents flag.
returns the following results:
When the --contents flag is used, dpkg calls down to an action provided by another tool dpkg-deb, which provides tools to manipulate a debian package archive.
Show packages containing a filename or filepath:
For example, passing a specific filepath:
will return all the package names that contain that file path.
Create an APT repository in less than 10 seconds, free.
Show package information:
You can show package metadata of installed packages by using dpkg -s:
for example, the following command:
returns the package metadata:
Getting more familiar with your package manager’s tools can help you be more productive when finding, installing, and querying packages.
We highly recommend that users of production Debian and Ubuntu systems become familiar with apt-get, apt-cache, and dpkg. You can learn more about the tools mentioned in this blog post by reading the man pages: