Skip to main content
๐ŸŽ“ Claude Code Masterclass Learn AI-assisted development on Udemy โ€” plus the companion book on Leanpub & Amazon. Start Learning
apt Cheat Sheet: Ubuntu/Debian Package Management
DevOps

apt Cheat Sheet: Ubuntu/Debian Package Management

APT cheat sheet for Ubuntu and Debian. Install, update, remove, search, pin versions, manage repos, and troubleshoot broken packages โ€” all essential commands.

LB
Luca Berton
ยท 1 min read

A quick reference for APT โ€” the Debian/Ubuntu package manager. Bookmark this page.

Package Management

# Update package index
sudo apt update

# Upgrade all packages
sudo apt upgrade
sudo apt full-upgrade        # Also removes obsolete packages

# Install package
sudo apt install nginx
sudo apt install nginx=1.24.0-1  # Specific version
sudo apt install -y nginx    # Auto-confirm

# Remove package
sudo apt remove nginx        # Keep config files
sudo apt purge nginx         # Remove config files too
sudo apt autoremove          # Remove unused dependencies

# Search
apt search "web server"
apt show nginx               # Package details
apt list --installed         # All installed packages
apt list --upgradable        # Available upgrades

Repository Management

# Add repository
sudo add-apt-repository ppa:deadsnakes/ppa
sudo add-apt-repository "deb https://repo.example.com/apt stable main"

# Remove repository
sudo add-apt-repository --remove ppa:deadsnakes/ppa

# Add GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg

# Add repo with signed-by
echo "deb [signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |   sudo tee /etc/apt/sources.list.d/docker.list

# List repositories
grep -r "^deb " /etc/apt/sources.list /etc/apt/sources.list.d/

Package Pinning and Holding

# Hold package (prevent upgrades)
sudo apt-mark hold nginx
sudo apt-mark unhold nginx
apt-mark showhold

# Pin package version
cat <<EOF | sudo tee /etc/apt/preferences.d/nginx
Package: nginx
Pin: version 1.24.*
Pin-Priority: 1000
EOF

Troubleshooting

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

# Reconfigure package
sudo dpkg-reconfigure tzdata

# Force install a .deb file
sudo dpkg -i package.deb
sudo apt --fix-broken install  # Fix deps after dpkg

# Clean cache
sudo apt clean               # Remove all cached packages
sudo apt autoclean           # Remove old cached packages

# Check package files
dpkg -L nginx                # List files in package
dpkg -S /usr/sbin/nginx      # Which package owns this file

Tips and Tricks

  • Always run apt update before apt install to get latest package index
  • Use apt (not apt-get) for interactive use โ€” it has progress bars and colors
  • Use apt-get in scripts โ€” its output is more stable for parsing
  • Use unattended-upgrades for automatic security updates
  • Use needrestart to check if services need restarting after updates

Free 30-min AI & Cloud consultation

Book Now