Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Manage packages with APT

By updated

Improve this page

On Debian, Ubuntu and their derivatives, APT is the tool that installs, updates and removes software, handling dependencies automatically. This guide covers the everyday commands, how repositories work (with the newer deb822 format), the safe addition of a third-party repository through a GPG key, and the troubleshooting of frequent errors. It is written for beginner to intermediate administrators on Debian/Ubuntu. For the other families, see dnf (Red Hat, Fedora), Pacman (Arch) or APK (Alpine).

What you will learn

  • Use the essential APT commands (install, update, remove)
  • Understand repositories and the modern deb822 format
  • Add a third-party repository with its GPG key, without apt-key
  • Pin a version and manage priorities
  • Troubleshoot common APT errors

Prerequisites

  • A Debian or Ubuntu machine (the examples use Debian 13 "trixie" and Ubuntu 24.04)
  • An account with sudo rights
  • Some basics of the Linux command line

apt or apt-get: which one to use

Two families of commands coexist and cause confusion. apt is the modern, friendly interface, designed for interactive use: readable output, a progress bar, colours since APT 3.0 (Debian 13). apt-get and apt-cache are the historical commands, with a stable output.

The rule is simple: apt at the keyboard, apt-get in scripts. APT reminds you itself if you script apt:

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

In other words, the output of apt can change from one version to the next: a script that parses it will break. In a playbook or a Dockerfile, prefer apt-get install -y.

The essential APT commands

Here are the commands you will use every day. Each one acts on the local cache of metadata or on the installed packages.

  • Refresh the package list:

    Terminal window
    sudo apt update

    This command queries the repositories and downloads the metadata of the available versions, without installing anything. It is the prerequisite for any installation. Important: apt update does not update your software, only the local cache.

  • Update the installed packages:

    Terminal window
    sudo apt upgrade

    Updates the installed packages to their latest version, without removing any or adding new ones.

  • Update, including dependency changes:

    Terminal window
    sudo apt full-upgrade

    More thorough than upgrade, it agrees to remove or add packages to resolve dependencies. Useful during a version upgrade, and the summary deserves a careful read before you confirm.

  • Install a package (and its dependencies automatically):

    Terminal window
    sudo apt install nginx

    To force a specific version, first list the available versions, then pass the one you want after an =:

    Terminal window
    apt list -a nginx # versions offered by the repositories
    sudo apt install nginx=1.26.3-3+deb13u9 # version recorded on Debian 13, 30 September 2026
  • Remove a package, keeping its configuration or not:

    Terminal window
    sudo apt remove nginx # keeps the files in /etc
    sudo apt purge nginx # also removes the configuration
  • Remove dependencies that are no longer needed:

    Terminal window
    sudo apt autoremove
  • Search for and inspect a package:

    Terminal window
    apt search <keyword> # searches the local cache
    apt show nginx # version, size, dependencies, description
  • Clean the downloaded archives:

    Terminal window
    sudo apt clean # removes all of /var/cache/apt/archives/
    sudo apt autoclean # removes only the obsolete .deb files

How APT finds packages: repositories

APT downloads packages from repositories declared in configuration files. Two formats coexist today.

The modern deb822 format (.sources)

The deb822 format stores repositories in .sources files under /etc/apt/sources.list.d/, easier to read and to automate. Ubuntu 24.04 uses it by default (ubuntu.sources), as do the official Debian images since Debian 12. On a Debian 13 image, recorded on 30 September 2026, the file /etc/apt/sources.list.d/debian.sources holds two blocks, one for the distribution, the other for the security fixes:

Types: deb
URIs: http://deb.debian.org/debian
Suites: trixie trixie-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp
Types: deb
URIs: http://deb.debian.org/debian-security
Suites: trixie-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

Each field is explicit: Types (deb for binaries), URIs (the server), Suites (the release, here trixie, and its variants), Components (the sections, to which you add contrib or non-free-firmware when needed) and above all Signed-By, which ties the repository to its signing key. That last field is what secures the chain.

The old one-line format (sources.list)

The historical format, one line per repository in /etc/apt/sources.list, is still accepted:

deb http://deb.debian.org/debian trixie main contrib non-free-firmware

You still come across it everywhere, but deb822 is gradually replacing it. For a new repository, prefer the .sources format.

A useful clarification, because many articles exaggerate it: the one-line format is deprecated, not removed, and the Debian 13 manual page announces a removal not before 2029. There is no hurry to convert an existing server. When you decide to do it, APT 3.0 provides the apt modernize-sources command, which converts the files and backs up the originals. It is covered, with its limits, in the Debian 13 "Trixie" guide (in French).

Suites and components: two different axes

A repository is split along two axes that are easy to mix up. The suite names the release of the distribution and its update streams; the component sorts software by licence or maintenance model. Security fixes and backports are therefore suites, not components:

AxisValueContent
Suitetrixie (Debian 13), noble (Ubuntu 24.04)the distribution as released
Suitetrixie-updates, noble-updatesfixes published after the release
Suitetrixie-security, noble-securitysecurity updates
Suitetrixie-backports, noble-backportsrecent versions backported to stable
Debian componentmain, contrib, non-free, non-free-firmwaresupported free software, free software depending on non-free parts, non-free, firmware
Ubuntu componentmain, restricted, universe, multiversesupported by Canonical, proprietary drivers, community, community non-free

