Managing packages with apt

Table of contents

Linux distributions like debian or those based on it, like Mint or Ubuntu, use deb files to package programs and the apt package manager to to deal with them. While the apt command is very simple to use, it is important to understand it properly to not be surprised by unused packages or outdated package information.

The command confusion: dpkg, apt-get, aptitude and apt

When first starting to look into package managers, a lot of confusing commands come up. Here is how they differ:

  • dpkg: This is the basic low-level package manager the other commands use. It is very simple, only capable of installing local .deb files and managing the installed ones.
  • apt-get: Comes with several commands. apt-cache maintains a list of packages installable from remote sources, apt-get ist used to install them, will automatically resolve dependencies and let's you upgrade installed packages to newer versions. It contains more commands like apt-show-versions or apt-config to provide the full range of functionality.
  • apt: Is the new and improved version of the apt-get toolchain. It is significantly more user-friendly to use and combines the functionality of all the seperate commands that apt-get contained. It is backwards-compatible with their syntax while also providing renamed commands and refined behaviour to better automate and abstract the package management process.
  • aptitude: Is largely equivalent to apt but uses an interactive terminal interface instead. It is slightly older than apt, not always installed by default and not as easy to use in automation scripts.

Finding packages and versions

Before we can even install a package, we first need to find one. Let's install the htop command, a more user-friendly version of the top command to check system utilization.

Before we can search for packages, we should update the package list so we have the most recent information about package names and versions:

apt update

Now that we have a copy of what packages can be installed, searching for it is really simple:

apt search htop

This lists all packages containing our keyword "htop". If we get multiple results, we can get more information by using apt show with the package name:

apt show htop

The output will contain more information about the package, like it's description, maintainer and dependencies:

Version: 3.2.2-2
Priority: optional
Section: utils
Maintainer: Daniel Lange <>
Installed-Size: 387 kB
Depends: libc6 (>= 2.34), libncursesw6 (>= 6), libnl-3-200 (>= 3.2.7), libnl-genl-3-200 (>= 3.2.7), libtinfo6 (>= 6)
Suggests: lm-sensors, lsof, strace
Tag: admin::monitoring, implemented-in::c, interface::text-mode,
 role::program, scope::utility, uitoolkit::ncurses, use::monitor,
Download-Size: 152 kB
APT-Sources: bookworm/main amd64 Packages
Description: interactive processes viewer

On a side note, there is an undocumented command apt info, which is simply an alias for apt show. You should prefer apt show as it is the official command stated in documentation and the other may be removed without warning in the future.

Installing and removing packages

Once we have found the package we were looking for, installing it is trivial:

apt install htop -y

The -y flag will skip the confirmation prompt asking if we are sure we want to install the package and it's dependencies.

If we decide we don't want the package anymore, we can remove it just as easily:

apt remove htop -y

Removing packages will delete the package itself from your harddrive, but leave small or modified configuration files untouched, as you may be reinstalling the package or it might have been removed by accident. If you want to delete those as well, you can run

apt purge htop -y

This will remove the package and all files related to it, even if modified by the user. You can use this instead of apt remove to get rid of the entire package, or call it even on already removed packages to clean leftover files.

If you ever encounter a broken package, for example because you accidentally deleted some of it's files or dependencies, you can quickly reinstall it to fix the issue:

apt reinstall htop

Getting package updates

To upgrade packages, it is always advisable to first refresh the local list of packages so we have the latest information about package versions available:

apt update

To see a list of upgradable packages (ie installed packages that have a newer version available), run:

apt list --upgradable

Now, upgrading installed packages that have new versions is just a single command:

apt upgrade -y

A thought to note is that in case of dependency conflicts, the apt upgrade command will simply skip the package. To forcibly upgrade all packages and allow it to remove dependency packages if required, make a full upgrade instead:

apt full-upgrade

Note: This used to be called dist-upgrade in apt-get and an alias for it still exists in the apt command, but using apt full-upgrade is the better choice as the alias may be removed in the future.

You should run package upgrades regularly or automate them with tools like unattended-upgrades.

More fine-grained control over packages

In some situations, the default behaviour of apt may not be exactly what you want. You may for example want to install a specific version instead of the most recent one. You can check all available versions of a package by running

apt list --all-versions python3

When you have found the version you want, you can add it to the package name separated by a = character during install:

apt install python3=3.11.2--1+b1

This will install the specific package version 3.11.2--1+b1 of the python3 package.

To check what packages and versions are currently installed, you can use apt list as well:

apt list --installed

Occasionally, you may want to upgrade a package if it is installed, but not install it in case it is not:

apt install --only-upgrade python3

Or you may want to install a package if it does not exist, but not upgrade it in case it is already installed:

apt install --only-install python3

Cleanup and maintenance commands

When a package releases to a new version, it may not need a prior dependency anymore. Simply upgrading the package will install the new version, but also leave the old, now unrequired dependency, installed as well. To free the space used by those packages and have them automatically removed, run:

apt autoremove -y

You may have seen commands like apt-cache clean or apt-cache autoclean in some tutorials out there. Those are outdated and, while apt has aliases for them, they are undocumented because it will manage cache cleaning automatically when installing/removing packages. You do not need to call these commands on your own anymore if you regularly upgrade packages.

More articles

Enabling automatic background updates with unattended-upgrades

Keep your debian-based servers up to date automatically

Sharing compiled go code libraries

Ever wondered how to create dll/shared object files in go, like other compiled languages can?

Finding files in linux

Hunting for files an conditional transforming made easy

Setting up an nginx load balancer

Distributing requests to multiple servers

Simplifying terminal operations with python modules

Saving time with python builtin modules

A complete guide to running windows software on linux

Making windows programs behave like they were written for linux