Ubuntu Package Management: APT and dpkg Cheatsheet

Essential APT & dpkg commands for Ubuntu packages

Ubuntu’s package management system is the backbone of software installation and maintenance, making it essential knowledge for any Linux user or system administrator.

tetris This image is generated by AI model Flux 1 dev.

What is APT Package Management?

APT (Advanced Package Tool) is Ubuntu’s primary package management system, inherited from Debian. It’s a powerful command-line tool for installing, upgrading, and removing software packages. APT handles dependency resolution automatically, connects to Ubuntu’s official repositories, and is the traditional method for managing system packages on Ubuntu and Debian-based distributions. If you’re new to Ubuntu, check out our guide on how to install Ubuntu 24.04 and useful tools to get started with a properly configured system.

Key Characteristics:

  • Native to Ubuntu/Debian: Built-in, no additional installation needed
  • Shared Libraries: Efficient disk usage through shared system dependencies
  • Manual Updates: You control when packages are updated
  • No Sandboxing: Packages have full system access (suitable for system tools)
  • Lightweight: No background daemon, runs only when invoked
  • Mature Ecosystem: Decades of development, extremely stable

Package Manager Comparison

Ubuntu supports multiple package management systems. Here’s how the main operations compare:

Operation APT Snap Flatpak
Search apt search keyword snap find keyword flatpak search keyword
Install sudo apt install package sudo snap install package flatpak install flathub app.id
List Installed apt list --installed snap list flatpak list --app
Show Info apt show package snap info package flatpak info app.id
Update All sudo apt update && sudo apt upgrade sudo snap refresh flatpak update
Update Single sudo apt install --only-upgrade pkg sudo snap refresh package flatpak update app.id
Remove sudo apt remove package sudo snap remove package flatpak uninstall app.id
Clean Up sudo apt autoremove snap remove --revision=N flatpak uninstall --unused
Repository /etc/apt/sources.list Snap Store (built-in) Add with flatpak remote-add

Quick Feature Comparison

Feature APT Snap Flatpak
Auto-updates No Yes No
Sandboxing No Yes Yes
Dependencies System libraries Bundled Shared runtimes
Package Size Small (5-50MB) Large (50-500MB) Medium (20-200MB)
Root Required Yes Yes No (user install)
Startup Speed Fast Slower Fast
Best For System packages Universal apps Desktop apps

About This Guide

This guide focuses on APT (apt/apt-get/dpkg) - Ubuntu’s traditional package management system. APT is essential for managing system packages, libraries, and services on Ubuntu and Debian-based systems.

For other package managers:

Each package manager has its strengths. Use APT for system packages and dependencies, Snap for cross-version compatibility and auto-updates, and Flatpak for desktop applications with fine-grained permissions.


APT Package Management - Detailed Guide

Understanding APT vs apt-get

APT (apt) - Modern Choice

Introduced in Ubuntu 14.04, apt combines features from apt-get and apt-cache with enhanced user experience:

Advantages:

  • Progress bars and colored output
  • More intuitive commands
  • Cleaner output for human readability
  • Combines multiple tool functionality

When to use: Daily interactive command-line operations

apt-get - Traditional Choice

The original package management tool, still widely used:

Advantages:

  • More stable API (better for scripts)
  • Backward compatibility
  • Predictable output format
  • More fine-grained control

When to use: Shell scripts, automation, legacy systems

Pro Tip: For faster terminal navigation and workflow efficiency, check out our Ubuntu keyboard shortcuts cheatsheet to boost your productivity while managing packages.

Essential Package Management Commands

Updating Package Lists

Always update package lists before installing or upgrading:

# Update package index
sudo apt update

# Or with apt-get
sudo apt-get update

This command refreshes the local database with information about available packages and their versions from configured repositories.

Installing Packages

Basic Installation:

# Install single package
sudo apt install package-name

# Install multiple packages
sudo apt install package1 package2 package3

# Install specific version
sudo apt install package-name=version-number

Examples:

# Install Nginx web server
sudo apt install nginx

# Install Python and pip
sudo apt install python3 python3-pip

# Install build essentials
sudo apt install build-essential

# Install system monitoring tools
sudo apt install htop