After any change to the sources, reload the index:

Terminal window
sudo apt update

Add a third-party repository safely

Installing software missing from the official repositories (Docker, Node.js, etc.) requires adding a third-party repository. The golden rule: always verify the signature of the packages with the publisher's GPG key.

The correct method takes two steps. First, download the publisher's key into a dedicated keyring:

Terminal window
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
| sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg

gpg --dearmor converts the key to the expected binary format. Then declare the repository in deb822, pointing Signed-By at that keyring:

/etc/apt/sources.list.d/nodesource.sources
Types: deb
URIs: https://deb.nodesource.com/node_22.x
Suites: nodistro
Components: main
Signed-By: /usr/share/keyrings/nodesource.gpg

A sudo apt update validates the configuration: if the key matches, the repository's packages become available; otherwise APT rejects the repository rather than install an unverified package.

apt-add-repository and PPAs (Ubuntu)

On Ubuntu and its derivatives, the apt-add-repository command (package software-properties-common) automates adding a repository, in particular the PPAs hosted on Launchpad:

Terminal window
sudo apt-add-repository ppa:graphics-drivers/ppa
sudo apt update

To remove a repository or a PPA, add --remove. PPAs do not exist on standard Debian; only add PPAs from trusted publishers, because they have the same level of access as the official repositories.

Control versions

APT lets you freeze a version or arbitrate between several sources, which is precious on a production server.

See which version will be installed with apt policy

Terminal window
apt policy bash
bash:
Installed: 5.2.37-2+b9
Candidate: 5.2.37-2+b9
Version table:
*** 5.2.37-2+b9 500
500 http://deb.debian.org/debian trixie/main amd64 Packages
100 /var/lib/dpkg/status

Installed is the version present, Candidate the one that would be installed, and the table lists the available versions with their priority and origin. Indispensable to understand why APT picks a given version.

Force a priority with pinning

In /etc/apt/preferences.d/, a preferences file imposes a priority on a source. Example to favour Node.js from the NodeSource repository:

/etc/apt/preferences.d/nodejs
Package: nodejs
Pin: origin deb.nodesource.com
Pin-Priority: 600

A Pin-Priority above 500 makes the source preferred; from 1000 upwards, the apt_preferences(5) manual even allows going back to an older version (downgrade), and common practice writes 1001. Handle with care: a badly tuned pin can block security updates.

Lock a package with apt-mark

To prevent a package from being updated (for instance a kernel or a validated database):

Terminal window
sudo apt-mark hold postgresql-17 # locks
apt-mark showhold # lists the locked packages
sudo apt-mark unhold postgresql-17 # unlocks

Security and good practice

Package management is a sensitive point in a server's security. A few rules limit the risks.

  • Apply updates regularly. A frequent sudo apt update && sudo apt upgrade applies the security fixes. On an exposed server, this is not negotiable.

  • Only add trusted repositories. Every third-party repository can install code with root rights. Check the publisher and the GPG key, and limit the number of external sources.

  • Automate security fixes with the unattended-upgrades package, which applies critical updates on its own:

    Terminal window
    sudo apt install unattended-upgrades
    sudo dpkg-reconfigure --priority=low unattended-upgrades
  • Version /etc/apt/. Before a big operation, back up your repository configuration. The etckeeper tool (in French) automatically versions /etc in a Git repository (in French), which lets you roll back after a risky change.

Troubleshooting

Even when used well, APT can get stuck. Here are the most frequent errors and their fix.

SymptomLikely causeSolution
dpkg was interruptedInterrupted installationsudo dpkg --configure -a
Unmet dependenciesBroken packagessudo apt --fix-broken install
Corrupted cache, read errorsDamaged archivessudo apt clean && sudo apt update
NO_PUBKEY on updateRepository GPG key missingReinstall the key as a Signed-By keyring
404 Not Found on updateInvalid URL or suite in the sourcesFix the .sources or the sources.list
E: Unable to locate package <name> and apt policy <name> completely silentThe component index (often universe) was never downloadedsudo apt update, and check that the component is listed in the sources
Package impossible to removePackage in an unstable statesudo dpkg --remove --force-remove-reinstreq <package>

To understand the origin of an incident, the APT logs trace every operation:

Terminal window
cat /var/log/apt/history.log # packages installed, updated, removed
cat /var/log/apt/term.log # detailed output of the operations

Hands-on practice

Put these moves into practice on a disposable VM: install a package with apt, freeze it with a hold so that an update ignores it, and identify which package owns a file with dpkg.

Key points

  • apt at the keyboard, apt-get/apt-cache in scripts (stable interface).
  • apt update refreshes the cache; apt upgrade updates the installed packages.
  • Repositories are now declared in deb822 (.sources), with a Signed-By field.
  • apt-key is gone since APT 3.0: a GPG key goes into a dedicated keyring, referenced by Signed-By.
  • apt policy shows which version will be installed; apt-mark hold locks a package.
  • A NO_PUBKEY is solved by reinstalling the key, never with apt-key.

Knowledge check

Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn