Website Vidya
Linux & ServersStep-by-Step Guide

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.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
apt Command in Linux: The Complete Package Management Guide
AI Executive SummaryWebsiteVidya AI

5 min read

Master the apt command in linux with step-by-step examples for installing, updating, removing, searching, cleaning packages, and handling dependencies.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Updating the Package Index (apt update)
  • Step 2: Upgrading Installed Packages (apt upgrade)
Grounded in full engineering runbookWebsiteVidya AI Engine

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.

Advertisement

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 sudo administrative 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.

Advertisement

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:

Advertisement

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.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
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.
APT stores downloaded .deb installer files in /var/cache/apt/archives/ until you clear them with sudo apt clean.
Use 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.
Inspect the APT history log located at /var/log/apt/history.log, which records exact timestamps, user commands, and package state changes.
RR
Written byVerified Author

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.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout