Linux Package Management (APT, DNF)
π§° Linux Package Management β Complete Lifecycle Guide
Package management is central to maintaining, updating, and troubleshooting Linux systems. This guide covers everything you need to master package management on Debian/Ubuntu (APT, dpkg) and RHEL/CentOS/Fedora (DNF, rpm).
π¦ 1. Package Management Basics
| Concept | Description |
|---|---|
| Package | A compressed archive containing binary files, configuration, and metadata. |
| Package Manager | Tool to automate installing, updating, and removing packages. |
| Repositories | Remote servers hosting packages. |
| Dependencies | Additional packages required for a package to work. |
π§ These are the foundational components. Understanding them helps prevent common issues like broken dependencies and version conflicts.
πΈ Package Management Diagram
+---------------------+
| Package Manager | β Tool like APT or DNF
+---------------------+
|
+-------+-------+
| |
Install Remove β Common actions
| |
v v
+------+ +------+
| .deb | | .rpm | β Package formats for Debian and Red Hat-based systems
+------+ +------+
π’ 2. APT β Advanced Package Tool (Debian/Ubuntu)
APT is the front-end tool used to handle packages in Debian-based distributions. It works by communicating with repositories to fetch and manage .deb files.
πΉ Update & Upgrade System
sudo apt update # Fetch the latest list of available packages from repositories
sudo apt upgrade # Upgrade all upgradable packages to newer versions
sudo apt full-upgrade # Also handles package removals if needed to resolve dependencies
sudo apt dist-upgrade # Older version of full-upgrade
π Always run
apt updatebefore installing or upgrading to avoid stale metadata.
πΉ Package Installation & Removal
sudo apt install <pkg> # Install the specified package
sudo apt remove <pkg> # Uninstall package (but keep config files)
sudo apt purge <pkg> # Remove package and its configuration files
purgeis helpful when you want to reset package settings too.
πΉ Search & Inspect Packages
apt search <term> # Search the package database for names/descriptions
apt show <pkg> # Show full details of a package (version, dependencies, etc.)
apt list --installed # List all packages currently installed
apt list --upgradable # Show packages with available updates
π APT Lifecycle Flow
[apt update] β [apt install] β [apt upgrade] β [apt autoremove] β [apt clean]
A good habit is to follow this lifecycle monthly to maintain system hygiene.
πΉ Cleanup Tools
sudo apt autoremove # Remove packages that were installed as dependencies but are no longer needed
sudo apt autoclean # Remove outdated package files from local cache
sudo apt clean # Remove all package files from local cache
These free up space and keep the system tidy.
πΉ Dry Run a Package Installation (Simulation)
apt install --simulate <pkg>
Prevent accidental installations during script testing or before deploying changes.
πΉ Download Without Installing
apt download <pkg>
Useful if youβre collecting packages for offline use or pre-installation review.
πΉ Reinstall a Package
sudo apt install --reinstall <pkg>
This can help fix packages with corrupted or missing files.
πΉ Hold/Unhold a Package (Prevent Auto-upgrade)
sudo apt-mark hold <pkg> # Lock package to current version
sudo apt-mark unhold <pkg> # Allow package to be upgraded again
πΉ Show Dependency Tree
apt-cache depends <pkg> # Show what packages a given package depends on
apt-rdepends <pkg> # Show both dependencies and reverse dependencies
Use this to understand what will be affected by installing or removing packages.
πΉ List Reverse Dependencies
apt-cache rdepends <pkg>
Helps find out what will break if you remove a package.
πΉ Verify Which Package Owns a File
dpkg -S /bin/ls
Useful in tracing down file origins or checking if a tool is part of a package.
πΉ List Package Content
dpkg -L <pkg>
Lists all files and locations installed by a package.
πΉ Query Package from a Command
dpkg -S $(which <command>)
Quickly discover which package installed a specific command or binary.
π 3. apt-file β Find Package That Provides a File (Not Installed)
apt-file is helpful when youβre trying to install a missing command or binary, but you donβt know which package provides it.
πΉ Install and Update apt-file
sudo apt install apt-file # Install apt-file tool
sudo apt-file update # Download file index from repositories
πΉ Usage
apt-file search <filename>
Example:
apt-file search traceroutewill show the package that provides the traceroute binary.
π apt-file Use Case Diagram
[filename] β [apt-file search] β [package that provides it]
π§ 4. dpkg β Debian Package Tool (Low-level)
dpkg is the backend tool used by apt. Itβs useful for working with local .deb packages directly.
πΉ Install a Local .deb File
sudo dpkg -i <file>.deb # Install a package from local disk
sudo apt -f install # Fix broken or missing dependencies
πΉ Query Installed Packages
dpkg -l # List all installed packages
πΉ List Files Installed by a Package
dpkg -L <pkg> # Show where the package files are located
πΉ Remove a Package (Low-level)
sudo dpkg -r <pkg>
π΄ 5. DNF β RHEL/Fedora/CentOS Systems
DNF is the front-end for RPM-based systems, used in Red Hat, CentOS, Fedora.
πΉ Update & Upgrade
sudo dnf check-update # Check for available updates
sudo dnf upgrade # Upgrade all installed packages
πΉ Install/Remove/Search
sudo dnf install <pkg> # Install a package
sudo dnf remove <pkg> # Remove a package
dnf search <keyword> # Search for packages matching a keyword
dnf info <pkg> # Get detailed info of a package
πΉ Dependency and File Search
dnf deplist <pkg> # View package dependencies
dnf provides */<command> # Find which package provides a given file or binary
πΉ Clean Cache
sudo dnf clean all # Clear all downloaded package files and metadata
βοΈ 6. RPM β Red Hat Package Manager (Low-level)
RPM is like dpkgβa low-level tool for installing, removing, and querying .rpm packages directly.
πΉ Install .rpm Package
sudo rpm -ivh <file>.rpm # Install a package with verbose output and hash progress
πΉ Remove a Package
sudo rpm -e <pkg>
πΉ Query Info, Files, Owner
rpm -q <pkg> # Check if a package is installed
rpm -ql <pkg> # List files installed by a package
rpm -qf /path/to/file # Identify package that owns a specific file
π§ͺ 7. Miscellaneous & Troubleshooting
πΉ Fix Broken Packages
sudo apt -f install # Automatically fix and install missing dependencies
πΉ Simulate an Upgrade
apt-get upgrade --simulate # Dry run to test if upgrade will succeed
πΉ Check for Held Packages
apt-mark showhold # Show packages on hold (not upgraded)
πΉ Compare Installed Version vs Available
apt policy <pkg> # Compare current and candidate versions
β Summary Table
| Task | APT/Debian | DNF/Fedora |
|---|---|---|
| Install a package | apt install | dnf install |
| Remove a package | apt remove / purge | dnf remove |
| Query owning package of a command | dpkg -S / apt-file search | rpm -qf / dnf provides |
| Low-level package install | dpkg -i | rpm -i |
| List installed packages | dpkg -l / apt list | rpm -qa / dnf list |
π¬ Final Note
Mastering package management tools like apt, dpkg, dnf, and rpm is crucial for any Linux administrator or DevOps engineer. These skills allow you to audit systems, recover from package issues, automate environments, and perform forensic analysis of systems.