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 upgradesRepository 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
EOFTroubleshooting
# 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 fileTips and Tricks
- Always run
apt updatebeforeapt installto get latest package index - Use
apt(notapt-get) for interactive use โ it has progress bars and colors - Use
apt-getin scripts โ its output is more stable for parsing - Use
unattended-upgradesfor automatic security updates - Use
needrestartto check if services need restarting after updates