Home / Alt manpages / apt(8)

  • apt(8)
  • Admin command
  • linux

A safe APT workflow for updating and installing Debian packages

You will finish with a repeatable way to inspect a package, refresh APT's package indexes, install a selected package, and apply upgrades while keeping destructive decisions visible. The examples match APT 2.8.3 on amd64, installed from the Ubuntu Noble updates repository. Allow 15 minutes for a normal update and one package installation.

You need a shell, a working package source configuration, and an account allowed to use sudo for changes under the system package database. Reading package information is normally unprivileged. Keep a terminal open for the command output: APT's proposed actions are the safety check, not background noise.

1. Confirm the APT version and package state

Start by checking what is installed and which architecture APT is using:

$ apt --version
apt 2.8.3 (amd64)
$ apt list --installed apt
Listing...
apt/noble-updates,noble-updates,now 2.8.3 amd64 [installed]

Your repository suite and package version may differ. The first command matters because apt is an interactive end-user interface and can change behaviour between versions. The manpage recommends the more stable apt-get and apt-cache interfaces for scripts. Use apt at the prompt when its readable output and command set are useful; use a dedicated tool when writing automation.

Checkpoint

Record the version before troubleshooting a command copied from another system. A flag documented for a later APT release is not automatically available here.

2. Inspect before changing anything

Use search to find package names and show to inspect metadata, dependencies, and download information. Neither command installs or removes a package:

$ apt search '^curl$'
$ apt show curl

The search pattern is a regular expression. Anchoring it with ^ and $ asks for the exact package name, which avoids a long list of similarly named results. If the result is empty or stale, refresh the indexes in the next step. For an installed package, apt list --installed NAME is a quick state check; replace NAME with the package you are investigating.

Before removing anything, run apt show NAME and read the proposed package changes. Package removal leaves most modified system configuration files behind, while purge removes packaged configuration files too. Neither operation removes data in a user's home directory, but application data can still live elsewhere, so inspect the package documentation and service state first.

3. Refresh package indexes

Run the update operation with elevated privileges:

$ sudo apt update

update downloads package information from every configured source. It does not upgrade installed packages. A successful run ends with a summary of fetched data and package lists; a mirror, DNS, signature, or repository configuration error leaves the indexes partly or wholly stale.

Verify the result by asking for packages that can be upgraded:

$ apt list --upgradable

An empty list is a valid result. The command may print a notice that its interface is not stable for scripting; that notice is a reason to avoid parsing it in automation, not evidence of failure. A non-zero exit status indicates an error. APT documents status 0 for normal operation and decimal 100 for an error.

Checkpoint

Do not continue to installation if sudo apt update reported repository errors. Fix the source, network, signing, or release problem and repeat the update. Installing from an index you know is incomplete can produce confusing dependency results.

4. Install one package and review the plan

Replace the example name with a package you have identified. This changes the system and normally needs sudo:

$ sudo apt install curl

APT displays the packages it will install, upgrade, or remove, followed by the download and disk-space totals. Read that list before accepting it. A package name can match a pattern, and a dependency solution can include more packages than the name at the prompt suggests.

To select an exact version, append = and the version shown by your package metadata:

$ apt show curl
$ sudo apt install curl=REPLACE_WITH_VERSION

Do not copy the placeholder literally. If more than one release is configured, a package can also be selected with /REPLACE_WITH_SUITE_OR_CODENAME. Check the candidate version first with apt-cache policy curl when you need to see every available version.

To undo an accidental ordinary removal, install the package again. Use purge only after checking that you really want the packaged configuration files gone:

$ sudo apt remove PACKAGE_NAME
$ sudo apt install PACKAGE_NAME

# Destructive: removes packaged configuration files as well.
$ sudo apt purge PACKAGE_NAME

These commands are recovery examples, not a reason to skip the confirmation prompt. Preserve application-specific data and backups separately from APT's package state.

5. Apply upgrades with the right boundary

After a successful update, the conservative upgrade operation is:

$ sudo apt upgrade

upgrade installs available upgrades but does not remove installed packages. If an upgrade would require removal, that package is not upgraded. This makes the proposed removal list a useful boundary for a routine maintenance window.

Use full-upgrade only when you have reviewed a plan that may remove packages to complete the system-wide upgrade:

$ sudo apt full-upgrade

Warning

full-upgrade can remove currently installed packages. Read every removal and service-related change before confirming, and keep a recovery path such as console access and a current backup. If the plan is surprising, cancel it, capture the output, and investigate package holds, repository mixing, or the dependency change rather than adding automatic confirmation.

Use autoremove with the same care:

$ sudo apt autoremove

It proposes packages that were automatically installed as dependencies and are no longer needed. Review the list for tools or libraries you still use. If a useful package is marked for automatic removal, mark it as manually installed with sudo apt-mark manual PACKAGE_NAME, then rerun the proposal.

6. Make a small, reversible configuration change

APT reads configuration in a defined order: the file named by APT_CONFIG, valid files in the parts directory, the main file, binary-specific settings, then command-line options. Files in /etc/apt/apt.conf.d/ are read in alphanumeric order when their names use the accepted characters and no extension or a .conf extension. This ordering explains why a later fragment or an -o option can override an earlier value.

For a one-off test, prefer -o so there is no file to forget. This example changes only the current invocation's download retry count:

$ apt -o Acquire::Retries=3 --version
apt 2.8.3 (amd64)

For a persistent setting, create a clearly named fragment as root and keep the value on one line with a quoted value and a trailing semicolon:

$ sudoedit /etc/apt/apt.conf.d/80-local-retries
Acquire::Retries "3";

Check that APT can parse the configuration without starting a package transaction:

$ apt-config dump | grep '^Acquire::Retries'

To undo this example, remove only the fragment you created, then run the dump command again:

$ sudo rm /etc/apt/apt.conf.d/80-local-retries
$ apt-config dump | grep '^Acquire::Retries'

The final command should produce no matching line if there is no other source for that setting. Do not edit /etc/apt/apt.conf just to add one local preference when a numbered fragment gives you a smaller rollback target. Treat options such as automatic confirmation, hooks, proxy commands, and repository locations as security-sensitive: audit them before use and never paste untrusted configuration into a root-owned file.

7. Keep common failure modes separate

  • "Unable to locate package": check the spelling, repository components, architecture, and whether apt update completed successfully.
  • Held or conflicting packages: inspect the proposed changes and package policy. Do not force a removal merely to make the command finish.
  • Interrupted package configuration: capture the error first, then use the package manager's documented recovery for the specific failure. Avoid deleting package database files or lock files.
  • Lock errors: another package operation may be running. Wait for it to finish and inspect the owning process before taking action; never remove a lock file as a shortcut.
  • Unexpected removals: cancel the prompt, check the configured sources and preferences, and compare the plan with apt-cache policy PACKAGE_NAME.

When an operation fails, save the complete terminal output and the exit status immediately with printf '%s\n' "$?". Do not run a second command first, because it replaces the status you are trying to diagnose.

Done means

  • APT's version and architecture were checked before using version-sensitive advice.
  • Package metadata was inspected before installation, removal, or an upgrade.
  • sudo apt update completed without repository errors before packages were changed.
  • The proposed changes for install, upgrade, full-upgrade, and autoremove were read before confirmation.
  • Configuration was tested with -o first, or placed in a named fragment with an explicit undo path.
  • No lock file, package database file, or unexpected package was deleted to hide a failure.