For specialized monitoring needs, such as tracking GPU performance on systems with NVIDIA cards, check out our guide on GPU monitoring applications in Linux/Ubuntu.

Install without prompts (useful for scripts):

sudo apt install -y package-name

# Or
sudo DEBIAN_FRONTEND=noninteractive apt install -y package-name

Install from .deb file:

# Method 1: Using apt (recommended, handles dependencies)
sudo apt install ./package.deb

# Method 2: Using dpkg
sudo dpkg -i package.deb
sudo apt install -f  # Fix missing dependencies

Note: When working with downloaded .deb files, you may want to use a file manager with a good context menu. See our comparison of file managers for Ubuntu 24.04 (Nautilus vs Nemo vs Dolphin vs Caja) to find one that suits your workflow.

Upgrading Packages

Upgrade Installed Packages:

# Upgrade all packages (safe, won't remove packages)
sudo apt upgrade

# Upgrade specific package
sudo apt install --only-upgrade package-name

# Full upgrade (may remove packages to resolve dependencies)
sudo apt full-upgrade

# Distribution upgrade (for major version updates)
sudo apt dist-upgrade

Complete Update Sequence:

# Recommended update procedure
sudo apt update
sudo apt upgrade -y
sudo apt autoremove
sudo apt autoclean

Upgrade Ubuntu Release:

# For LTS to next LTS
sudo do-release-upgrade

# For any release upgrade
sudo do-release-upgrade -d

Listing Packages

List Installed Packages:

# All installed packages
apt list --installed

# With grep filter
apt list --installed | grep package-name

# Count installed packages
apt list --installed | wc -l

# Using dpkg
dpkg -l
dpkg --list
dpkg -l | grep package-name

List Upgradable Packages:

# Show packages with available updates
apt list --upgradable

# Detailed upgrade simulation
apt list --upgradable -a

List All Available Packages:

# All packages in repositories
apt list

# List from specific repository
apt list | grep "source-name"

Searching for Packages

Search Package Names and Descriptions:

# Search with apt
apt search keyword

# Case-insensitive search
apt search -i keyword

# Show package names only
apt-cache search keyword

# Search package names only (faster)
apt-cache search --names-only keyword

Examples:

# Search for editor
apt search text editor

# Search for database
apt search database

# Search for Python packages
apt search python | grep python3

Viewing Package Information

Detailed Package Information:

# Show package details
apt show package-name

# Using apt-cache
apt-cache show package-name

# Show package dependencies
apt-cache depends package-name

# Show reverse dependencies
apt-cache rdepends package-name

# Check package policy and versions
apt-cache policy package-name

# Show package stats
apt-cache stats

Check if Package is Installed:

# Method 1
dpkg -s package-name

# Method 2
dpkg -l package-name

# Method 3
apt list --installed package-name

# Method 4 (exit code 0 if installed)
dpkg-query -W package-name

Removing Packages

Remove Packages:

# Remove package (keep configuration files)
sudo apt remove package-name

# Remove package and configuration files (purge)
sudo apt purge package-name

# Remove with dependencies
sudo apt autoremove package-name

# Completely remove package and configs
sudo apt purge package-name && sudo apt autoremove

Clean Up System:

# Remove unused dependencies
sudo apt autoremove

# Remove with purge
sudo apt autoremove --purge

# Delete downloaded package files
sudo apt clean

# Delete old versions of packages
sudo apt autoclean

Check Disk Usage:

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

# Check total package size
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n

Package Holds (Prevent Upgrades)

Hold Packages:

# Hold package at current version
sudo apt-mark hold package-name

# Unhold package
sudo apt-mark unhold package-name

# Show held packages
apt-mark showhold

# Hold multiple packages
sudo apt-mark hold package1 package2 package3

Use Case: Prevent specific software versions from being upgraded automatically.

Using dpkg - Low-Level Package Tool

dpkg is the underlying package manager that APT uses:

Basic dpkg Commands:

# Install package
sudo dpkg -i package.deb

# Remove package
sudo dpkg -r package-name

# Purge package
sudo dpkg -P package-name

# List installed packages
dpkg -l

# List files in package
dpkg -L package-name

# Find which package owns a file
dpkg -S /path/to/file

# Check package status
dpkg -s package-name

