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
.debarchives and records package status, files, and configuration state. - Repository configuration: The sources listed in
/etc/apt/sources.listand 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
| Operation | Program files | Package configuration | Unused dependencies | Cached downloads |
|---|---|---|---|---|
remove | Removes the package | Generally retained | No | No |
purge | Removes the package | Removes package-owned configuration | No | No |
autoremove | Removes selected unused packages | Depends on package selection | Yes | No |
clean | No | No | No | Removes all cached archives |
autoclean | No | No | No | Removes 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 searchsearches package names and descriptions for a keyword.apt-cache showdisplays a package description, dependencies, architecture, and other metadata.apt-cache policyshows the installed version, candidate version, and available versions or sources.dpkg -ldisplays whether a package is installed and its status. A status such asiigenerally indicates an installed package.dpkg -Llists files installed by a package.dpkg -Sidentifies 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
| Command | Primary purpose | Changes package indexes | Changes installed packages | Key caution |
|---|---|---|---|---|
update | Download current package indexes | Yes | No | Investigate fetch or signature errors |
install | Install named packages and dependencies | Uses indexes | Yes | Check dependency and removal summary |
upgrade | Upgrade installed packages conservatively | Uses indexes | Yes | Run update first |
dist-upgrade | Resolve dependency-changing upgrades | Uses indexes | Yes | May install or remove packages |
remove | Uninstall packages | No | Yes | Configuration commonly remains |
purge | Uninstall packages and package configuration | No | Yes | Review data and configuration consequences |
autoremove | Remove unneeded automatic dependencies | No | Yes | Review every proposed removal |
clean | Delete all cached package archives | No | No | Archives must be downloaded again if needed |
autoclean | Delete obsolete cached archives | No | No | Only 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 message | Likely cause | Safe diagnostic or corrective action | When to stop and investigate |
|---|---|---|---|
| Could not get lock | Another apt, apt-get, dpkg, graphical updater, or automated updater is active | Wait, then identify active package processes before retrying | Stop if the owner is unknown or appears stuck; do not delete lock files merely to bypass a running process |
| Unable to locate package | Stale indexes, misspelled name, or missing repository | Run sudo apt-get update, search metadata, and review sources | Stop if the package is absent from supported sources or the release does not match |
| Unmet dependencies | Interrupted installation, conflicting versions, mixed sources, or held packages | Review sources and proposed changes; use configuration repair and cautious dependency repair | Stop if repair proposes removing essential or unrelated packages |
| dpkg was interrupted | Previous operation ended before configuration completed | Run sudo dpkg --configure -a, then retry after it succeeds | Stop if dpkg reports repeated configuration errors |
| Repository signature or authentication error | Incorrect clock, obsolete source, missing or changed signing key, or unsupported release | Check time, source definitions, network, and trusted repository instructions | Stop; do not bypass signature verification |
| Download or DNS failure | Network, DNS, proxy, or unavailable mirror problem | Test connectivity and name resolution, check proxy settings, and retry a supported mirror | Stop 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 updatebefore installing or checking for upgrades. - Read download size, disk-space impact, new packages, upgrades, and removals.
- Use
apt-get -sbefore 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 updaterefreshes package indexes; it does not install available updates.apt-get upgradeupgrades installed packages without the dependency-changing behavior normally associated with dist-upgrade.apt-get dist-upgrademay install or remove packages to resolve changing dependencies.removecommonly retains package configuration;purgeremoves it.autoremovetargets automatically installed dependencies that are no longer required.cleanremoves all cached package archives;autocleanremoves 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.