Unit

APT-GET Command: Package Management on Debian and Ubuntu

Learn apt-get on Debian-based Linux systems: update indexes, install, upgrade, remove, inspect, clean, simulate, and troubleshoot packages safely.

apt-get is a command-line interface to APT, the Advanced Package Tool used by Debian, Ubuntu, Linux Mint, and related distributions. It retrieves package information and software from configured repositories, resolves dependencies, and coordinates installation and removal.

This lesson covers routine package administration with apt-get, including safe previews, repository awareness, cleanup, package inspection, and recovery from common failures.

How APT Package Management Fits Together

A package is a distributable unit containing software, documentation, libraries, or system components. A repository is a software source that publishes package indexes and package files. A package index is local metadata describing packages available from those sources.

  • APT: The package-management system that works with repositories, indexes, dependencies, and package selections.
  • apt-get: A command-line APT client for retrieving, installing, upgrading, and removing packages.
  • dpkg: The lower-level Debian package tool. It installs local .deb archives and records package status, files, and configuration state.
  • Repository configuration: The sources listed in /etc/apt/sources.list and files under /etc/apt/sources.list.d/ determine what APT can find.
  • Package cache: Local storage for downloaded package archive files, normally under APT's cache directory.
  • Installed-package records: Local dpkg records track installed versions, files, and configuration status.

In a typical installation, apt-get reads repository configuration, downloads indexes, calculates dependencies, retrieves package archives, and asks dpkg to perform the low-level installation or removal. APT decides what should happen; dpkg applies and records the package operation.

Refreshing Package Indexes with update

Run update to download current package lists from configured repositories:

sudo apt-get update

This refreshes metadata. It does not install updates, upgrade packages, or otherwise change the versions of installed software. Run it before installing a package or checking for available upgrades, especially after changing repository configuration or when the indexes may be stale.

Output commonly shows repository fetch lines followed by messages that package lists have been read. Errors in this step matter: a failed repository may leave you with incomplete or old metadata.

Installing Packages

Install one or more packages

sudo apt-get install curl
sudo apt-get install git vim tree

Use the exact package name known to the configured repositories. APT calculates dependencies, which are packages required by another package to install or function correctly. Supporting libraries and utilities may therefore appear in the proposed changes.

Before confirmation, review the summary. Pay attention to packages that will be newly installed, upgraded, removed, downloaded, and the amount of disk space that will be used.

Reading package lists... Done
The following additional packages will be installed:
  supporting-package
The following NEW packages will be installed:
  curl supporting-package
Need to get ... of archives.
After this operation, ... of additional disk space will be used.
Do you want to continue? [Y/n]

You can request a particular version when that version is available from the configured sources:

sudo apt-get install <package>=<version>

Version strings must match an available version exactly. Inspect available versions first with apt-cache policy, and remember that choosing an older version may introduce dependency or security consequences.

Upgrading Installed Software

First refresh metadata, then perform an ordinary upgrade:

sudo apt-get update
sudo apt-get upgrade

apt-get upgrade installs newer versions of installed packages when the upgrade can be completed without removing packages or making the more substantial package-selection changes associated with a dependency transition.

apt-get dist-upgrade uses a more flexible dependency-solving mode. It may install new packages or remove existing packages to complete an upgrade:

sudo apt-get dist-upgrade

Modern apt commonly exposes similar behavior as full-upgrade. Do not confuse either operation with a distribution-release upgrade. Moving from one Debian or Ubuntu release to another is a separate, planned procedure.

Preview an upgrade

sudo apt-get -s upgrade
sudo apt-get -s dist-upgrade

Simulation prints intended actions without applying them. Review proposed removals carefully, particularly for a dist-upgrade. Make a backup or snapshot before a large upgrade, and ensure the system has adequate free space.

Removing Packages

