Skip to content

The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.


1. Introduction

Upstream: APT (Advanced Package Tool) | POSIX: None | Safety Tier: privileged-destructive | Scope: package-management

apt provides a high-level command-line interface for the package management system on Debian and Debian-based Linux distributions (such as Ubuntu). It combines the most commonly used commands from apt-get and apt-cache with visual enhancements like progress bars and colorized output, designed primarily for interactive use.

  • Upstream Project & Provenance: Developed and maintained by the Debian Project.
  • Portability & Standards Baseline: apt is completely specific to Debian-derived systems using the dpkg packaging backend. It is not defined in POSIX.
  • Target Research Implementation: Audited against APT 2.9.
  • Applicability & Lifecycle: The standard, recommended interactive tool for resolving dependencies, downloading, installing, updating, and removing packages on Debian/Ubuntu systems.

2. Syntax and Command Model

2.1 Canonical Synopsis

bash
apt [OPTIONS] command [package ...]

2.2 Execution Model & Adjacency Requirement

  • apt operates on local package databases and remote repositories.
  • Most operations that modify the system state (install, upgrade, remove) require superuser privileges (root or via sudo).
  • Read-only operations (search, show, list) can be run by unprivileged users.
  • apt relies on /var/lib/dpkg/lock and /var/lib/apt/lists/lock to prevent concurrent modifications.

3. Options

3.1 Primary Commands

CommandDescription
updateDownloads package information from all configured sources.
upgradeUpgrades installed packages to their newest available versions.
full-upgradeUpgrades packages, automatically removing dependencies if necessary.
installInstalls one or more packages and their dependencies.
removeRemoves packages but leaves configuration files intact.
purgeCompletely removes packages along with their configuration files.
autoremoveRemoves packages that were automatically installed to satisfy dependencies and are no longer needed.
searchSearches for the given regex term in the package descriptions.
showDisplays detailed information about the specified package(s).
listLists packages based on specific criteria (e.g., --installed, --upgradable).

3.2 Primary Flags

Short FlagLong FlagDescriptionPOSIX Defined
-y--yesAutomatically answer "yes" to all prompts, assuming non-interactive execution.No
-q--quietProduce output suitable for logging, omitting progress indicators.No
N/A--simulatePerform a dry run without actually changing the system state (alias -s).No
N/A--no-install-recommendsDo not automatically install recommended packages.No
N/A--reinstallReinstall a package that is already installed at the newest version.No

4. Basic Usage

4.1 Quick-Reference Cheatsheet Card

OperationCommandNotes
Update repository metadatasudo apt updateAlways run this before installing or upgrading.
Upgrade all packagessudo apt upgradeSafely upgrades software without removing packages.
Search for a packageapt search <term>Read-only; queries the local cache.
Display package detailsapt show <package>Shows dependencies, size, and description.
List installed packagesapt list --installedLists all packages currently on the system.

4.2 Searching for a Package

console
$ apt search nginx
Sorting... Done
Full Text Search... Done
nginx/stable,now 1.24.0-2ubuntu7 amd64 [installed]
  small, powerful, scalable web/proxy server

4.3 Listing Upgradable Packages

console
$ apt list --upgradable
Listing... Done
curl/stable 8.5.0-2ubuntu10.1 amd64 [upgradable from: 8.5.0-2ubuntu10]
libcurl4/stable 8.5.0-2ubuntu10.1 amd64 [upgradable from: 8.5.0-2ubuntu10]

5. Practical Operations

5.1 Installing a Package

Installing the htop utility, which automatically resolves and installs dependencies:

console
$ sudo apt install htop
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 135 kB of archives.
After this operation, 350 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble/main amd64 htop amd64 3.3.0-4build1 [135 kB]
Fetched 135 kB in 1s (200 kB/s)
Selecting previously unselected package htop.
Preparing to unpack .../htop_3.3.0-4build1_amd64.deb ...
Unpacking htop (3.3.0-4build1) ...
Setting up htop (3.3.0-4build1) ...
Processing triggers for man-db (2.12.0-3) ...

