Skip to content

CLI Tools

There are various CLI tools to interact with packages in Debian, either for installing, upgrading, removing, or to interact in more detail with them

APT vs. DPKG

The major difference in packaging lies between the purpose of APT and DPKG.

DPKG is the low-lever package manager that takes care about:

  • Validating and unpacking a package archive
    • Dependencies
    • File ownership
    • Consistency
  • Configuring the package
    • Writing or updating config files
    • Starting / controlling services
    • Custom scripts

APT is the main user-CLI interface to work with, it will help you:

  • Downloading repository meta-data and validating signatures
  • Downloading and installing packages
  • Resolving all dependencies and including them in any action
  • Updating all packages on your system
  • Removing and cleaning up after packages

Under the hood APT will control DPKG to execute all actions in the proper order.

Summary: Use APT for the daily business, only touch dpkg when something special needs to be done.

APT Tools

The APT package manager currently has 3 major interfaces.

  • apt which is the simplest CLI tools that offers most of the actions in one binary
  • aptitude offers a CLI based UI to search and manipulate packages on your system
  • apt-* additional APT binaries that are the legacy interface (like apt-get or apt-cache)

Other graphical interfaces also exist, but we keep working on the command-line here.

Learning by example

To learn the basics, just try to execute the examples, and follow whats happening on your test system.

Update cache

APT has a local cache for all meta-data, this needs to be current so APT knows all packages and can determine what is needs to be updated.

Sometimes this is run automatically in background, but by default on a basic system, it is not.

sudo apt update

You should see:

  • Meta data being checked and downloaded from repositories
  • Rebuilding of package indices
  • Count of packages that can be updated

Searching and inspecting

You can search the package meta data, with description, regexp or just the package names:

sudo apt search archimedes constant
sudo apt search "^pi$"
sudo apt search --names-only icinga2

You should see: Listings of known packages matching your search

To inspect a package in more detail you can use the show command:

sudo apt show pi
sudo apt show icinga2

You should see:

  • Version and Description
  • Dependencies
  • Maintainer
  • Various other meta data

Install a package

To install a package with all its dependencies you just need to run:

sudo apt install pi

If dependencies need to be installed, APT will ask you about it.

You should see:

  • APT downloading the package from a remote server
  • Installing the packages in the proper order
  • Output after downloading is mainly generated by DPKG, which is doing the local work

Listing packages

You can list packages with some filter, for example installed packages, or those available for update.

sudo apt list --installed
sudo apt list --upgradable

Update your system

To update your system there are two slightly different commands:

sudo apt upgrade
sudo apt full-upgrade
  • upgrade is called the safe upgrade, meaning it will update packages while possibly install new dependencies, but never remove anything.
  • full-upgrade or dist-upgrade was mainly used for running major distribution updates, but it can be used anytime
    • This will in addition to upgrading, also remove obsolete or no longer compatible packages

But no matter which command you use, it will always show you what will be done, and wait for a confirmation.

Cleaning up cache

After larger updates you can cleanup cached downloads of APT:

sudo apt autoclean
sudo apt clean

Question: What is the difference between those commands? See man apt-get

Removing no longer used packages

APT can also help you remove no longer needed packages. It knows which packages have been installed manually (by user action), and those who were installed for dependency.

sudo apt remove pi
sudo apt autoremove

You should see:

  • APT will give you a notice about no longer required packages on apt remove pi
  • autoremove will then uninstall the packages after confirmation

Purging packages

To completely remove a package and its data, you should purge it instead of remove. This will ensure config and possible other data in /var will also be removed, also very helpful for reinstalling a package for a fresh start.

sudo apt purge pi
sudo apt remove --purge pi

Also check dpkg -L <package> after removing a package to see left over files from the package.

APT History

A full log of actions with APT is stored in /var/log/apt/history.log.

Start-Date: 2021-04-08  13:54:28
Commandline: apt install finger
Requested-By: user (1000)
Install: finger:amd64 (0.17-17)
End-Date: 2021-04-08  13:54:31

This will keep details on installs, upgrades, or any other action for up to 12 months. See /etc/logrotate.d/apt to change rotation settings.

Configuration files

DPKG remembers all files installed to /etc as config files, it can ask you if you want to keep your changes or replace the content on an upgrade.

Upgrade will store the new config as .dpkg-new or the old version as.dpkg-old, depending on your choice.

When a user removed a configuration file completely, it won't be put back, unless you purge and re-install the package.

Inspecting with DPKG

DPKG can also give you information about packages that have ever been installed on the local system, or names DPKG has seen in dependencies:

dpkg -l
dpkg -l pi
dpkg -l "lib*"

You can see the contents of a package:

dpkg -L bash

Or see which package a file belongs to:

dpkg -S /usr/bin/apt

Online Resources

Debian has web tools to search for packages or bugs in them.

Further documentation