OperationProgram filesPackage configurationUnused dependenciesCached downloads
removeRemoves the packageGenerally retainedNoNo
purgeRemoves the packageRemoves package-owned configurationNoNo
autoremoveRemoves selected unused packagesDepends on package selectionYesNo
cleanNoNoNoRemoves all cached archives
autocleanNoNoNoRemoves obsolete cached archives

remove versus purge

sudo apt-get remove <package>
sudo apt-get purge <package>

remove uninstalls program files but commonly leaves system-wide configuration files so a later reinstall can retain settings. purge also removes configuration files owned by the package. Neither command necessarily removes every user-created file in a home directory or application data directory.

Remove no-longer-needed dependencies

sudo apt-get autoremove

APT marks some dependencies as automatically installed. When no remaining package needs them, autoremove can delete them. Always inspect the removal list. A package that looks unused may still be important to a workflow, and removing an essential package can make a system unusable.

Cleaning the Local Package Cache

When APT downloads a package archive, it may retain that archive in the local package cache. The cache can consume significant disk space over time.

sudo apt-get autoclean
sudo apt-get clean

autoclean removes cached archives that can no longer be downloaded from configured repositories, such as obsolete versions. clean removes all cached package archives. Cleaning does not uninstall software and does not remove package indexes.

Check available storage before large operations with Linux disk-space commands.

Searching and Inspecting Packages

Discovery commands inspect metadata rather than changing installed software.

apt-cache search <keyword>
apt-cache show <package>
apt-cache policy <package>
dpkg -l <package>
dpkg -L <package>
dpkg -S /path/to/file
  • apt-cache search searches package names and descriptions for a keyword.
  • apt-cache show displays a package description, dependencies, architecture, and other metadata.
  • apt-cache policy shows the installed version, candidate version, and available versions or sources.
  • dpkg -l displays whether a package is installed and its status. A status such as ii generally indicates an installed package.
  • dpkg -L lists files installed by a package.
  • dpkg -S identifies which installed package owns a specified file path.

Modern distributions also provide the user-oriented apt command for searching and displaying package information. apt-get remains useful in scripts and documentation because its command interface is intended to be more stable for automation.

Simulation and Confirmation Safety

Use the -s option to perform a dry run:

sudo apt-get -s install htop
sudo apt-get -s remove <package>
sudo apt-get -s autoremove

A simulation reports packages that would be installed, upgraded, removed, or left back without applying changes. It is especially useful before removals, autoremove, dist-upgrade, or operations involving third-party repositories.

APT normally asks for confirmation. Options that automatically answer yes, such as -y, can be useful in carefully reviewed automation, but they remove an important safety checkpoint:

sudo apt-get -y install <package>

Core apt-get Commands and Effects

CommandPrimary purposeChanges package indexesChanges installed packagesKey caution
updateDownload current package indexesYesNoInvestigate fetch or signature errors
installInstall named packages and dependenciesUses indexesYesCheck dependency and removal summary
upgradeUpgrade installed packages conservativelyUses indexesYesRun update first
dist-upgradeResolve dependency-changing upgradesUses indexesYesMay install or remove packages
removeUninstall packagesNoYesConfiguration commonly remains
purgeUninstall packages and package configurationNoYesReview data and configuration consequences
autoremoveRemove unneeded automatic dependenciesNoYesReview every proposed removal
cleanDelete all cached package archivesNoNoArchives must be downloaded again if needed
autocleanDelete obsolete cached archivesNoNoOnly obsolete archives are removed

Repository and Package-Source Awareness

APT can install only packages described by its configured sources. The main configuration file is /etc/apt/sources.list; additional source-list fragments are commonly stored in /etc/apt/sources.list.d/. These entries identify repositories and the distribution release or component they provide.

Repository changes should match the installed distribution release. Unsupported, mixed, obsolete, or poorly maintained third-party sources can cause version conflicts, dependency failures, unavailable Release files, and security risks. Do not disable signature verification just to make an operation continue. Use trusted repository documentation and remove or correct incompatible entries.