5.2 Completely Removing a Package (Purge)

To remove a package and wipe its configuration files:

console
$ sudo apt purge apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  apache2*
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
After this operation, 540 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 115000 files and directories currently installed.)
Removing apache2 (2.4.58-1ubuntu8.1) ...
Purging configuration files for apache2 (2.4.58-1ubuntu8.1) ...

5.3 Performing a Full System Upgrade

A full-upgrade intelligently handles changing dependencies with new versions of packages and will remove obsolete packages if necessary:

console
$ sudo apt update && sudo apt full-upgrade

6. Advanced Usage

6.1 Unattended / Scripted Installation

WARNING

The apt command does not have a stable CLI interface and is explicitly designed for interactive use. For scripts, use apt-get and apt-cache instead to avoid breaking changes in output formatting.

If you must automate package installation, use the DEBIAN_FRONTEND environment variable alongside -y:

bash
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends docker-ce
  • DEBIAN_FRONTEND=noninteractive: Prevents ncurses or dialog prompts (e.g., timezone configurations) from stalling the script.
  • -y: Automatically confirms the installation.
  • --no-install-recommends: Keeps the footprint small by excluding non-essential suggested packages.

6.2 Holding a Package

To prevent a specific package from being upgraded during a global apt upgrade:

bash
sudo apt-mark hold linux-image-generic

To release the hold later:

bash
sudo apt-mark unhold linux-image-generic

7. Exit Status, Environment, and Configuration

7.1 Exit Status Codes

Exit CodeMeaning
0Success (operation completed without errors).
100General error (e.g., package not found, unmet dependencies, or user aborted).

7.2 Configuration Files

PathPurpose
/etc/apt/sources.listPrimary repository configuration file.
/etc/apt/sources.list.d/Directory for additional repository fragments (e.g., third-party PPAs).
/etc/apt/apt.confGlobal APT configuration options.
/etc/apt/apt.conf.d/Directory for configuration fragments.
/var/lib/apt/lists/Storage area for downloaded package repository metadata.
/var/cache/apt/archives/Cache of downloaded .deb package files.

8. Safety, Security, and Portability

8.1 Lock Files and Concurrency

APT uses strict locking mechanisms on /var/lib/dpkg/lock-frontend and /var/lib/apt/lists/lock. If another instance of apt, apt-get, or dpkg is running, subsequent invocations will fail with a Could not get lock error. Never manually delete these lock files unless you have verified that no dpkg process is active, as doing so can corrupt the package database.

8.2 Security Boundaries

Installing, removing, and upgrading packages fundamentally modifies the system state and places executables in system directories (e.g., /usr/bin/). These operations inherently require superuser privileges.

8.3 Third-Party Repositories (PPAs)

Adding third-party repositories expands your software catalog but introduces significant security risks, as packages from these sources run as root during installation. Always audit the GPG keys and provenance of external repositories before adding them to /etc/apt/sources.list.d/.


9. Best Practices

  1. Do not use apt in scripts:
    • Guidance: Always use apt-get and apt-cache in automated shell scripts, Dockerfiles, and CI/CD pipelines.
    • Authoritative Justification: The official apt(8) manual explicitly states: "The apt command is meant to be pleasant for end users and does not need to be backward compatible like its more specific cousins like apt-get(8) and apt-cache(8). Therefore you should not use apt in scripts."
  2. Update before installing:
    • Guidance: Always run sudo apt update before sudo apt install.
    • Authoritative Justification: Installing without updating the cache can lead to 404 Not Found errors as the repository mirrors move older package versions.
  3. Clean up automatically:
    • Guidance: Routinely run sudo apt autoremove to clear out orphaned dependencies and old kernel images that consume /boot space.

References

  1. Debian Administrator's Handbook - APT: https://debian-handbook.info/browse/stable/apt.html
  2. APT Manual Page: man 8 apt
  3. Debian Wiki - Package Management: https://wiki.debian.org/PackageManagement

Last updated:

Released under the MIT License.