Linux online course

Advanced Packaging Tool (APT) in Linux

Learn how APT works on Debian-based Linux systems, including apt-get, apt-cache, repositories, dependencies, upgrades, removal, troubleshooting, and safe package practices.

APT (Advanced Packaging Tool) is the package-management framework used by Debian-based Linux distributions such as Debian, Ubuntu, Linux Mint, and related systems. It helps you find, install, update, upgrade, and remove software while handling the packages that software depends on.

This lesson focuses on the apt-get and apt-cache commands. You should be comfortable opening a terminal, running basic commands, and using sudo. For a related shell skill, see how to show the full path of shell commands.

What APT Is

APT is a higher-level interface to dpkg, the low-level Debian package manager. dpkg can install and track individual .deb files on a local system. APT adds repository access, dependency resolution, version selection, downloading, and coordinated package operations.

When you ask APT to install an application, it generally performs these steps:

  1. Reads package information from configured repositories.
  2. Chooses a suitable package version.
  3. Calculates direct and transitive dependencies.
  4. Downloads the required package files.
  5. Passes installation and configuration work to dpkg.
  6. Records the installed-package state and reports any problems.

APT can also coordinate upgrades and removals. It may install additional packages, replace versions, or remove packages when necessary to satisfy dependency rules.

APT components

  • APT: The overall package-management framework that works with repositories, metadata, dependencies, and package operations.
  • dpkg: The low-level Debian package manager that installs and tracks local .deb packages.
  • apt-get: A command-line APT program for installing, upgrading, removing, and maintaining packages.
  • apt-cache: A command-line APT program for querying locally available package metadata.
  • Graphical and console front ends: Alternative interfaces that use APT-related package-management functionality.

Packages and the Debian Package Format

Linux software is commonly distributed as a package. On Debian-derived systems, the usual package-file format is .deb. A package contains files to install, metadata, and information needed to integrate the software with the operating system.

A package's metadata can include:

  • The package name and description.
  • The version and target architecture.
  • The package size and installed size.
  • Required dependencies.
  • Recommended or suggested related packages.
  • Conflicts and replacement information.
  • The package's repository source and priority.
  • Whether the package is installed, available, held, or obsolete.

Local .deb files and repository packages

A local .deb file is a package archive already present on your computer, perhaps downloaded from a software publisher. A repository package is described in the package indexes downloaded from a configured repository and is normally fetched by APT when needed.

These are related but different situations. Installing a local file does not automatically make its publisher a configured repository. APT can often resolve dependencies for a local package, but repository packages are generally easier to update because APT can discover newer versions through the configured sources.

Repositories and Package Indexes

An APT repository is a configured software source that provides package files and package metadata. Repositories may be operated by the distribution or by a trusted third-party publisher that supports your distribution release and architecture.

Repository source entries are commonly stored in:

  • /etc/apt/sources.list, the primary source-list file.
  • /etc/apt/sources.list.d/, a directory for additional source-list files.

APT downloads repository metadata into its local package cache. This metadata is commonly called the package index. It lets commands such as apt-cache search and apt-cache policy work without downloading every package in advance.

Refresh indexes with apt-get update

sudo apt-get update

This command contacts configured repositories and refreshes local package indexes. It does not install upgrades. Refreshing first is important because searches, version selection, and upgrade decisions depend on the locally stored metadata.

Read the command output. Warnings or errors can indicate an unreachable repository, an incorrect release name, expired or missing signing information, or an obsolete third-party source.

Using apt-cache

apt-cache queries the local APT package cache. It searches and displays package metadata but does not install software or change the system. Its results are only as current as the last successful index refresh.

  • search: Finds package names and descriptions containing a keyword. Example: apt-cache search text editor.
  • show: Displays detailed metadata. Example: apt-cache show vim.
  • policy: Shows installed and candidate versions, repository origins, and priorities. Example: apt-cache policy vim.
  • depends: Displays packages required by a package. Example: apt-cache depends vim.
  • rdepends: Displays packages that depend on a package. Example: apt-cache rdepends libfoo.

Search for a package

apt-cache search image editor

Search results can include several related packages. Compare names and descriptions rather than installing the first result automatically. If the exact name is unknown, start with a broad keyword and then narrow the search.

Inspect package details

apt-cache show package-name