Before changing sources, identify the operating system and release with the guidance in how to determine the operating system. Keep routine package updates separate from a distribution-release upgrade, and make a backup or snapshot before repository changes.

Common Errors and Safe Recovery

Symptom or messageLikely causeSafe diagnostic or corrective actionWhen to stop and investigate
Could not get lockAnother apt, apt-get, dpkg, graphical updater, or automated updater is activeWait, then identify active package processes before retryingStop if the owner is unknown or appears stuck; do not delete lock files merely to bypass a running process
Unable to locate packageStale indexes, misspelled name, or missing repositoryRun sudo apt-get update, search metadata, and review sourcesStop if the package is absent from supported sources or the release does not match
Unmet dependenciesInterrupted installation, conflicting versions, mixed sources, or held packagesReview sources and proposed changes; use configuration repair and cautious dependency repairStop if repair proposes removing essential or unrelated packages
dpkg was interruptedPrevious operation ended before configuration completedRun sudo dpkg --configure -a, then retry after it succeedsStop if dpkg reports repeated configuration errors
Repository signature or authentication errorIncorrect clock, obsolete source, missing or changed signing key, or unsupported releaseCheck time, source definitions, network, and trusted repository instructionsStop; do not bypass signature verification
Download or DNS failureNetwork, DNS, proxy, or unavailable mirror problemTest connectivity and name resolution, check proxy settings, and retry a supported mirrorStop if the repository identity or downloaded metadata cannot be verified

Repair an interrupted operation

sudo dpkg --configure -a
sudo apt-get -f install

The first command completes pending dpkg configuration. The second asks APT to correct missing dependencies. Use both cautiously and read the proposed changes. Do not repeatedly force installation or remove package database files to hide an error; forcing operations can leave the package database or system dependencies in a worse state.

Operational Safety Checklist

  • Use a backup or snapshot before large upgrades, major removals, or repository changes.
  • Run apt-get update before installing or checking for upgrades.
  • Read download size, disk-space impact, new packages, upgrades, and removals.
  • Use apt-get -s before risky operations.
  • Check free disk space before upgrades and large installations.
  • Do not run multiple package-management processes at the same time.
  • Do not delete lock files or bypass repository signature checks as a shortcut.
  • Maintain a supported distribution release and use repositories intended for that release.
  • Distinguish ordinary package updates from a distribution-release upgrade.

Practical Workflows

Prepare before installing software

sudo apt-get update

This refreshes metadata only. It does not upgrade installed packages.

Install and preview a utility

sudo apt-get -s install htop
sudo apt-get install htop

Install several packages together

sudo apt-get install git vim tree

Apply ordinary updates

sudo apt-get update
sudo apt-get -s upgrade
sudo apt-get upgrade

Remove an application and review orphaned dependencies

sudo apt-get -s remove example-package
sudo apt-get remove example-package
sudo apt-get -s autoremove
sudo apt-get autoremove

Remove an application and its package configuration

sudo apt-get purge example-package

Free cache space

sudo apt-get autoclean
sudo apt-get clean

Exam-Relevant Notes

  • apt-get update refreshes package indexes; it does not install available updates.
  • apt-get upgrade upgrades installed packages without the dependency-changing behavior normally associated with dist-upgrade.
  • apt-get dist-upgrade may install or remove packages to resolve changing dependencies.
  • remove commonly retains package configuration; purge removes it.
  • autoremove targets automatically installed dependencies that are no longer required.
  • clean removes all cached package archives; autoclean removes obsolete archives only.
  • APT resolves packages and dependencies, while dpkg performs low-level Debian package installation and tracks package state.
  • A package-not-found error can result from stale indexes, a wrong name, or a repository that is not configured.
  • A lock error usually means another package-management process is active; deleting lock files is not a safe first response.

For broader command-line practice, continue with essential Linux commands and review Ubuntu installation concepts.