Package Management (apt, yum, dnf)
Category: Intermediate Linux Commands
Type: Linux Commands
Generated on: 2025-07-10 03:09:27
For: System Administration, Development & Technical Interviews
Package Management Cheatsheet (apt, yum, dnf)
Section titled “Package Management Cheatsheet (apt, yum, dnf)”This cheatsheet provides a comprehensive guide to package management using apt (Debian/Ubuntu), yum (CentOS/RHEL 7), and dnf (CentOS/RHEL 8+). It is designed for both system administrators and developers, covering basic and advanced usage, troubleshooting, and practical examples.
1. Command Overview
apt(Advanced Package Tool): The primary package manager for Debian-based systems (Ubuntu, Mint, etc.). It handles installing, updating, removing, and managing software packages.yum(Yellowdog Updater, Modified): The package manager for older Red Hat-based systems (CentOS 7, RHEL 7). It’s now largely superseded bydnf.dnf(Dandified YUM): The next-generation package manager for newer Red Hat-based systems (CentOS 8+, RHEL 8+). It’s faster and more efficient thanyum.
When to Use:
- Installing new software: When you need to add a program or library to your system.
- Updating existing software: To apply security patches and bug fixes.
- Removing software: To uninstall programs you no longer need.
- Managing dependencies: To ensure that all required libraries and components are installed.
- Resolving conflicts: To fix broken dependencies and other package-related issues.
2. Basic Syntax
apt
sudo apt [command] [package_name]yum
sudo yum [command] [package_name]dnf
sudo dnf [command] [package_name]3. Practical Examples
Installing a Package:
# aptsudo apt install nginx# yumsudo yum install nginx# dnfsudo dnf install nginxExpected Output (similar across all three, variations in progress bar and messages)
[sudo] password for user:Reading package lists... DoneBuilding dependency tree... DoneReading state information... DoneThe following NEW packages will be installed: nginx0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Need to get 1.2 MB of archives.After this operation, 3.0 MB of additional disk space will be used.Do you want to continue? [Y/n] yGet:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 nginx amd64 1.18.0-6ubuntu14.2 [1.2 MB]Fetched 1.2 MB in 1s (1.2 MB/s)Selecting previously unselected package nginx.(Reading database ... 100000 files and directories currently installed.)Preparing to unpack .../nginx_1.18.0-6ubuntu14.2_amd64.deb ...Unpacking nginx (1.18.0-6ubuntu14.2) ...Setting up nginx (1.18.0-6ubuntu14.2) ...Processing triggers for systemd (249.11-0ubuntu3.10) ...Processing triggers for man-db (2.10.2-1) ...Removing a Package:
# aptsudo apt remove nginx# yumsudo yum remove nginx# dnfsudo dnf remove nginxUpdating Package Lists:
# aptsudo apt update# yumsudo yum update # No update needed for repo metadata, only package updates# dnfsudo dnf updateExpected Output for apt update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]Get:2 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]Get:4 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [80.1 kB]Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [108 kB]Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1390 kB]Get:7 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [906 kB]Get:8 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [12.5 MB]Get:9 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1050 kB]Get:10 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1069 kB]Get:11 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [47.7 kB]Get:12 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [25.6 kB]Fetched 17.7 MB in 2s (8787 kB/s)Reading package lists... DoneBuilding dependency tree... DoneReading state information... Done6 packages can be upgraded. Run 'apt list --upgradable' to see them.Upgrading Packages:
# aptsudo apt upgrade# yumsudo yum update# dnfsudo dnf upgradeSearching for a Package:
# aptapt search nginx# yumyum search nginx# dnfdnf search nginxGetting Package Information:
# aptapt show nginx# yumyum info nginx# dnfdnf info nginx4. Common Options
apt
install: Install a package.remove: Remove a package (but keep configuration files).purge: Remove a package and its configuration files. (USE WITH CAUTION!)update: Update the package lists from the repositories.upgrade: Upgrade all upgradable packages.autoremove: Remove automatically installed dependencies that are no longer needed.autoclean: Clean up the local repository of retrieved package files.show: Display detailed information about a package.search: Search for a package.-yor--yes: Automatically answer “yes” to prompts. Use with caution, especially withremoveandpurge.--no-install-recommends: Prevent installation of recommended packages.--fix-broken: Attempts to correct broken dependencies.
yum/dnf
install: Install a package.remove: Remove a package.update: Update packages.search: Search for a package.info: Display detailed information about a package.list: List installed or available packages.repolist: List enabled repositories.clean all: Cleans all cached package data.autoremove: Remove automatically installed dependencies that are no longer needed.-yor--assumeyes: Automatically answer “yes” to prompts. Use with caution, especially withremove.--enablerepoor--disablerepo: Enable or disable specific repositories.--refresh: Force metadata refresh before performing the operation.
5. Advanced Usage
apt
-
Installing a specific version:
Terminal window sudo apt install nginx=1.18.0-6ubuntu14.2 -
Pinning a package version (preventing updates):
Create a file
/etc/apt/preferences.d/nginxwith the following content:Package: nginxPin: version 1.18.0-6ubuntu14.2Pin-Priority: 1001 -
Adding a PPA (Personal Package Archive):
Terminal window sudo add-apt-repository ppa:certbot/certbot # Example: Certbot PPAsudo apt updatesudo apt install certbot -
Fixing Broken Packages:
Terminal window sudo apt --fix-broken install
yum/dnf
-
Installing from a local RPM file:
Terminal window sudo yum install /path/to/package.rpm # yumsudo dnf install /path/to/package.rpm # dnf -
Listing available updates:
Terminal window yum check-update # yumdnf check-update # dnf -
Enabling/Disabling Repositories:
Terminal window sudo yum --enablerepo=epel install <package_name> # yumsudo dnf --enablerepo=epel install <package_name> # dnfsudo yum --disablerepo=epel update # yumsudo dnf --disablerepo=epel update # dnf -
Downgrading a Package:
Terminal window sudo dnf downgrade <package_name> -
Listing all repositories:
Terminal window yum repolist all #yumdnf repolist all #dnf
6. Tips & Tricks
-
Update and Upgrade in one command:
yum/dnf # aptsudo apt update && sudo apt upgrade -ysudo yum update -y # yumsudo dnf upgrade -y # dnf -
Use aliases: Create aliases in your
.bashrcor.zshrcfor frequently used commands. For example:Terminal window alias up='sudo apt update && sudo apt upgrade -y' # aptalias up='sudo dnf upgrade -y' # dnf -
History: Use command history (up/down arrows) and tab completion to speed up typing.
-
Dry Run (dnf only): Use
--dry-runto preview changes without actually making them. This is useful for testing updates or installations.Terminal window sudo dnf upgrade --dry-run -
Fastest Mirror (dnf only):
dnfautomatically selects the fastest mirror, but you can force a re-evaluation with:Terminal window sudo dnf makecache
7. Troubleshooting
-
“Could not get lock” error: Another package manager or process is already running. Wait for it to finish or try:
Terminal window sudo fuser -v /var/lib/dpkg/lock # apt - Identify the process holding the locksudo kill <PID> # apt - Kill the process (USE WITH CAUTION!)sudo rm /var/lib/apt/lists/lock # apt - Remove the lock file (USE WITH CAUTION!)sudo rm /var/cache/apt/archives/lock # apt - Remove another lock file (USE WITH CAUTION!)sudo rm /var/run/yum.pid # yum/dnf - If yum or dnf crashedsudo rm /var/run/dnf.pid # yum/dnf - If yum or dnf crashed -
“Package not found” error: The package name is incorrect, or the repository containing the package is not enabled. Double-check the spelling and ensure the repository is active. For
apt, runsudo apt update. Foryum/dnf, check the repository list usingyum repolistordnf repolist. -
Dependency errors: The package requires other packages that are not installed or are conflicting. Try:
Terminal window # aptsudo apt --fix-broken installsudo apt install -f # Force installation, resolving dependencies# yum/dnfsudo yum install yum-utils # yum - Install yum-utils if not already installedsudo yum deplist <package_name> # yum - List package dependenciessudo dnf install <package_name> --allowerasing # dnf - Try to resolve by removing conflicting packages (USE WITH CAUTION!) -
Slow downloads: The mirror you are using might be slow. Try changing mirrors by editing
/etc/apt/sources.list(forapt) or/etc/yum.repos.d/(foryum/dnf). Fordnf, thefastestmirrorplugin helps automatically select the fastest mirror. -
Corrupted Package Database: If you suspect corruption, try:
Terminal window # aptsudo rm -rf /var/lib/apt/lists/*sudo apt update# yumsudo yum clean allsudo yum makecache fast# dnfsudo dnf clean allsudo dnf makecache -
RPM Database Corruption (yum/dnf):
Terminal window sudo rpm --rebuilddb
8. Related Commands
dpkg(Debian Package Manager): The low-level package manager used byapt. Generally, you should useaptinstead ofdpkgdirectly.rpm(RPM Package Manager): The low-level package manager used byyumanddnf. Generally, you should useyumordnfinstead ofrpmdirectly.add-apt-repository: Adds a Personal Package Archive (PPA) to your system.apt-cache: Queries theaptcache. Useful for finding package versions, dependencies, and other information.
This cheatsheet provides a solid foundation for managing packages on Linux systems using apt, yum, and dnf. Remember to consult the official documentation for each command for more detailed information and options. Always exercise caution when using commands that modify system files or remove packages.