apt Package Manager

apt cheatsheet — install, update, remove packages on Ubuntu/Debian. apt install, apt update && apt upgrade, apt autoremove. Every package management command.

6 min read

What it is

apt is the command-line package manager for Debian-based Linux distributions, used for installing, upgrading, and removing software packages.

Installation

apt is typically pre-installed on Debian, Ubuntu, and their derivatives. If for some reason it’s missing (highly unlikely on a standard installation), you would first need to ensure apt-utils is installed:

Debian/Ubuntu:

sudo apt update
sudo apt install apt

Core Concepts

  • Package: A .deb file containing the program files, metadata, and installation scripts.
  • Repository (Repo): A server that hosts packages. apt consults configured repositories to find and download software.
  • Source List (sources.list and sources.list.d/): Configuration files that tell apt which repositories to use.
  • Package Index: A local cache of package information (names, versions, descriptions) from the configured repositories. This is updated by apt update.
  • Dependency: A package that another package requires to function correctly. apt automatically handles installing dependencies.
  • Pinning: A mechanism to force apt to prefer or avoid specific versions of packages from certain repositories.

Commands / Usage

Managing Package Lists

  • Update the package index:

    sudo apt update
    

    Fetches the latest package information from all configured repositories.

  • Upgrade installed packages:

    sudo apt upgrade
    

    Installs newer versions of all currently installed packages, without removing existing packages or installing new ones that aren’t dependencies.

  • Perform a full upgrade (dist-upgrade):

    sudo apt full-upgrade
    

    Installs newer versions of all currently installed packages, and may install new packages or remove existing ones if necessary to resolve dependencies (e.g., during a distribution upgrade).

Installing Software

  • Install a specific package:

    sudo apt install firefox
    

    Downloads and installs the firefox package and any necessary dependencies.

  • Install multiple packages:

    sudo apt install vim git htop
    

    Installs vim, git, and htop along with their dependencies.

  • Install a specific version of a package:

    sudo apt install firefox=115.0.2+build1-0ubuntu0.22.04.1
    

    Installs a precise version of firefox if available in the configured repositories.

  • Install packages from a file list:

    sudo apt install $(cat ~/packages.txt)
    

    Installs all packages listed in ~/packages.txt, one per line.

Removing Software

  • Remove a package:

    sudo apt remove thunderbird
    

    Removes the thunderbird package but leaves configuration files behind.

  • Remove a package and its configuration files:

    sudo apt purge gedit
    

    Removes the gedit package and its associated configuration files.

  • Remove unused packages and dependencies:

    sudo apt autoremove
    

    Removes packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.

Searching for Software

  • Search for a package by name or description:

    apt search text-editor
    

    Lists packages whose names or descriptions contain "text-editor".

  • Show detailed information about a package:

    apt show nano
    

    Displays the package name, version, dependencies, description, and other details for nano.

  • List files owned by a package:

    apt-file list nano
    

    Lists all files installed by the nano package. (Requires apt-file to be installed: sudo apt install apt-file && sudo apt-file update).

  • Find which package owns a specific file:

    apt-file search /etc/nginx/nginx.conf
    

    Shows which installed package provides the file /etc/nginx/nginx.conf. (Requires apt-file).

Package Sources and Configuration

  • View the list of configured repositories:

    cat /etc/apt/sources.list
    

    Displays the content of the main apt sources file.

  • View files in the sources.list.d directory:

    ls /etc/apt/sources.list.d/
    

    Lists additional .list files, often used for third-party repositories.

  • Add a repository (often requires software-properties-common):

    sudo add-apt-repository ppa:graphics-drivers/ppa
    

    Adds the specified Personal Package Archive (PPA) to your sources. apt update must be run afterwards.

  • Remove a repository:

    sudo add-apt-repository --remove ppa:graphics-drivers/ppa
    

    Removes the specified PPA. apt update must be run afterwards.

Advanced Actions

  • Mark a package as manually installed:

    sudo apt-mark manual firefox
    

    Prevents apt autoremove from considering firefox for removal if it was installed as a dependency.

  • Mark a package as automatically installed:

    sudo apt-mark auto htop
    

    Marks htop as an automatically installed dependency, making it eligible for removal by apt autoremove.

  • Hold a package at its current version:

    sudo apt-mark hold unattended-upgrades
    

    Prevents apt upgrade or apt full-upgrade from upgrading unattended-upgrades.

  • Un-hold a package:

    sudo apt-mark unhold unattended-upgrades
    

    Allows unattended-upgrades to be upgraded normally.

  • Clean the local package cache:

    sudo apt clean
    

    Removes downloaded package files (.deb files) from /var/cache/apt/archives/. Frees up disk space.

  • Remove obsolete package files:

    sudo apt autoclean
    

    Removes downloaded package files that can no longer be downloaded and are largely useless. Less aggressive than apt clean.

Common Patterns

  • Install a package and its dependencies, then remove it and its dependencies:

    sudo apt install neofetch
    sudo apt remove neofetch
    sudo apt autoremove
    

    A common way to temporarily install and then clean up software.

  • Upgrade the system and clean up afterward:

    sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt clean
    

    A comprehensive command to update, upgrade, remove unneeded packages, and clean the cache. The -y flag automatically confirms prompts.

  • Install a list of packages from a file:

    cat ~/my_dev_packages.txt | xargs sudo apt install -y
    

    Alternative to the $(cat ...) method, useful if the list is very long.

  • Reinstall a package:

    sudo apt update && sudo apt --reinstall install package-name
    

    Useful if a package’s files are corrupted or missing.

  • Install a package without interactive prompts:

    sudo apt install -y package-name
    

    The -y flag answers "yes" to all prompts, useful in scripts.

  • Download a package without installing it:

    apt-get --download-only install package-name
    

    Downloads the .deb file to /var/cache/apt/archives/ but doesn’t install it.

Gotchas

  • apt upgrade vs apt full-upgrade: apt upgrade is safer and will not remove packages. apt full-upgrade (formerly dist-upgrade) is more aggressive and can remove packages to resolve complex dependency changes, especially during major OS version upgrades. Always understand the difference.
  • sudo requirement: Most apt commands that modify the system (install, remove, update) require root privileges, hence the common use of sudo.
  • Repository order: The order of repositories in /etc/apt/sources.list and /etc/apt/sources.list.d/ can matter. If a package exists in multiple repositories, apt will typically prefer the one listed earlier.
  • Third-party PPAs: While convenient, adding PPAs from untrusted sources can introduce security risks or system instability. Always verify the source of a PPA.
  • apt-get vs apt: apt is the newer, more user-friendly command-line interface. apt-get is the older, more established tool. For most interactive use, apt is preferred due to its progress bar and friendlier output. apt-get is often used in scripts for backward compatibility. They generally perform the same core functions.
  • Broken packages: Sometimes, interrupted installations or conflicting repositories can leave the package manager in a "broken" state. sudo apt --fix-broken install can often resolve these issues.