APT Reference

APT (Advanced Package Tool) is the package management system for Debian-based distributions.

Basic Operations

Install Packages

# Update package lists first
sudo apt update

# Install package
sudo apt install vim

# Install multiple packages
sudo apt install vim tmux htop

# Install without prompts
sudo apt install -y package

# Install specific version
sudo apt install package=1.2.3-1

Remove Packages

# Remove package (keep config)
sudo apt remove package

# Remove package and config
sudo apt purge package

# Remove with unused dependencies
sudo apt autoremove package

# Purge with autoremove
sudo apt purge --autoremove package

Update System

# Update package lists
sudo apt update

# Upgrade packages (safe)
sudo apt upgrade

# Upgrade with dependency changes
sudo apt full-upgrade

# Dist upgrade (major version)
sudo apt dist-upgrade

Querying Packages

Search Packages

# Search by name/description
apt search term

# Search package names only
apt search --names-only term

Package Information

# Package details
apt show package

# List package files (installed)
dpkg -L package

# Find which package owns file
dpkg -S /usr/bin/vim

# List package dependencies
apt depends package

# What depends on package
apt rdepends package

List Packages

# All installed
apt list --installed

# Upgradable
apt list --upgradable

# All available
apt list

# List with pattern
apt list 'vim*'

# Manually installed
apt-mark showmanual

Package State Management

Hold Packages

Prevent package from upgrading:

# Hold package
sudo apt-mark hold package

# Unhold package
sudo apt-mark unhold package

# List held packages
apt-mark showhold

Mark Packages

# Mark as manually installed
sudo apt-mark manual package

# Mark as automatically installed
sudo apt-mark auto package

# Show manual packages
apt-mark showmanual

# Show auto packages
apt-mark showauto

Cache Management

View Cache

# Cache location
ls /var/cache/apt/archives/

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

Clean Cache

# Remove all cached packages
sudo apt clean

# Remove outdated packages only
sudo apt autoclean

# Remove unused dependencies
sudo apt autoremove

Repository Management

sources.list

Main repository configuration at /etc/apt/sources.list:

# Debian 12 (Bookworm)
deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware

# Ubuntu 24.04 (Noble)
deb http://archive.ubuntu.com/ubuntu noble main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu noble-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu noble-security main restricted universe multiverse

Additional Sources

Files in /etc/apt/sources.list.d/:

# Example: /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable

Add Repository

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

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

# Update
sudo apt update

dpkg Commands

Low-level package operations:

Install/Remove

# Install from .deb file
sudo dpkg -i package.deb

# Remove package
sudo dpkg -r package

# Purge package
sudo dpkg -P package

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

Query

# List installed packages
dpkg -l

# List files in package
dpkg -L package

# Find package owning file
dpkg -S /path/to/file

# Package status
dpkg -s package

# List contents of .deb
dpkg -c package.deb

Reconfigure

# Reconfigure package
sudo dpkg-reconfigure package

# Reconfigure timezone
sudo dpkg-reconfigure tzdata

# Reconfigure locales
sudo dpkg-reconfigure locales

Configuration

APT Configuration

Files in /etc/apt/apt.conf.d/:

# Example: /etc/apt/apt.conf.d/99local
APT::Get::Assume-Yes "true";
APT::Install-Recommends "false";
Acquire::Languages "none";

Proxy Configuration

# /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://proxy:8080";
Acquire::https::Proxy "http://proxy:8080";

Troubleshooting

Common Issues

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

# Reconfigure dpkg
sudo dpkg --configure -a

# Force install
sudo apt install -f

# Clear cache and retry
sudo apt clean
sudo apt update

Lock Issues

# Check for running apt/dpkg
ps aux | grep -E 'apt|dpkg'

# Remove lock (if safe)
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*

# Reconfigure
sudo dpkg --configure -a

GPG Key Issues

# List keys
sudo apt-key list  # deprecated

# Add key (modern method)
curl -fsSL https://example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg

# Verify key
gpg --show-keys /etc/apt/keyrings/example.gpg

Useful Aliases

Add to ~/.bashrc:

alias apti='sudo apt install'
alias aptr='sudo apt remove'
alias aptu='sudo apt update && sudo apt upgrade'
alias apts='apt search'
alias aptl='apt list --installed'
alias aptc='sudo apt clean && sudo apt autoremove'