Featured image of post Apt

Apt

Advanced Package Tool for Debian-based Linux distributions

Reference

Hook

Imagine you’re trying to install a new software on your Linux system, but you have to manually download packages, resolve dependencies, and configure everything by hand. This process can quickly become overwhelming and error-prone. The Advanced Package Tool (APT) was created to solve these problems by providing an easy and automated way to manage software packages on Debian-based systems.

Feature

APT (Advanced Package Tool) is a powerful command-line package management system used in Debian and its derivatives like Ubuntu. It simplifies the process of installing, updating, and removing software packages while automatically handling dependencies. APT maintains a database of available packages from configured repositories and ensures system integrity by resolving dependencies and conflicts.

Reference

Quick Start

# Update package index (downloads latest package information)
sudo apt update

# Upgrade all installed packages to their latest versions
sudo apt upgrade

# Install a package / include local deb docs
sudo apt install <package_name>

# Remove a package (keeps configuration files)
sudo apt remove <package_name>

# Completely remove a package and its configuration files
sudo apt purge <package_name>

# Search for packages
apt search <keyword>

Common Commands

# Update package list and upgrade all packages
sudo apt update && sudo apt upgrade -y

# Install multiple packages
sudo apt install package1 package2 package3

# Install a package without confirmation prompts
sudo apt install -y <package_name>

# Show package information
apt show <package_name>

# List all installed packages
apt list --installed

# List upgradable packages
apt list --upgradable

# Remove unused dependencies
sudo apt autoremove

# Clean package cache (removes downloaded .deb files)
sudo apt clean

# Clean package cache (removes outdated .deb files)
sudo apt autoclean

Always run sudo apt update before installing new packages to ensure you’re getting the latest versions.

Advanced Commands & Features

Package Search and Information

# Search for packages by name or description
apt search <keyword>

# Show detailed information about a package
apt show <package_name>

# List all available versions of a package
apt list -a <package_name>

# Show installed packages matching a pattern
apt list --installed | grep <pattern>

Package Installation Options

# Install a specific version of a package
sudo apt install <package_name>=<version>

# Reinstall a package
sudo apt reinstall <package_name>

# Simulate an installation (dry run)
sudo apt install --dry-run <package_name>

# Install recommended packages automatically
sudo apt install --install-recommends <package_name>

# Install without recommended packages
sudo apt install --no-install-recommends <package_name>

System Maintenance

# Full system upgrade (may remove/install packages)
sudo apt full-upgrade

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

# Fix missing packages
sudo apt --fix-missing update

# Download package files without installing
sudo apt download <package_name>

# Check for broken dependencies
sudo apt check

Repository Management

# Add a repository
sudo add-apt-repository <repository>

# Add a repository without prompting
sudo add-apt-repository -y <repository>

# Remove a repository
sudo add-apt-repository --remove <repository>

# List all configured repositories
apt policy

# Update package list for a specific repository
sudo apt update -o Dir::Etc::sourcelist="sources.list.d/<repo>.list" \
                -o Dir::Etc::sourceparts="-" \
                -o APT::Get::List-Cleanup="0"

Package Pinning (Holding Package Versions)

# Hold a package at its current version (prevent upgrades)
sudo apt-mark hold <package_name>

# Unhold a package (allow upgrades again)
sudo apt-mark unhold <package_name>

# Show held packages
apt-mark showhold

# Mark a package as automatically installed
sudo apt-mark auto <package_name>

# Mark a package as manually installed
sudo apt-mark manual <package_name>

Configuration Files

APT configuration files are located in /etc/apt/:

  • /etc/apt/sources.list - Main repository configuration
  • /etc/apt/sources.list.d/ - Additional repository files
  • /etc/apt/apt.conf - Main APT configuration
  • /etc/apt/apt.conf.d/ - Configuration snippets
  • /etc/apt/preferences - Package pinning preferences
  • /etc/apt/preferences.d/ - Additional pinning files

Best Practices

  1. Regular Updates: Run sudo apt update regularly to keep package information current
  2. System Upgrades: Use sudo apt full-upgrade instead of upgrade for major system updates
  3. Clean Cache: Periodically run sudo apt autoclean to free disk space
  4. Remove Unused Packages: Run sudo apt autoremove to remove unnecessary dependencies
  5. Backup Configuration: Before major upgrades, backup important configuration files
  6. Check Before Installing: Use apt show to review package details before installation

Troubleshooting

Common Issues and Solutions

  1. GPG Errors:

    # Fix missing GPG keys
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <KEY_ID>
    
  2. Repository Issues:

    # Fix repository errors
    sudo apt clean
    sudo apt update --fix-missing
    
  3. Broken Packages:

    # Fix broken dependencies
    sudo apt --fix-broken install
    sudo dpkg --configure -a
    
  4. Lock Errors:

    # Remove lock files (only if no other apt process is running)
    sudo rm /var/lib/apt/lists/lock
    sudo rm /var/cache/apt/archives/lock
    sudo rm /var/lib/dpkg/lock*
    

Always be cautious when removing lock files. Make sure no other package manager is running.

A winner is just a loser who tried one more time.
Robust AI
使用 Hugo 构建
主题 StackJimmy 设计