# Reconfigure package
sudo dpkg-reconfigure package-name

# Verify package integrity
dpkg -V package-name

Fix Broken Packages:

# Reconfigure broken packages
sudo dpkg --configure -a

# Fix missing dependencies
sudo apt install -f

# Force reinstall
sudo apt install --reinstall package-name

Repository Management

Viewing Repositories

Check Configured Repositories:

# View sources list
cat /etc/apt/sources.list

# View additional sources
ls /etc/apt/sources.list.d/

# View all enabled repos
grep -r --include '*.list' '^deb ' /etc/apt/

Adding Repositories

Add PPA (Personal Package Archive):

# Add PPA repository
sudo add-apt-repository ppa:user/ppa-name
sudo apt update

# Example: Add Git PPA
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git

Add Repository Manually:

# Add repository to sources.list
echo "deb http://repository-url distribution component" | sudo tee -a /etc/apt/sources.list.d/custom.list

# Add GPG key
wget -qO - https://example.com/key.gpg | sudo apt-key add -

# Update package lists
sudo apt update

Modern Key Management (Ubuntu 22.04+):

# Download and add key to keyring
wget -qO - https://example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/custom.gpg

# Add signed repository
echo "deb [signed-by=/etc/apt/keyrings/custom.gpg] http://repo-url dist component" | sudo tee /etc/apt/sources.list.d/custom.list

Removing Repositories

# Remove PPA
sudo add-apt-repository --remove ppa:user/ppa-name

# Or delete the list file
sudo rm /etc/apt/sources.list.d/ppa-name.list

# Update
sudo apt update

Alternative Package Managers

Ubuntu also supports Snap and Flatpak for universal application packaging:

  • Snap: See our comprehensive Snap Package Manager Guide for detailed information on installation, usage, channels, confinement, and troubleshooting.

  • Flatpak: See our comprehensive Flatpak Package Manager Guide for detailed information on installation, Flathub, permissions, runtimes, and management.

Advanced Package Management

Simulate Operations (Dry Run)

# Simulate install
apt install -s package-name

# Simulate upgrade
apt upgrade -s

# Show what would be installed
apt-cache showpkg package-name

Download Packages Without Installing

# Download package only
apt download package-name

# Download with dependencies
apt install --download-only package-name

# Download to specific directory
cd /path/to/dir
apt download package-name

Find Package Files

# Which package provides a file
dpkg -S /path/to/file

# Search for files in packages (including not installed)
apt-file search filename

# Install apt-file first
sudo apt install apt-file
sudo apt-file update

Package Pinning

Create /etc/apt/preferences to control package versions:

# Pin package to specific version
sudo nano /etc/apt/preferences

Add:

Package: package-name
Pin: version 1.2.3*
Pin-Priority: 1001

Check Package Vulnerabilities

# Check for security updates
sudo apt list --upgradable | grep security

# Check specific package for CVEs
apt-cache policy package-name

Troubleshooting Common Issues

Fix Broken Packages

# Method 1: Fix dependencies
sudo apt install -f

# Method 2: Reconfigure packages
sudo dpkg --configure -a

# Method 3: Clean and update
sudo apt clean
sudo apt update
sudo apt upgrade

# Method 4: Force fix
sudo apt --fix-broken install

Fix “Locked” Database

# Remove lock files (only if no apt process is running!)
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
sudo dpkg --configure -a

Check for running processes first:

ps aux | grep -i apt
sudo lsof /var/lib/dpkg/lock-frontend

Hash Sum Mismatch Error

