apt

package managementlinux
The apt command is one of the most frequently used commands in Linux/Unix-like operating systems. apt The apt command is a high-level package management tool for Debian and Ubuntu-based Linux distributions, providing a user-friendly interface for installing, updating, removing, and managing software packages.

Quick Reference

Command Name:

apt

Category:

package management

Platform:

linux

Basic Usage:

apt [options] [arguments]

Common Use Cases

  • 1

    Package installation

    Install software packages with automatic dependency resolution

  • 2

    System updates

    Update the system and installed packages to latest versions

  • 3

    Package management

    Search, install, remove, and manage software packages

  • 4

    Repository management

    Add, remove, and configure software repositories

Syntax

apt [options] command

Options

Option Description
-h, --help Show help message
-v, --version Show version information
-y, --yes Automatically answer yes to prompts
-q, --quiet No output except for errors
-s, --simulate Simulate operations but don't actually perform them
-d, --download-only Download packages but don't install them
--no-install-recommends Don't install recommended packages
--install-suggests Consider suggested packages as dependencies
--no-upgrade Don't upgrade packages
--only-upgrade Only upgrade already installed packages
--allow-downgrades Allow packages to be downgraded
--reinstall Reinstall packages that are already installed
--fix-broken Fix broken dependencies
--verbose-versions Show full versions for packages

Examples

How to Use These Examples

The examples below show common ways to use the apt command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Update package list
sudo apt update
Upgrade all installed packages
sudo apt upgrade
Install a new package
sudo apt install package-name
Remove a package
sudo apt remove package-name
Search for a package
apt search keyword

Advanced Examples:

Update package list and upgrade in one command
sudo apt update && sudo apt upgrade -y
Install multiple packages
sudo apt install package1 package2 package3
Remove a package and its configuration files
sudo apt purge package-name
Remove orphaned packages (no longer needed dependencies)
sudo apt autoremove
Show detailed information about a package
apt show package-name
List all available upgrades
apt list --upgradable
Fix broken dependencies
sudo apt --fix-broken install
Clean the local repository of retrieved package files
sudo apt clean
Download a package without installing it
apt download package-name

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Common Commands:

The apt tool provides several commands for package management:

  • update: Update the package list from repositories
  • upgrade: Upgrade installed packages to newer versions
  • full-upgrade: Upgrade packages, even if it requires removing other packages (similar to dist-upgrade in apt-get)
  • install: Install new packages
  • remove: Remove packages while keeping configuration files
  • purge: Remove packages along with their configuration files
  • autoremove: Remove automatically installed packages that are no longer needed
  • search: Search for packages by name or description
  • show: Display detailed information about packages
  • list: List packages based on criteria (installed, upgradable, etc.)
  • edit-sources: Edit sources.list using the default editor
  • clean: Remove downloaded package files from the local cache
  • autoclean: Remove obsolete downloaded package files

Apt vs Apt-get:

The apt command was designed as a more user-friendly alternative to apt-get and apt-cache:

  • More intuitive command structure and simplified options
  • Progress bar display for better usability
  • Color output for better readability
  • Combines commonly used features from apt-get, apt-cache, and apt-config
  • Designed for interactive use, while apt-get is better for scripting

Package Management Tips:

  • Always run sudo apt update before installing or upgrading packages to ensure you have the latest package information
  • Use apt list --installed to see all installed packages
  • Use apt show package-name to view detailed information about a package before installing it
  • Add -y flag to automatically answer yes to prompts in scripts (e.g., sudo apt install -y package-name)
  • Use apt search with grep for more specific searches: apt search text-editor | grep gtk
  • The apt full-upgrade command is similar to apt-get dist-upgrade and should be used with caution

Configuration:

APT uses several configuration files:

  • /etc/apt/sources.list: Main file listing package repositories
  • /etc/apt/sources.list.d/: Directory containing additional repository configuration files
  • /etc/apt/apt.conf and /etc/apt/apt.conf.d/: Configuration files for APT behavior
  • /etc/apt/preferences and /etc/apt/preferences.d/: Package pinning configuration

Package States:

Understanding package states can help manage your system:

  • Installed: Package is installed on the system
  • Upgradable: A newer version is available in the repositories
  • Held: Package is marked to not be upgraded automatically
  • Removable: Package installed but not required by other packages
  • Auto-removable: Package installed automatically but no longer required

Managing Package Versions:

To install a specific version of a package:

sudo apt install package-name=version

To prevent a package from being upgraded:

sudo apt-mark hold package-name

To allow a held package to be upgraded again:

sudo apt-mark unhold package-name

Important Notes:

  • Always use sudo when making changes to the system through apt
  • Be careful when using --allow-downgrades as it can potentially break dependencies
  • Use apt --simulate install package-name to see what would happen without making changes
  • For scripting and automated tasks, apt-get is still recommended over apt
  • The apt cache is stored in /var/cache/apt/archives/
  • Always check the suggested/recommended packages to avoid installing unnecessary software
  • When in doubt, use apt show to get details about a package before installation

Common Use Cases

Package installation

Install software packages with automatic dependency resolution

System updates

Update the system and installed packages to latest versions

Package management

Search, install, remove, and manage software packages

Repository management

Add, remove, and configure software repositories

System maintenance

Clean up package caches and perform system maintenance tasks

Related Commands

These commands are frequently used alongside apt or serve similar purposes:

Use Cases

1

Package installation

Install software packages with automatic dependency resolution

2

System updates

Update the system and installed packages to latest versions

3

Package management

Search, install, remove, and manage software packages

4

Repository management

Add, remove, and configure software repositories

5

System maintenance

Clean up package caches and perform system maintenance tasks

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the apt command works in different scenarios.

$ apt
View All Commands
apt - Linux Command Guide