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-getis 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 forapt-get:
apt-cache / high level package query utlity
apt-cacheprovides an interface to perform read-only operations on the APT package cache.apt-cachedoes 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 forapt-cache:
dpkg / low level package manager for Debian
dpkgis a tool for installing, removing, and querying individual packages. We’ll investigate some common commands and go over some basic usage ofdpkgin a couple of real-world examples.
apt-gethigh level package handling utility
Installing a Debian package:
apt-getwants you to pass the[package-name]you wish to install, for example:
Removing a Debian package:
The following will remove a packagewithoutremoving its configuration files:
To remove a packageandits configuration files, usepurge:
or alternatively, use the--purgeflag 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.listand/etc/apt/sources.list.d/.
Upgrade all debian system packages:
This command installsallof 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 runapt-get updateto update repository metadata, you can update an installed package by runningapt-get install
If you need to force reinstall a package, just pass the--reinstallflag
By passing the--reinstallflag, 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:
Thecleancommand clears out the local repository of downloaded package files. It removes everythingexceptthepartials folderandlock filefrom/var/cache/apt/archives/.
Useapt-get cleanto free up disk space when necessary, or as part of regularly scheduled maintenance.
Remove useless files from the APT cache:
autocleanis another method used to clear out the local repository of downloaded package files, just likeclean. The difference betweencleanandautocleanis that the latter only removes package files that can no longer be downloaded from their sources, and are very likely to be useless.
apt-cachehigh 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 thename,description, andprovidesfields 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 usingdpkgwithoutremoving its configuration files:
alternatively, use the-rflag:
To remove a package usingdpkgalong withits corresponding configuration files, use the--purgecommand:
List available system packages:
dpkg -lallows 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 fromdpkg -lthen all packages in/var/lib/dpkg/statuswill 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 usingdpkg -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--contentsflag.
returns the following results:
When the--contentsflag is used,dpkgcalls down to an action provided by another tooldpkg-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.
Show package information:
You can show package metadata of installed packages by usingdpkg -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 withapt-get,apt-cache, anddpkg. You can learn more about the tools mentioned in this blog post by reading the man pages: