apt Command in Linux: Complete Guide with Examples & Cheat Sheet

apt Command in Linux: Complete Guide with Examples & Cheat Sheet

The apt command (Advanced Package Tool) is the default package management utility used in Debian-based Linux distributions such as Ubuntu, Linux Mint, and Debian. Unlike low-level tools like dpkg, apt handles dependency resolution automatically. It communicates with remote repositories, calculates the required library versions, and installs them in the correct order.


Quick apt Command Reference (Cheat Sheet)

The table below provides a quick reference for commonly used apt commands in Linux. Use it as a shortcut before diving into detailed examples.

Purpose apt Command
Update package index sudo apt update
Upgrade installed packages sudo apt upgrade
Full system upgrade sudo apt full-upgrade
Install a package sudo apt install package_name
Remove a package sudo apt remove package_name
Remove package with config sudo apt purge package_name
Search for a package apt search keyword
Show package details apt show package_name
List installed packages apt list --installed
List upgradable packages apt list --upgradable
Remove unused dependencies sudo apt autoremove
Check package dependencies apt depends package_name
Skip confirmation prompt sudo apt install -y package_name
Edit repository sources sudo apt edit-sources

Comparison Between apt and apt-get

Both apt and apt-get are command-line tools used for managing packages, but there are important differences:

  • apt is designed for interactive, end-user usage.
  • It combines the most commonly used features of apt-get and apt-cache.
  • apt provides colored output and progress bars, improving readability.
  • Output format of apt may change between versions, unlike apt-get which is more script-stable.

For day-to-day usage, apt is recommended, while scripts often still use apt-get.


How the apt Command Works

