apt-get Command in Linux: Update, Upgrade, Install, Remove, and Clean Packages
Learn apt-get on Debian and Ubuntu: refresh repositories, upgrade, install, remove, check package health, clean caches, and troubleshoot common errors.
apt-get is a non-interactive command-line frontend for APT, the Advanced Package Tool framework used by Debian and Debian-derived distributions such as Ubuntu. It manages software packages from configured repositories and is useful for interactive administration, SSH sessions, and shell scripts.
This lesson covers the package lifecycle: retrieving repository metadata, installing packages, upgrading installed software, removing packages, checking package state, and clearing downloaded package archives.
Prerequisites and Package-Management Layers
You should know how to execute shell commands, use sudo, read files and directories, and work safely in an SSH session. Linux distributions use different package-management systems; the commands in this lesson apply to Debian-family systems.
- APT resolves package versions and dependencies and obtains packages from repositories.
- apt-get performs package-management actions such as installation and upgrades. Its predictable command-line behavior makes it suitable for scripts.
- dpkg is the lower-level Debian package manager. After a package archive is downloaded, dpkg performs much of the installation, configuration, or removal work.
- apt-cache queries local package metadata. It searches packages and displays candidate versions but does not normally change installed software.
A useful mental model is: repository configuration tells APT where to look, apt-get update downloads package indexes, APT resolves dependencies, package archives are cached, and dpkg performs the package operation.
How apt-get Installation Works
A repository is a package source that provides package files and metadata. A package index is local metadata downloaded from configured repositories. It records information such as available package names, versions, dependencies, and repository locations.
- APT reads repository definitions from its configuration.
apt-get updatedownloads current package indexes and stores them locally.- When you request a package, APT looks up its name and selects an available version.
- APT resolves dependencies. A dependency is a package or version requirement that another package needs to install or run correctly.
- APT downloads the selected package archives and required dependencies into its cache.
- dpkg installs and configures the packages, or removes them when requested.
Because package names are matched against local indexes, a package may not be found until the indexes have been refreshed. Package names are also not always identical to the names of the programs they provide.
Repository Configuration
The primary repository configuration file is /etc/apt/sources.list. Additional repository definitions are commonly stored in /etc/apt/sources.list.d/. These files contain repository entries describing where APT can obtain package metadata and archives.
| Entry type | Content provided | Typical use |
|---|---|---|
deb | Prebuilt binary packages | Normal software installation and updates |
deb-src | Source-package records | Obtaining source metadata and source packages when enabled |
Conceptually, a repository entry contains a repository location, a distribution release or suite, and one or more enabled components. An illustrative entry has this form:
deb https://example.invalid/debian stable main contrib non-free
deb-src https://example.invalid/debian stable main contrib non-free
Use repository endpoints appropriate for the installed distribution and release. The suite and components must match your system, and repository definitions must provide compatible packages for the system architecture. Incorrect changes can cause unavailable packages, signature errors, or dependency conflicts. Do not mix releases casually or bypass package-signature verification as a routine workaround.
Core apt-get Actions
| Command | Purpose | Changes installed packages? | Important caution |
|---|---|---|---|
apt-get update | Download current package indexes | No | Repository errors leave metadata incomplete or stale |
apt-get upgrade | Upgrade eligible installed packages | Yes | Review the transaction; some dependency changes may be held back |
apt-get dist-upgrade | Perform a broader dependency-aware upgrade | Yes | Packages may be installed or removed |
apt-get install PACKAGE | Install a package and required dependencies | Yes | Check package name, download size, and disk usage |
apt-get remove PACKAGE | Remove a package | Yes | Review possible dependent-package removals |
apt-get check | Check dependency and package-state consistency | Normally no | It diagnoses problems; it does not automatically fix every issue |
apt-get clean | Delete downloaded package archives from the cache | No | It does not uninstall installed software |
Refreshing Repository Metadata with apt-get update
Run update when you need current information from configured repositories:
sudo apt-get update
This downloads package lists and updates local metadata. It does not install updates or change currently installed packages. Run it before installing software or checking for upgrades when current repository information is required.
Successful output normally reports indexes retrieved from configured sources. Problems can include unavailable repositories, network or DNS failures, authentication or signature errors, and stale package lists. Read the output rather than proceeding as if every source succeeded.
Upgrading Installed Packages
Routine upgrades
apt-get upgrade updates installed packages to newer available versions when the operation can complete without removing installed packages or requiring certain new dependencies. Refresh indexes first:
sudo apt-get update
sudo apt-get -u upgrade
The -u option displays packages expected to change. Review the proposed transaction, including package counts, download size, disk-space impact, removals, and configuration or service changes, before confirming it.
On production systems, use a maintenance window when appropriate. Package upgrades can restart services, change behavior, require configuration decisions, or require a reboot. Test important upgrades and check application health afterward.
Dependency-aware upgrades with dist-upgrade
apt-get dist-upgrade is the historical apt-get name for a more comprehensive upgrade. It can install or remove packages when necessary to satisfy changing dependencies. This can resolve cases where apt-get upgrade leaves packages held back.
sudo apt-get update
sudo apt-get dist-upgrade
Review this transaction particularly carefully because package removals and major component transitions can occur. Current Debian-family documentation may also use full-upgrade terminology through other APT interfaces; this lesson retains apt-get dist-upgrade because it focuses on apt-get syntax.
Installing Software
Use install followed by one or more package names:
sudo apt-get install nmap
In this example, APT searches the local indexes for nmap, selects a candidate version from the configured repositories, calculates dependencies, shows confirmation details, downloads the required archives, and invokes dpkg to install and configure them.
Before confirming, check the package list, download size, additional disk-space requirement, and any packages that will be removed. To investigate names and versions first, use apt-cache:
apt-cache search nmap
apt-cache policy nmap
apt-cache search searches package metadata, while apt-cache policy shows version and repository information. A program name may be provided by a differently named package, so searching is useful when apt-get install reports that it cannot locate a package.
Removing and Purging Software
Remove a package with:
sudo apt-get remove nmap
remove normally uninstalls the package files while preserving its configuration files. This can be useful if you might reinstall the package and want to retain its settings.
purge removes the package and its package-managed configuration files:
sudo apt-get purge nmap
Removing a package can also remove dependent packages that are no longer needed. Always review the transaction summary, especially when removing libraries or software used by services. Neither remove nor purge is the same as clearing downloaded archives.
| Operation | Installed program files | Configuration files | Downloaded archive cache |
|---|---|---|---|
remove | Removed | Commonly preserved | Not specifically removed |
purge | Removed | Package-managed configuration removed | Not specifically removed |
clean | Preserved | Preserved | Downloaded archives removed |
Checking Package Database Integrity
Use apt-get check as a diagnostic command:
sudo apt-get check
It examines dependency consistency and can report unmet dependencies or other package-state problems. It may identify an issue without automatically repairing every problem. Interrupted installations, failed dependency resolution, partially completed upgrades, and mixed repository configurations are common causes.
Use the diagnostic output together with recent package activity and repository configuration. Avoid forcing removals or mixing distribution releases unless you understand the dependency consequences.
Cleaning the APT Package Cache
APT stores retrieved package archives in /var/cache/apt/archives, with partially downloaded files commonly located in /var/cache/apt/archives/partial. When disk space is low, remove cached archives with:
sudo apt-get clean
clean removes downloaded archive files from the APT cache but does not uninstall currently installed packages or delete their configuration. A related autoclean operation is more selective and removes cached archives that can no longer be downloaded; it is not a substitute for the documented behavior of clean.
Permissions, Logs, and Automation
System-wide package changes generally require root privileges, so commands are commonly invoked through sudo. Query commands such as apt-cache often do not need root privileges.
/var/log/dpkg.log records important installation, upgrade, and removal activity. Inspect it when verifying what happened:
sudo less /var/log/dpkg.log
apt-get works well over SSH because it is terminal-oriented and does not require a graphical desktop. It is also useful in scripts, but automation must be explicit and cautious:
- Use explicit confirmation behavior appropriate to the script and environment rather than assuming a prompt can be answered safely.
- Capture output and exit status, and stop or alert when an operation fails.
- Test package changes before applying them broadly.
- Avoid unattended broad upgrades on important systems without maintenance planning, backups, monitoring, and a rollback strategy.
- When scheduling through cron, account for environment differences, network availability, maintenance windows, and package-manager lock contention.
A lock file prevents more than one package-management process from modifying package state simultaneously. Automatic updates, graphical tools, apt, apt-get, and dpkg can therefore conflict if run at the same time.
Troubleshooting apt-get
Another process holds the package-management lock
Another apt, apt-get, dpkg, graphical updater, or automatic-update process may be running. Confirm whether a legitimate process is active and wait for it to finish. Do not delete lock files blindly. If an operation was interrupted, investigate the process and package state before retrying.
Repository update fails
Missing release metadata, connection errors, or signature failures can result from an incorrect URL or suite, network or DNS failure, an obsolete repository, or an improperly signed source. Review /etc/apt/sources.list and files under /etc/apt/sources.list.d/, verify network access, and ensure every source matches the installed release. Use trusted official repository configuration.
Packages are held back
A routine upgrade may leave packages held back when updating them requires dependency changes that upgrade will not perform. Inspect the proposed changes and consider apt-get dist-upgrade only after reviewing possible additions and removals.
apt-get check reports broken dependencies
Interrupted installations, partially completed upgrades, conflicting packages, and incompatible repository sources can create unmet dependencies. Review /var/log/dpkg.log, refresh indexes, inspect repository consistency, and follow the package manager's dependency-resolution guidance. Do not force package removal without understanding the result.
There is not enough disk space
Downloaded archives may consume space in the package cache, while the root filesystem may also be close to full. Check available space, then use sudo apt-get clean when removing cached archives is appropriate. Do not delete essential system files merely to create space.
A package cannot be located
Run sudo apt-get update, verify the package name with apt-cache search, and inspect whether the required repository component is enabled. The package may not be available for the installed release or architecture.
apt-get and Related APT Tools
Use apt-get for action-oriented package operations such as installing, removing, upgrading, updating indexes, and cleaning caches. Use apt-cache for information queries such as searching package names and inspecting candidate versions.
The higher-level apt command is commonly used interactively on current Debian-family systems and presents a user-friendly interface. apt-get remains valuable for command-focused administration, scripts, and compatibility-oriented workflows.
For related shell guidance, see how to show the full path of shell commands and browse the Linux command lessons.
Exam-Ready Summary
apt-get updaterefreshes local repository indexes; it does not upgrade installed software.apt-get upgradeperforms routine upgrades without the broader dependency changes permitted by dist-upgrade.apt-get dist-upgradecan install or remove packages to satisfy dependency changes, so review it carefully.apt-get install PACKAGEresolves and installs dependencies automatically.apt-get remove PACKAGEnormally preserves configuration;purgeremoves package-managed configuration too.apt-get checkdiagnoses dependency and package-state consistency.apt-get cleanremoves downloaded archives from/var/cache/apt/archivesand does not uninstall applications.- Repository definitions live in
/etc/apt/sources.listand commonly in/etc/apt/sources.list.d/. - Package-changing commands usually need root privileges through
sudo. - Review transaction summaries, watch for locks, and consult
/var/log/dpkg.logwhen investigating package activity.