Skip to content

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 by dnf.
  • dnf (Dandified YUM): The next-generation package manager for newer Red Hat-based systems (CentOS 8+, RHEL 8+). It’s faster and more efficient than yum.

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

Terminal window
sudo apt [command] [package_name]

yum

Terminal window
sudo yum [command] [package_name]

dnf

Terminal window
sudo dnf [command] [package_name]

3. Practical Examples

Installing a Package:

Terminal window
# apt
sudo apt install nginx
# yum
sudo yum install nginx
# dnf
sudo dnf install nginx

Expected Output (similar across all three, variations in progress bar and messages)

[sudo] password for user:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nginx
0 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] y
Get: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:

Terminal window
# apt
sudo apt remove nginx
# yum
sudo yum remove nginx
# dnf
sudo dnf remove nginx

Updating Package Lists:

Terminal window
# apt
sudo apt update
# yum
sudo yum update # No update needed for repo metadata, only package updates
# dnf
sudo dnf update

Expected 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... Done
Building dependency tree... Done
Reading state information... Done
6 packages can be upgraded. Run 'apt list --upgradable' to see them.

Upgrading Packages:

Terminal window
# apt
sudo apt upgrade
# yum
sudo yum update
# dnf
sudo dnf upgrade

Searching for a Package:

Terminal window
# apt
apt search nginx
# yum
yum search nginx
# dnf
dnf search nginx

Getting Package Information:

Terminal window
# apt
apt show nginx
# yum
yum info nginx
# dnf
dnf info nginx

4. 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.
  • -y or --yes: Automatically answer “yes” to prompts. Use with caution, especially with remove and purge.
  • --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.
  • -y or --assumeyes: Automatically answer “yes” to prompts. Use with caution, especially with remove.
  • --enablerepo or --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/nginx with the following content:

    Package: nginx
    Pin: version 1.18.0-6ubuntu14.2
    Pin-Priority: 1001
  • Adding a PPA (Personal Package Archive):

    Terminal window
    sudo add-apt-repository ppa:certbot/certbot # Example: Certbot PPA
    sudo apt update
    sudo 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 # yum
    sudo dnf install /path/to/package.rpm # dnf
  • Listing available updates:

    Terminal window
    yum check-update # yum
    dnf check-update # dnf
  • Enabling/Disabling Repositories:

    Terminal window
    sudo yum --enablerepo=epel install <package_name> # yum
    sudo dnf --enablerepo=epel install <package_name> # dnf
    sudo yum --disablerepo=epel update # yum
    sudo dnf --disablerepo=epel update # dnf
  • Downgrading a Package:

    Terminal window
    sudo dnf downgrade <package_name>
  • Listing all repositories:

    Terminal window
    yum repolist all #yum
    dnf repolist all #dnf

6. Tips & Tricks

  • Update and Upgrade in one command:

    yum/dnf
    # apt
    sudo apt update && sudo apt upgrade -y
    sudo yum update -y # yum
    sudo dnf upgrade -y # dnf
  • Use aliases: Create aliases in your .bashrc or .zshrc for frequently used commands. For example:

    Terminal window
    alias up='sudo apt update && sudo apt upgrade -y' # apt
    alias 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-run to 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): dnf automatically 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 lock
    sudo 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 crashed
    sudo 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, run sudo apt update. For yum/dnf, check the repository list using yum repolist or dnf repolist.

  • Dependency errors: The package requires other packages that are not installed or are conflicting. Try:

    Terminal window
    # apt
    sudo apt --fix-broken install
    sudo apt install -f # Force installation, resolving dependencies
    # yum/dnf
    sudo yum install yum-utils # yum - Install yum-utils if not already installed
    sudo yum deplist <package_name> # yum - List package dependencies
    sudo 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 (for apt) or /etc/yum.repos.d/ (for yum/dnf). For dnf, the fastestmirror plugin helps automatically select the fastest mirror.

  • Corrupted Package Database: If you suspect corruption, try:

    Terminal window
    # apt
    sudo rm -rf /var/lib/apt/lists/*
    sudo apt update
    # yum
    sudo yum clean all
    sudo yum makecache fast
    # dnf
    sudo dnf clean all
    sudo 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 by apt. Generally, you should use apt instead of dpkg directly.
  • rpm (RPM Package Manager): The low-level package manager used by yum and dnf. Generally, you should use yum or dnf instead of rpm directly.
  • add-apt-repository: Adds a Personal Package Archive (PPA) to your system.
  • apt-cache: Queries the apt cache. 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.