Skip to content

APT Package Manager

Comprehensive APT (Advanced Package Tool) commands and workflows for Debian and Ubuntu package management.

Installation & Updates

Basic Package Operations

CommandDescription
sudo apt updateUpdate package index
sudo apt upgradeUpgrade all packages
sudo apt full-upgradeUpgrade with dependency resolution
sudo apt install package_nameInstall package
sudo apt remove package_nameRemove package
sudo apt purge package_nameRemove package and config files
sudo apt autoremoveRemove unused dependencies

Advanced Installation

CommandDescription
sudo apt install package1 package2Install multiple packages
sudo apt install package=versionInstall specific version
sudo apt install ./package.debInstall local .deb file
sudo apt reinstall package_nameReinstall package
sudo apt install --no-install-recommends packageInstall without recommended packages

Package Information

Search and Information

CommandDescription
apt search keywordSearch for packages
apt show package_nameShow package information
apt list --installedList installed packages
apt list --upgradableList upgradable packages
apt list package_nameCheck if package is available
dpkg -lList all installed packages
`dpkg -lgrep package`

Package Dependencies

CommandDescription
apt depends package_nameShow package dependencies
apt rdepends package_nameShow reverse dependencies
apt-cache policy package_nameShow package policy

Repository Management

Repository Operations

CommandDescription
sudo add-apt-repository ppa:user/repoAdd PPA repository
sudo add-apt-repository --remove ppa:user/repoRemove PPA repository
sudo apt edit-sourcesEdit sources list
ls /etc/apt/sources.list.d/List additional repositories

GPG Keys

CommandDescription
sudo apt-key listList GPG keys
wget -qO - https://example.com/key.gpg | sudo apt-key add -Add GPG key
sudo apt-key del KEYIDRemove GPG key

System Maintenance

Cleanup Operations

CommandDescription
sudo apt autocleanClean package cache
sudo apt cleanRemove all cached packages
sudo apt autoremove --purgeRemove unused packages and configs
sudo apt-get checkCheck for broken dependencies

Fix Broken Packages

CommandDescription
sudo apt --fix-broken installFix broken dependencies
sudo dpkg --configure -aConfigure unconfigured packages
sudo apt-get -f installForce install to fix dependencies

Configuration Files

APT Configuration

FileDescription
/etc/apt/sources.listMain repository list
/etc/apt/sources.list.d/Additional repository files
/etc/apt/apt.confAPT configuration
/etc/apt/preferencesPackage pinning

Example sources.list

bash
# Ubuntu 22.04 LTS (Jammy Jellyfish)
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ jammy universe
deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted
deb http://security.ubuntu.com/ubuntu/ jammy-security universe
deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse

Package Pinning

Pin Package Version

bash
# /etc/apt/preferences.d/package-pin
Package: package_name
Pin: version 1.2.3*
Pin-Priority: 1001

Pin Repository

bash
# /etc/apt/preferences.d/repo-pin
Package: *
Pin: release o=Ubuntu,a=jammy-backports
Pin-Priority: 100

Advanced Usage

Simulation and Testing

CommandDescription
apt list --upgradable -aShow all available versions
sudo apt upgrade --dry-runSimulate upgrade
sudo apt install package --dry-runSimulate installation

Logging and History

CommandDescription
cat /var/log/apt/history.logView installation history
cat /var/log/apt/term.logView detailed logs
grep " install " /var/log/dpkg.logView dpkg install log

Troubleshooting

Common Issues

ProblemSolution
Package conflictssudo apt --fix-broken install
Locked databasesudo rm /var/lib/dpkg/lock*
Corrupted cachesudo apt clean && sudo apt update
Unmet dependenciessudo apt -f install

Emergency Recovery

CommandDescription
sudo dpkg --force-depends --remove packageForce remove problematic package
sudo apt-get download packageDownload package without installing
sudo dpkg -i --force-depends package.debForce install .deb file

Best Practices

Security

  1. Regular Updates: Run sudo apt update && sudo apt upgrade regularly
  2. Verify Sources: Only add trusted repositories
  3. GPG Verification: Ensure packages are properly signed
  4. Security Updates: Enable automatic security updates
  5. Backup: Backup important data before major upgrades

Performance

  1. Mirror Selection: Use fastest mirror for your location
  2. Parallel Downloads: Enable parallel downloads in apt.conf
  3. Cache Management: Regular cleanup of package cache
  4. Dependency Resolution: Use apt instead of apt-get for better output
  5. Network: Use reliable network connection for updates

Maintenance

  1. Regular Cleanup: Remove unused packages and dependencies
  2. Log Monitoring: Check logs for errors and warnings
  3. Disk Space: Monitor disk space in /var/cache/apt/
  4. Repository Health: Verify repository accessibility
  5. System Consistency: Regular dependency checks