Useful fields include the description, version, architecture, dependencies, recommended packages, download size, installed size, and source package.

Check package policy and versions

apt-cache policy package-name

Package policy helps answer which version is installed, which version APT considers the candidate, and which repositories provide available versions. This is especially useful when multiple repositories or package versions are involved.

Inspect dependencies

apt-cache depends package-name
apt-cache rdepends package-name

depends shows the package's dependency relationships. rdepends shows reverse dependencies: packages that rely on the selected package. Reverse-dependency output can be large, so inspect it carefully before removing a library or other shared component.

Using apt-get

apt-get retrieves packages, resolves dependencies, and coordinates installation, upgrades, removal, and package-cache maintenance. Operations that modify system files normally require administrative privileges, so use sudo for those commands. Read-only queries such as the usual apt-cache commands do not normally require it.

Install a package

sudo apt-get install package-name

APT displays the packages it will install, upgrade, or remove, along with download and disk-space information. Review this proposal before confirming. A successful installation also installs required dependencies.

Upgrade installed packages

sudo apt-get update
sudo apt-get upgrade

apt-get upgrade installs newer versions of installed packages when the upgrade does not require removing packages or adding packages to resolve more complex dependency changes. Packages that cannot be safely upgraded under these rules may be kept back.

For a more comprehensive dependency transition, use:

sudo apt-get dist-upgrade

dist-upgrade can add or remove packages to resolve changed dependencies. On systems where the newer naming is available, full-upgrade expresses the same general idea:

sudo apt-get full-upgrade

Because these operations can remove packages, inspect the proposed changes carefully. They are not the same as merely refreshing package indexes.

Remove a package

sudo apt-get remove package-name

remove uninstalls the package but normally keeps its system-wide configuration files. Keeping configuration can be useful if you plan to reinstall the application.

Purge a package

sudo apt-get purge package-name

purge removes the package and its system-wide configuration files. It does not necessarily remove user data stored in a home directory, and it may not remove every file created manually by the application.

Remove unused dependencies

sudo apt-get autoremove

APT marks packages installed automatically as dependencies. If no installed package needs one of those dependencies anymore, autoremove proposes removing it. Always review the proposed list, especially on systems with unusual package relationships.

Clean downloaded package archives

sudo apt-get clean
sudo apt-get autoclean

clean removes downloaded package archives from the local cache. autoclean removes cached archives that can no longer be downloaded, such as obsolete versions. Cleaning affects cached files, not installed software.

Simulate changes

apt-get -s install package-name
apt-get --simulate dist-upgrade

The -s or --simulate option shows what APT would do without changing the system. Use simulation before a major upgrade, a purge, or any operation that proposes many removals. A simulation is valuable, but still review the real command's final confirmation because repository state can change.

Understanding Dependencies

A dependency is a package required by another package. A direct dependency is explicitly required by the application you selected. A transitive dependency is needed by one of those dependencies, possibly several levels deep.

APT calculates this dependency graph and normally installs required packages automatically. It can also consider:

  • Recommended packages: Packages that improve normal functionality but may not be strictly required. Whether recommendations are installed depends on APT configuration.
  • Optional packages: Packages that add features but are not needed for the basic application.
  • Conflicts: Package relationships stating that two packages, or particular versions, cannot coexist.
  • Held packages: Packages marked to remain at their current version during ordinary operations.
  • Obsolete packages: Packages no longer provided by configured repositories or replaced by newer packages.

Before confirming an operation, check the number of new, upgraded, removed, and kept-back packages. Unexpected removals may indicate a dependency transition, held packages, pinned versions, or incompatible third-party repositories.

Typical APT Workflow

  1. Refresh metadata:
    sudo apt-get update
    Read and resolve repository errors before continuing.
  2. Search:
    apt-cache search keyword
    Compare names and descriptions.
  3. Inspect:
    apt-cache show package-name
    apt-cache policy package-name
    Review the description, version, candidate, origin, dependencies, and size.
  4. Install:
    sudo apt-get install package-name
    Review the proposed changes and confirm only when appropriate.
  5. Verify:
    apt-cache policy package-name
    Confirm that an installed version is shown. You can also query the installed package database with distribution-supported package tools.
  6. Apply routine upgrades:
    sudo apt-get update
    sudo apt-get upgrade
  7. Clean up when appropriate:
    sudo apt-get autoremove
    sudo apt-get autoclean
    Review removals before accepting them.

