π§° 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#
1
2
3
4
5
6
7
8
9
10
11
12
| +---------------------+
| Package Manager | β Tool like APT or DNF
+---------------------+
|
+-------+-------+
| |
Install Remove β Common actions
| |
v v
+------+ +------+
| .deb | | .rpm | β Package formats for Debian and Red Hat-based systems
+------+ +------+
|
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#
1
2
3
4
| 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 update
before installing or upgrading to avoid stale metadata.
πΉ Package Installation & Removal#
1
2
3
| 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
|
purge
is helpful when you want to reset package settings too.
πΉ Search & Inspect Packages#
1
2
3
4
| 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#
1
| [apt update] β [apt install] β [apt upgrade] β [apt autoremove] β [apt clean]
|
A good habit is to follow this lifecycle monthly to maintain system hygiene.
1
2
3
| 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)#
1
| apt install --simulate <pkg>
|
Prevent accidental installations during script testing or before deploying changes.
πΉ Download Without Installing#
Useful if you’re collecting packages for offline use or pre-installation review.
πΉ Reinstall a Package#
1
| sudo apt install --reinstall <pkg>
|
This can help fix packages with corrupted or missing files.
πΉ Hold/Unhold a Package (Prevent Auto-upgrade)#
1
2
| sudo apt-mark hold <pkg> # Lock package to current version
sudo apt-mark unhold <pkg> # Allow package to be upgraded again
|
πΉ Show Dependency Tree#
1
2
| 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#
1
| apt-cache rdepends <pkg>
|
Helps find out what will break if you remove a package.
πΉ Verify Which Package Owns a File#
Useful in tracing down file origins or checking if a tool is part of a package.
πΉ List Package Content#
Lists all files and locations installed by a package.
πΉ Query Package from a Command#
1
| 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#
1
2
| sudo apt install apt-file # Install apt-file tool
sudo apt-file update # Download file index from repositories
|
πΉ Usage#
1
| apt-file search <filename>
|
Example: apt-file search traceroute
will show the package that provides the traceroute binary.
π apt-file Use Case Diagram#
1
| [filename] β [apt-file search] β [package that provides it]
|
dpkg
is the backend tool used by apt
. It’s useful for working with local .deb
packages directly.
πΉ Install a Local .deb File#
1
2
| sudo dpkg -i <file>.deb # Install a package from local disk
sudo apt -f install # Fix broken or missing dependencies
|
πΉ Query Installed Packages#
1
| dpkg -l # List all installed packages
|
πΉ List Files Installed by a Package#
1
| dpkg -L <pkg> # Show where the package files are located
|
πΉ Remove a Package (Low-level)#
π΄ 5. DNF β RHEL/Fedora/CentOS Systems#
DNF is the front-end for RPM-based systems, used in Red Hat, CentOS, Fedora.
πΉ Update & Upgrade#
1
2
| sudo dnf check-update # Check for available updates
sudo dnf upgrade # Upgrade all installed packages
|
πΉ Install/Remove/Search#
1
2
3
4
| 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#
1
2
| dnf deplist <pkg> # View package dependencies
dnf provides */<command> # Find which package provides a given file or binary
|
πΉ Clean Cache#
1
| 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#
1
| sudo rpm -ivh <file>.rpm # Install a package with verbose output and hash progress
|
πΉ Remove a Package#
πΉ Query Info, Files, Owner#
1
2
3
| 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#
1
| sudo apt -f install # Automatically fix and install missing dependencies
|
πΉ Simulate an Upgrade#
1
| apt-get upgrade --simulate # Dry run to test if upgrade will succeed
|
πΉ Check for Held Packages#
1
| apt-mark showhold # Show packages on hold (not upgraded)
|
πΉ Compare Installed Version vs Available#
1
| 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.