apt Command in Linux: The Complete Package Management Guide
Master the apt command in linux with step-by-step examples for installing, updating, removing, searching, cleaning packages, and handling dependencies.

5 min read
Master the apt command in linux with step-by-step examples for installing, updating, removing, searching, cleaning packages, and handling dependencies.
- Prerequisites and Supported Releases
- Step 1: Updating the Package Index (apt update)
- Step 2: Upgrading Installed Packages (apt upgrade)
In the Debian and Ubuntu ecosystem, the apt (Advanced Package Tool) command is the fundamental terminal interface used by millions of system administrators and software engineers every day.
Introduced to unify the disparate workflows of older utilities like apt-get, apt-cache, and apt-config, apt delivers a streamlined, user-friendly experience featuring colorful output, real-time progress bars, and intelligent dependency resolution.
Mastering apt is essential for maintaining server security, installing developer environments, and optimizing disk storage on any modern Ubuntu system.
In this complete guide, we cover the apt command in linux with practical, reproducible terminal commands for searching, installing, upgrading, and purging packages cleanly.
Comprehensive step-by-step administrator guide and reference manual for the modern apt command in Linux (Ubuntu and Debian). Covers updating repository indexes, safely upgrading installed packages, searching and querying package metadata, installing and reinstalling software, removing and purging configurations, cleaning up cached packages, and managing software sources efficiently.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A computer or server running Ubuntu 24.04 LTS, Ubuntu 22.04 LTS, or any modern Debian-based distribution.
- A non-root user account with
sudoadministrative privileges for installation commands. - Basic command-line terminal shell access.
Step 1: Updating the Package Index (apt update)
Before installing or upgrading software, synchronize your local package index with remote Ubuntu repositories.
The apt update command fetches repository metadata (package lists, version numbers, and dependencies) without modifying any currently installed software on your machine:
The output reveals how many packages currently installed on your system have newer versions available for upgrade:
# Refresh repository package index metadata:
sudo apt update
# Example output snippet:
# Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
# Hit:2 http://archive.ubuntu.com/ubuntu noble-updates InRelease
# 14 packages can be upgraded. Run 'apt list --upgradable' to see them.
Step 2: Upgrading Installed Packages (apt upgrade)
Once the repository index is refreshed, upgrade your installed software to the latest versions.
To inspect which specific packages have updates available before committing changes, run apt list --upgradable:
Execute sudo apt upgrade to download and apply available package updates safely without removing existing packages:
# List packages that have updates available:
apt list --upgradable
# Upgrade all installed packages to their newest versions:
sudo apt upgrade -y
# Full upgrade (installs new dependencies and removes obsolete conflicting packages if needed):
sudo apt full-upgrade -y
Step 3: Searching and Inspecting Packages (apt search & apt show)
When you need to find software or inspect its package details, apt provides quick search and inspection subcommands that do not require administrative sudo privileges.
Use apt search to query the package cache for matching names and descriptions:
Use apt show to display comprehensive metadata, including maintainer information, package homepage, download size, installed size, and dependency requirements:
# Search for packages matching a keyword (e.g., htop):
apt search htop
# Display detailed package metadata and dependencies:
apt show htop
# Displays: Version, Priority, Section, Installed-Size, Depends, Description
Step 4: Installing and Reinstalling Packages (apt install)
Installing software with apt automatically identifies, downloads, and configures all prerequisite libraries and dependencies.
You can install a single package, multiple packages simultaneously, or reinstall an existing package to repair corrupted files:
You can also target specific package versions if multiple releases are available in your repositories:
# Install a single package:
sudo apt install -y htop
# Install multiple packages in a single command:
sudo apt install -y curl wget git unzip
# Reinstall a package to repair damaged configuration or missing files:
sudo apt install --reinstall -y nginx
# Install a specific version of a package:
sudo apt install -y package_name=1.2.3-1ubuntu1
Step 5: Removing and Purging Packages (apt remove & apt purge)
When software is no longer needed, apt offers two distinct removal operations depending on whether you wish to preserve or destroy user configuration files.
apt remove deletes application binaries and libraries while retaining global configuration files in /etc/:
apt purge deletes both the application binaries and all associated configuration files completely:
# Remove package binaries while keeping configuration files:
sudo apt remove apache2
# Purge package binaries AND configuration files completely:
sudo apt purge apache2
# Verify removal status:
which apache2
# Output confirms no binary found.
Step 6: Cleaning the APT Cache and Removing Unused Dependencies
Over time, software installations leave behind orphaned dependency libraries and cached .deb installer files in /var/cache/apt/archives/.
The apt autoremove command scans your system and safely removes libraries that were originally installed as dependencies for other programs but are no longer required by any active package:
The apt clean command deletes all downloaded package archive files from the disk cache, freeing significant disk space:
# Remove orphaned dependencies no longer needed by any installed software:
sudo apt autoremove -y
# Remove orphaned packages along with their configuration files:
sudo apt autoremove --purge -y
# Clear the local repository package cache (/var/cache/apt/archives/):
sudo apt clean
# Clear only obsolete package files that can no longer be downloaded:
sudo apt autoclean
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
Cause: Another process (such as unattended-upgrades, software updater, or apt) is actively running in the background.
Resolution: Identify and wait for the competing process, or terminate it safely if hung.
sudo lsof /var/lib/dpkg/lock-frontend
E: Unable to locate package [package_name]
Cause: The package name is misspelled, or the package repository list has not been refreshed recently.
Resolution: Run sudo apt update and check package availability using apt search [package_name].
sudo apt update && apt search [package_name]
apt: command not found
Cause: Operating on a minimal non-Debian distribution (like Alpine, RHEL, or Arch Linux).
Resolution: Verify your distribution using cat /etc/os-release; apt is specific to Debian and Ubuntu.
cat /etc/os-release
Security Best Practices and Hardening
Always inspect the list of packages marked for removal before pressing 'Y' during apt upgrade or apt autoremove.
Enable automated security patching by configuring the unattended-upgrades service on production servers.
Avoid running apt-get dist-upgrade or apt full-upgrade on production database nodes without verified offsite backups.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
apt update vs apt upgrade vs apt full-upgrade— Deep dive into upgrade strategies, kernel updates, and package holding.apt vs apt-get: The Difference and When to Use Each— Learn when to use high-level apt vs automation-friendly apt-get scripts.- How to Fix APT and dpkg Lock Errors on Ubuntu and Debian — Troubleshoot and fix package manager lock collisions safely.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
apt is a higher-level command designed for interactive human use, combining features of apt-get and apt-cache with colorful outputs and progress indicators. apt-get is lower-level, provides strict backward compatibility, and is preferred in shell scripts..deb installer files in /var/cache/apt/archives/ until you clear them with sudo apt clean.sudo apt-mark hold package_name to pin the package at its current version. You can re-enable upgrades later with sudo apt-mark unhold package_name./var/log/apt/history.log, which records exact timestamps, user commands, and package state changes.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides


Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!