VMware ESXi and vSphere Cluster Management

apt-get Command: Update, Upgrade, Install, Remove, and Clean Packages

Learn how to use apt-get on Debian-based Linux systems to refresh repositories, install and remove packages, upgrade software, diagnose dependencies, and clean cached archives.

apt-get is a command-line client in the APT package-management toolset. It is designed primarily for Debian-family distributions, including Debian and distributions derived from it. Use it to refresh repository metadata, install and upgrade software, remove packages, inspect dependency problems, and clear downloaded package archives.

This lesson assumes basic terminal use, administrative access through sudo or a root shell, basic knowledge of Linux paths, and network access to package repositories.

What apt-get Does

A package is a distributable unit containing software, metadata, and related files. APT determines which package versions are available and how their dependencies fit together. The lower-level dpkg tool records and performs many Debian package state changes; apt-get works above dpkg to obtain packages and resolve dependencies.

The main apt-get lifecycle tasks are:

  • Refresh local package indexes from configured repositories.
  • Upgrade installed packages.
  • Install named packages and their dependencies.
  • Remove packages, or purge packages and their configuration files.
  • Check whether package dependencies are consistent.
  • Delete downloaded package archives from the local cache.

Compared with higher-level interactive tools such as the apt command or dselect, apt-get has stable, script-friendly behavior and is commonly used in administration scripts, automated jobs, and SSH sessions. Interactive confirmation is still important for destructive or broad operations.

How apt-get Finds Packages

APT uses configured repositories. A repository is a package source that provides package indexes and package files. The primary repository configuration file is /etc/apt/sources.list. Additional definitions can be stored in files under /etc/apt/sources.list.d/.

A typical binary-package entry has this structure:

deb https://repository.example/debian suite component

The fields identify the entry type, repository URI, distribution suite, and repository component.

APT Repository Entry Components

ComponentExampleMeaning

Entry type: debdeb — Provides indexes for precompiled binary packages.

Entry type: deb-srcdeb-src — Provides indexes for source packages, which contain source code and build metadata rather than directly installable binaries.

Repository URIhttps://repository.example/debian — Location from which APT retrieves metadata and package files.

Distribution suitesuite — Release or distribution series to use.

Repository componentcomponent — A section of the repository containing a class of packages.

After apt-get update, APT stores downloaded package indexes locally. An index is metadata describing available package names, versions, dependencies, and repository locations. Later, an install or upgrade uses those indexes to select an available version and calculate the required dependency changes.

Refreshing Metadata with apt-get update

update downloads current package index information from every enabled repository. It does not install, remove, or upgrade installed software.

sudo apt-get update

Typical output reports repository contacts, fetched index data, and completion status. An error may indicate a network, DNS, proxy, mirror, malformed-source, or unavailable-release problem. Read errors rather than assuming that every repository was refreshed successfully.

Run update before an upgrade and normally before installing a package when current repository information is needed. The command refreshes APT's knowledge; it does not apply the changes described by that knowledge.

Upgrading Installed Packages

apt-get upgrade

upgrade updates installed packages when the transaction can be completed without removing installed packages or introducing dependency changes that require a broader operation. Refresh the indexes first, then preview and apply the ordinary upgrade:

sudo apt-get update
sudo apt-get -u upgrade

The -u option displays package names that would be upgraded. Review the proposed package list, download size, disk-space impact, and any removals before confirming.

apt-get dist-upgrade

dist-upgrade can resolve more complex dependency transitions than ordinary upgrade. To complete the requested state, it may install additional packages or remove packages that conflict with the new dependency set.

sudo apt-get dist-upgrade

Inspect proposed additions, upgrades, and removals especially carefully on production systems. The related modern terminology is apt-get full-upgrade; it refers to the same broader style of dependency-aware upgrade.

apt-get Actions at a Glance

CommandPurposeChanges Installed Packages?Important Notes

update — Downloads current package indexes — No — Refreshes metadata only.

upgrade — Updates installed packages conservatively — Yes — Normally avoids removals and major dependency changes.

dist-upgrade — Performs a broader dependency-aware upgrade — Yes — May install or remove packages; review carefully.

install — Installs named packages and dependencies — Yes — Selects versions using configured sources and indexes.

remove — Uninstalls named packages — Yes — Some system-wide configuration files may remain.

check — Checks dependency consistency — Normally no — Diagnostic action for broken package state.

clean — Deletes cached package archives — No — Reclaims space under the APT archive cache.

Installing Packages

Pass one or more package names to install. For example, the following installs the network-scanning utility nmap:

sudo apt-get install nmap

APT looks up nmap in its local package indexes, chooses an available version from the configured sources, downloads the package, and resolves required dependencies. Supporting packages may therefore be installed automatically. Read the transaction summary before approving it.

If a package cannot be located, refresh the indexes and verify the package name and repository configuration. Availability also depends on the configured distribution suite and components.

Removing and Purging Packages

Remove an installed package with:

sudo apt-get remove nmap

remove uninstalls the package files, but package-specific system-wide configuration files may remain. A related purge operation removes the package and its retained configuration files:

sudo apt-get purge nmap