Common apt-get Operations

  • update: Refreshes repository metadata; it does not install upgrades. sudo apt-get update
  • install: Installs a package and required dependencies. sudo apt-get install package-name
  • upgrade: Upgrades installed packages without removing packages or adding replacements for complex dependency changes. sudo apt-get upgrade
  • dist-upgrade/full-upgrade: Handles dependency changes that may require adding or removing packages. sudo apt-get dist-upgrade
  • remove: Removes a package while normally retaining system configuration. sudo apt-get remove package-name
  • purge: Removes a package and its system-wide configuration. sudo apt-get purge package-name
  • autoremove: Removes automatically installed dependencies no longer needed. sudo apt-get autoremove
  • clean: Removes all downloaded package archives from the cache. sudo apt-get clean
  • autoclean: Removes obsolete downloaded package archives. sudo apt-get autoclean

APT Front Ends

A front end is a graphical, console, or alternative command-line interface for package management. Front ends can make searching and reviewing changes easier, but the available choices vary by distribution and desktop environment.

  • Synaptic: A GTK-based graphical package manager with detailed package search, status, version, and dependency views.
  • Ubuntu Software Center or equivalent: A distribution-provided graphical software application for browsing and installing applications.
  • aptitude: A console-oriented APT client with interactive package browsing and dependency views.
  • KPackage or a KDE equivalent: A KDE-oriented package-management interface, depending on the distribution and desktop environment.

Graphical tools do not remove the need to understand repositories, dependencies, and proposed changes. Use the interface to inspect package details and installation actions, and consult terminal output when diagnosing errors.

Safe Package-Management Practices

  • Use trusted repositories that are compatible with your distribution release and architecture.
  • Run sudo apt-get update before searching for newly published packages or applying upgrades.
  • Inspect the proposed changes, especially removals and major dependency transitions.
  • Use simulation before large upgrades or risky removals.
  • Do not interrupt package installation or configuration. If a process appears slow, investigate before terminating it.
  • Be particularly cautious when removing libraries, desktop components, kernels, or packages on which many other packages depend.
  • Back up important data before release upgrades or large package changes.
  • Do not bypass repository signature verification merely to suppress an error.
  • Prefer distribution-supported repository and signing instructions for third-party software.

Troubleshooting APT

A package cannot be found

Possible causes include outdated indexes, an incorrect package name, a disabled repository, or a package unavailable for the installed release or architecture.

  1. Refresh the indexes: sudo apt-get update.
  2. Search with a broader keyword: apt-cache search keyword.
  3. Check whether APT has a candidate version: apt-cache policy package-name.
  4. Inspect source entries in /etc/apt/sources.list and /etc/apt/sources.list.d/.

Repository, signature, or release-file errors appear

Inspect the relevant source entry. The repository may be unreachable, target an unsupported release, or have missing, expired, or untrusted signing information. Correct or disable obsolete third-party entries and use signing instructions provided by the distribution or software publisher. Do not disable verification simply to force an update.

An upgrade proposes removing many packages

Do not confirm immediately. Simulate the operation, inspect versions with apt-cache policy, check for held packages, and look for conflicting third-party repositories. A complex dependency transition may be legitimate, but a large unexpected removal list requires investigation.

apt-get --simulate dist-upgrade

Installation is blocked by broken dependencies

An interrupted operation, conflicting versions, or an unavailable dependency may have left the package state incomplete. Refresh indexes, read the exact dependency error, and inspect candidate versions with apt-cache policy. Then use the repair and package-configuration steps documented for your distribution. Correct incompatible repository sources if they caused the mismatch.

Package operations consume too much disk space

Downloaded .deb archives may have accumulated, or automatically installed dependencies may no longer be needed. Review these commands before running them:

sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove

Exam-Relevant Notes

  • APT versus dpkg: APT is the higher-level framework; dpkg performs low-level local Debian package installation and tracking.
  • apt-get update versus apt-get upgrade: update refreshes metadata; upgrade installs newer package versions.
  • remove versus purge: remove normally retains system configuration; purge removes it.
  • autoremove: Removes automatically installed dependencies that are no longer required.
  • apt-cache: Queries local metadata and does not install software.
  • upgrade versus dist-upgrade: The latter can add or remove packages to resolve changed dependencies.