# Clean cache and retry
sudo apt clean
sudo rm -rf /var/lib/apt/lists/*
sudo apt update

GPG/Key Errors

# Re-add key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEY_ID

# Or download key
wget -qO - https://repo-url/key.gpg | sudo apt-key add -

# Modern approach (Ubuntu 22.04+)
wget -qO - https://repo-url/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/repo.gpg

Unmet Dependencies

# Try aptitude (better dependency resolver)
sudo apt install aptitude
sudo aptitude install package-name

# Or manual resolution
sudo apt install package-name package-dependency

Repository Not Found (404)

# Update to correct repository
sudo nano /etc/apt/sources.list

# Change old release name to current
# Or remove outdated PPA
sudo add-apt-repository --remove ppa:name/ppa

Best Practices

Regular Maintenance

Weekly Routine:

#!/bin/bash
# System update script

echo "Updating package lists..."
sudo apt update

echo "Upgrading packages..."
sudo apt upgrade -y

echo "Removing unused packages..."
sudo apt autoremove -y

echo "Cleaning package cache..."
sudo apt autoclean

echo "Update complete!"

Security Updates

Auto-updates for Security Patches:

# Install unattended-upgrades
sudo apt install unattended-upgrades

# Configure
sudo dpkg-reconfigure unattended-upgrades

# Edit configuration
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Server Configuration: If you’re managing Ubuntu Server, proper network configuration is essential for reliable package downloads and system updates. Learn how to change a static IP address in Ubuntu Server to ensure stable connectivity for your package management operations.

Backup Package List

Export Installed Packages:

# Create backup
dpkg --get-selections > ~/package-list.txt

# Or with explicit installed packages
apt-mark showmanual > ~/manually-installed.txt

# Restore on another system
sudo dpkg --set-selections < ~/package-list.txt
sudo apt-get dselect-upgrade

Check System Health

# Check for broken dependencies
sudo apt check

# Verify all packages
sudo debsums -c

# Check system logs
journalctl -xe | grep -i apt
tail -f /var/log/apt/history.log

Performance Optimization

Faster Package Downloads

Enable Parallel Downloads:

Edit /etc/apt/apt.conf.d/99parallel:

echo 'Acquire::Queue-Mode "host";' | sudo tee /etc/apt/apt.conf.d/99parallel
echo 'Acquire::http::Pipeline-Depth "5";' | sudo tee -a /etc/apt/apt.conf.d/99parallel

Use Fastest Mirror:

# Install apt-fast
sudo add-apt-repository ppa:apt-fast/stable
sudo apt update
sudo apt install apt-fast

# Use apt-fast instead of apt
sudo apt-fast install package-name

Reduce Cache Size

# Limit cache to 100MB
echo 'APT::Archives::MaxSize "100";' | sudo tee /etc/apt/apt.conf.d/99max-cache

Useful Package Management Scripts

Update All Package Managers

#!/bin/bash
# Update all package managers

echo "=== APT Update ==="
sudo apt update && sudo apt upgrade -y

echo "=== Snap Update ==="
sudo snap refresh

echo "=== Flatpak Update ==="
flatpak update -y

echo "All systems updated!"

Find Large Packages

# List largest installed packages
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nr | head -20

# Or more readable format
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nr | head -20 | awk '{printf "%.2f MB\t%s\n", $1/1024, $2}'

Package Audit

#!/bin/bash
# Package system audit

echo "=== System Package Statistics ==="
echo "Installed packages: $(dpkg -l | grep ^ii | wc -l)"
echo "Upgradable packages: $(apt list --upgradable 2>/dev/null | grep -c upgradable)"
echo "Held packages: $(apt-mark showhold | wc -l)"
echo "Auto-removable: $(apt autoremove --dry-run | grep -Po '^\d+')"
echo "Cache size: $(du -sh /var/cache/apt/archives | cut -f1)"

Quick Reference Cheatsheet

Essential Commands:

# Update & Upgrade
sudo apt update && sudo apt upgrade -y

# Install package
sudo apt install package-name

# Remove package
sudo apt remove package-name

# Search package
apt search keyword

# Show package info
apt show package-name

# List installed
apt list --installed

# Clean system
sudo apt autoremove && sudo apt autoclean

# Fix broken packages
sudo apt install -f && sudo dpkg --configure -a

Conclusion

Mastering Ubuntu’s package management system is essential for efficient system administration. Whether you’re using the modern apt command for interactive sessions, apt-get for scripting, or exploring universal packages with Snap and Flatpak, understanding these tools will help you maintain a secure, up-to-date, and well-organized Ubuntu system.

Start with the basic commands, gradually incorporate advanced techniques, and always keep your system updated. Regular maintenance, combined with best practices, ensures a smooth and reliable Ubuntu experience.

Pro Tip: Always run sudo apt update before installing or upgrading packages to ensure you’re working with the latest package information!