apt Cheat Sheet: Ubuntu & Debian Package Management

4 min read
Beginner Linux Ubuntu Debian apt Package Manager Cheat Sheet

Quick Answer: Update package list: sudo apt update. Upgrade all packages: sudo apt upgrade. Install: sudo apt install nginx. Remove: sudo apt remove nginx. Search: apt search nginx. Always run apt update before installing anything.


apt vs apt-get

apt is the modern, user-friendly command. apt-get is the older version that still works. Use apt for interactive use, apt-get in scripts (more stable output format).

Task apt (modern) apt-get (classic)
Update package list apt update apt-get update
Upgrade packages apt upgrade apt-get upgrade
Install apt install pkg apt-get install pkg
Remove apt remove pkg apt-get remove pkg
Search apt search pkg apt-cache search pkg
Show info apt show pkg apt-cache show pkg
List installed apt list --installed dpkg -l

Update and Upgrade

# Update package list (always do this first)
sudo apt update

# Upgrade all installed packages
sudo apt upgrade

# Upgrade with automatic handling of dependencies (may add/remove packages)
sudo apt full-upgrade

# Update and upgrade in one command
sudo apt update && sudo apt upgrade -y

# Distribution upgrade (e.g., Ubuntu 22.04 → 24.04)
sudo do-release-upgrade

Install Packages

# Install a package
sudo apt install nginx

# Install multiple packages
sudo apt install nginx curl wget git

# Install without prompting (yes to all)
sudo apt install -y nginx

# Install a specific version
sudo apt install nginx=1.18.0-0ubuntu1

# Install a .deb file
sudo apt install ./package.deb

# Reinstall a package
sudo apt reinstall nginx

# Install recommended packages too
sudo apt install --install-recommends package

# Install without recommended packages
sudo apt install --no-install-recommends package

Remove Packages

# Remove package (keep config files)
sudo apt remove nginx

# Remove package AND config files
sudo apt purge nginx

# Remove unused dependencies
sudo apt autoremove

# Purge and autoremove (cleanest removal)
sudo apt purge nginx && sudo apt autoremove

# Remove all packages matching pattern
sudo apt remove 'nginx*'

Search and Information

# Search for packages
apt search nginx

# Show package details
apt show nginx

# Show package version and repo
apt policy nginx

# List all installed packages
apt list --installed

# List upgradable packages
apt list --upgradable

# List all versions available
apt list -a nginx

# Check if package is installed
dpkg -l | grep nginx

# Show files installed by a package
dpkg -L nginx

# Find which package owns a file
dpkg -S /usr/sbin/nginx
# or
apt-file search /usr/sbin/nginx

Cache Management

# Clean downloaded package files
sudo apt clean

# Remove old package files (keep latest)
sudo apt autoclean

# Show cache size
du -sh /var/cache/apt/archives/

# Download package without installing
apt download nginx

Hold and Pin Packages

# Prevent a package from being upgraded
sudo apt-mark hold nginx

# Release the hold
sudo apt-mark unhold nginx

# Show held packages
apt-mark showhold

# Mark a package as manually installed
sudo apt-mark manual nginx

# Mark as automatically installed (can be autoremoved)
sudo apt-mark auto nginx

# Show manually installed packages
apt-mark showmanual

Repositories

# List configured repositories
cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/

# Add a PPA (Ubuntu)
sudo add-apt-repository ppa:ondrej/php
sudo apt update

# Remove a PPA
sudo add-apt-repository --remove ppa:ondrej/php

# Add a repository manually
echo "deb [signed-by=/etc/apt/keyrings/example.gpg] http://repo.example.com/apt stable main" | sudo tee /etc/apt/sources.list.d/example.list

# Add a GPG key for a repository
curl -fsSL https://example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg

Automatic Updates

# Install unattended-upgrades
sudo apt install unattended-upgrades

# Configure
sudo dpkg-reconfigure unattended-upgrades

# Check status
systemctl status unattended-upgrades

# View log
cat /var/log/unattended-upgrades/unattended-upgrades.log

Troubleshooting

# Fix broken packages
sudo apt --fix-broken install

# Reconfigure a package
sudo dpkg-reconfigure package

# Fix interrupted dpkg
sudo dpkg --configure -a

# Force overwrite conflicting files
sudo apt install -o Dpkg::Options::="--force-overwrite" package

# Clear package list and rebuild
sudo rm /var/lib/apt/lists/* -rf
sudo apt update

# Fix GPG key errors (modern method)
curl -fsSL https://example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg
Problem Fix
E: Unable to locate package Run sudo apt update first, check spelling
E: Could not get lock Another apt is running. Wait or: sudo kill $(lsof -t /var/lib/dpkg/lock-frontend)
Unmet dependencies sudo apt --fix-broken install
Hash Sum mismatch sudo rm /var/lib/apt/lists/* -rf && sudo apt update
GPG error: NO_PUBKEY Download key to /etc/apt/keyrings/ with gpg --dearmor
dpkg was interrupted sudo dpkg --configure -a

Quick Reference

Command What It Does
sudo apt update Refresh package list
sudo apt upgrade Upgrade all packages
sudo apt install pkg Install package
sudo apt remove pkg Remove (keep config)
sudo apt purge pkg Remove + delete config
sudo apt autoremove Remove unused dependencies
apt search keyword Search packages
apt show pkg Package details
apt list --installed List installed
apt list --upgradable List available upgrades
sudo apt-mark hold pkg Prevent upgrades
sudo apt --fix-broken install Fix broken packages
sudo apt clean Clear download cache

Related Guides