The apt command retrieves package information from software repositories. These repositories are defined in the following locations:

  • /etc/apt/sources.list
  • /etc/apt/sources.list.d/*.list

A typical repository entry looks like this:

deb http://archive.ubuntu.com/ubuntu jammy main restricted

Each entry specifies:

  • Package type (deb or deb-src)
  • Repository URL
  • Distribution codename
  • Repository component (main, restricted, universe, multiverse)

apt command syntax

Only root users or sudo users can execute most apt commands.

Basic syntax:

apt [options] command [package_name]

Install and Remove Packages Using apt

Install a Package

You can install a package by specifying its exact name. The apt command automatically resolves and installs any required dependencies.

sudo apt install package_name

During installation:

  • required dependencies are downloaded automatically
  • the package is installed system-wide
  • default configuration files are created

To install multiple packages at once, list them separated by spaces:

sudo apt install package1 package2 package3

Remove a Package

To remove a package while keeping its configuration files, use apt remove:

sudo apt remove package_name

This command:

  • removes the package binaries
  • keeps configuration files under /etc
  • allows easy reinstallation with previous settings intact

To remove the package along with its configuration files, use apt purge:

sudo apt purge package_name

Use purge when you want a completely clean removal.

Remove Unused Dependencies

Over time, packages installed as dependencies may no longer be required. You can remove them using:

sudo apt autoremove

This command:

  • removes unused dependency packages
  • frees up disk space
  • helps keep the system clean and optimized

Search and Inspect Packages

Show Package Details

To display detailed information about a specific package, use:

apt show package_name

This command shows:

  • the package version
  • package description
  • installed size and download size
  • dependency list
  • maintainer and repository source

Search for Packages

If you do not know the exact package name, you can search for packages using a keyword:

apt search keyword

The search:

  • looks through package names and descriptions
  • returns matching packages from enabled repositories

List Package Dependencies

View dependency information for a package:

apt depends package_name

This command displays:

  • required dependencies
  • recommended packages
  • suggested packages
  • conflicting packages

Update and Upgrade Packages

Update the Package Index

Refresh the list of available packages from repositories:

sudo apt update

This command:

  • downloads the latest package lists from repositories
  • does not install or upgrade any packages
  • ensures apt knows about the newest available versions

Always run this before installing or upgrading packages.

Upgrade Installed Packages

To upgrade all installed packages without removing or installing additional packages, use:

sudo apt upgrade

This command:

  • upgrades packages to newer versions
  • keeps existing packages intact
  • skips upgrades that require dependency changes
sudo apt upgrade package_name

Perform a Full System Upgrade

For a complete system upgrade that allows package removals or new dependency installations, use:

sudo apt full-upgrade

This command:

  • upgrades all packages to the latest versions
  • installs new dependencies if required
  • removes obsolete or conflicting packages

full-upgrade is commonly used during:

  • major distribution upgrades
  • kernel upgrades
  • situations where dependency changes are unavoidable

List Installed and Available Packages

List All Available Packages

Display all packages available in repositories:

apt list

This command:

  • lists every package known to APT
  • includes installed and uninstalled packages
  • can produce very large output

List Installed Packages

List all packages installed on the system:

apt list --installed

This command:

  • shows installed package names
  • includes version and architecture details

To filter for a specific package, use grep:

apt list --installed | grep package_name

List Upgradable Packages

To check which installed packages have newer versions available, use:

apt list --upgradable

This command:

  • compares installed versions with repository versions
  • helps you understand what will be upgraded during apt upgrade

List All Available Versions of a Package

To list all available versions of a specific package from all repositories, use:

apt list -a package_name

This command:

  • shows installed and candidate versions
  • helps compare versions across repositories

Automate apt Commands

Skip Confirmation Prompts

By default, apt asks for confirmation before installing or removing packages. You can automatically answer Yes to all prompts using the -y option:

sudo apt install -y package_name

Simulate an Action (Dry Run)

To test what an apt command would do without making any changes, use the --simulate option:

sudo apt install package_name --simulate

This command:

  • shows which packages would be installed or upgraded
  • displays dependency changes
  • does not modify the system

Troubleshooting and Maintenance

Fix Broken Dependencies

If an installation is interrupted, use this command to repair your package database.

sudo apt --fix-broken install

This command:

  • fixes missing or broken dependencies
  • completes interrupted installations
  • restores package consistency

Clean Local Package Cache

APT stores downloaded package files in /var/cache/apt/archives. To delete these cached files and reclaim disk space, use:

sudo apt clean

This command:

  • removes all cached .deb files
  • does not remove installed packages
  • is safe to run at any time

Repository and Package Policy Management

Edit Repository Source List

APT repositories define where packages are downloaded from. To safely edit repository sources using the built-in editor, run:

sudo apt edit-sources

This command:

  • opens the repository configuration in a text editor
  • modifies files under /etc/apt/sources.list.d/
  • validates the configuration before saving

Using apt edit-sources is preferred over manually editing /etc/apt/sources.list because it helps prevent syntax errors.

After editing repository sources, always refresh the package index:

sudo apt update

Check Package Policy Information

To see repository priority and version selection logic, use:

apt policy package_name

This command displays:

  • installed version (if any)
  • candidate version
  • repository source
  • pin priority

Troubleshooting Common apt Command Issues

Below are solutions to some of the most common errors encountered while using the apt command in Linux.

1. Could not get lock /var/lib/dpkg/lock

This error occurs when another package manager process is already running.

Solution:

sudo killall apt apt-get
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a

2. dpkg was interrupted, you must manually run ‘dpkg –configure -a’

This happens when a package installation or upgrade is interrupted.

Solution:

sudo dpkg --configure -a
sudo apt update

3. Failed to fetch repository / 404 Not Found

This usually means a repository URL is invalid or no longer available.

Solution:

  • Check repository entries:
sudo apt edit-sources
  • Remove or correct invalid repository URLs
  • Run:

4. Unable to locate package

This error occurs when the package name is incorrect or the package list is outdated.

Solution:

sudo apt update
apt search package_name

5. Hash Sum mismatch

This happens due to corrupted package cache or mirror synchronization issues.

Solution:

sudo rm -rf /var/lib/apt/lists/*
sudo apt clean
sudo apt update

6. Not enough free disk space

Package installation fails if the disk is full.

Solution:

sudo apt autoremove
sudo apt clean
df -h

Frequently Asked Questions

1. What is the apt command in Linux?

The apt command is a package management tool used in Debian-based Linux distributions to install, update, upgrade, remove, and manage software packages from the command line.

2. What is the difference between apt and apt-get?

apt is designed for interactive use and combines features of apt-get and apt-cache, offering better output formatting and progress indicators, while apt-get is more stable for scripting.

3. How do I update all packages using apt?

To update all installed packages, first run ‘sudo apt update’ to refresh the package index, followed by ‘sudo apt upgrade’ to upgrade installed packages.

4. Is it safe to use apt on production systems?

Yes, apt is safe for production systems when used carefully. It is recommended to review upgrades before applying them and avoid running full-upgrade without validation.

5. How do I fix apt lock errors?

apt lock errors usually occur when another package process is running. You can resolve this by stopping the conflicting process and running ‘sudo dpkg –configure -a’.

Conclusion

The apt command in Linux is an essential tool for managing software on Debian-based systems. By learning these 15 apt command examples, you can confidently install, remove, update, and maintain packages on your system.

Bookmark this APT commands cheat sheet for quick reference during daily Linux administration tasks.


Further Reading

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.