Use purge only when deleting the configuration is appropriate. In either case, review the complete proposed removal list. Removing one package can affect packages that depend on it.

Checking Package Database Health

apt-get check evaluates dependency consistency and reports problems involving broken or incomplete package installations:

sudo apt-get check

Use it as a diagnostic action when installation or upgrade operations fail. Problems may follow an interrupted package operation, conflicting version requirements, or unmet dependencies. After identifying the state, follow an appropriate repair procedure, such as completing interrupted configuration or resolving the reported dependencies; do not blindly accept a large removal proposal.

Cleaning Downloaded Package Files

Downloaded package archives are commonly cached in /var/cache/apt/archives/. The cache can grow over time, particularly after upgrades.

sudo apt-get clean

clean removes all cached retrieved package archives while preserving the necessary lock-related structure. It does not remove installed software. The related autoclean operation removes only cached archives that can no longer be downloaded or are obsolete according to APT.

Package Cache and Logs

PathContents or PurposeRelevant Command

/var/cache/apt/archives/ — Downloaded package archives — sudo apt-get clean

/var/log/dpkg.log — Records package installation, removal, and upgrade actions performed through dpkg — tail -n 50 /var/log/dpkg.log

/etc/apt/sources.list — Primary APT repository configuration — Inspect with a text editor or other read-only command.

Logging and Operational Use

Review recent package activity with:

tail -n 50 /var/log/dpkg.log

This log helps verify which packages changed and when. It is useful when investigating an unexpected upgrade, failed installation, or interrupted transaction. APT may also produce command-specific output, but /var/log/dpkg.log is the key record of dpkg package actions.

apt-get is well suited to remote administration over SSH because it is terminal-based and does not require a graphical session. For scripts, use explicit commands, check exit statuses, capture output, and avoid assuming that a transaction succeeded merely because a command started.

Automation may use options such as -y to answer confirmation prompts automatically, and some environments use noninteractive configuration for package prompts. These settings can accept removals or choose configuration-file behavior without a human review. Plan, test, and log unattended changes; do not use automatic confirmation as a substitute for examining the proposed transaction.

Safe Package-Management Workflow

  1. Inspect sources: Review /etc/apt/sources.list and relevant files under /etc/apt/sources.list.d/. Confirm that the suite, components, and repository locations are appropriate.
  2. Refresh indexes: Run sudo apt-get update and investigate any errors.
  3. Preview changes: Use the relevant operation and review package additions, upgrades, removals, download sizes, and disk-space requirements.
  4. Perform the action: Install, remove, upgrade, or use a broader dependency operation only when its proposed changes are understood.
  5. Verify results: Check command output, run sudo apt-get check when appropriate, and review /var/log/dpkg.log.
  6. Clean when necessary: Use sudo apt-get clean if cached archives are consuming needed disk space.

Successful operations depend on repository connectivity, valid source definitions, sufficient disk space, and access to the package-management lock. Before major upgrades, create a backup or system snapshot when possible. Production systems deserve a maintenance window, a tested recovery plan, and careful review of dependency changes.

Troubleshooting Common Problems

Package cannot be located

First run sudo apt-get update and inspect errors. Then review /etc/apt/sources.list and files under /etc/apt/sources.list.d/. Confirm the spelling of the package name and that the package exists for the configured distribution release and repository components.

Repository metadata cannot be downloaded

Check network access, DNS, proxy settings, and repository locations. Validate the entry type, URI, suite, and component. If a third-party repository no longer provides the configured release, correct or disable that definition before retrying.

Packages are held back or dependencies cannot be resolved

Run sudo apt-get check to assess the dependency state. Ordinary upgrade may hold a package back because it cannot make the required dependency changes. Review the proposed transaction and consider dist-upgrade only after understanding which packages may be installed or removed.

A lock error is reported

Another APT or dpkg process may be running, including an automated update task. Identify the active process and allow it to finish. Do not forcibly delete lock files. Retry only after confirming that no other package-management process is active.

There is not enough disk space

Inspect filesystem usage and check whether downloaded archives occupy space in /var/cache/apt/archives/. Run sudo apt-get clean when appropriate, free enough space for the transaction, and retry.

Configuration remains after removal

This is expected behavior for remove. Use purge when retained system-wide configuration should also be deleted, after confirming that the configuration is not needed for a future reinstall.

Exam-Relevant Distinctions

  • apt-get update refreshes package metadata; it does not upgrade installed software.
  • apt-get upgrade uses conservative dependency rules, while dist-upgrade or full-upgrade can add and remove packages to complete dependency transitions.
  • remove uninstalls package files but can leave configuration; purge also removes retained package configuration.
  • clean removes all cached package archives; autoclean removes only obsolete cached archives.
  • deb entries describe binary package indexes; deb-src entries describe source-package indexes.
  • apt-get check diagnoses dependency consistency; it is not a normal package installation or upgrade command.
  • /var/cache/apt/archives/ stores downloaded archives, while /var/log/dpkg.log records package state changes.

For related package inspection, apt-cache examines package metadata and cache information, while dpkg provides lower-level package-state operations. See the apt-get command reference as a compact starting point for these workflows.