Skip to content

Linux apt, yum, and dnf Commands: Package Management Essentials

Published: at 03:46 PMSuggest Changes

Package management is a crucial aspect of system administration and DevOps. Linux distributions typically use one of the following package managers: apt (Debian-based), yum (Red Hat-based), or dnf (newer version of yum). This guide covers the essential commands and usage for these package managers.

apt (Advanced Package Tool)

Basic Syntax

apt [sub-command] [options]

Common Sub-commands

Usage Examples

# Update package lists
$ sudo apt update

# Install a package
$ sudo apt install nginx

# Upgrade all installed packages
$ sudo apt upgrade

# Remove a package
$ sudo apt remove nginx

yum (Yellowdog Updater, Modified)

Basic Syntax

yum [sub-command] [options] [package(s)]

Common Sub-commands

Usage Examples

# Install a package
$ sudo yum install nginx

# Update a package
$ sudo yum update nginx

# Upgrade all installed packages
$ sudo yum upgrade

# Remove a package
$ sudo yum remove nginx

dnf (Dandified YUM)

Basic Syntax

dnf [sub-command] [options] [package(s)]

Common Sub-commands

Usage Examples

# Install a package
$ sudo dnf install nginx

# Update a package
$ sudo dnf update nginx

# Upgrade all installed packages
$ sudo dnf upgrade

# Remove a package
$ sudo dnf remove nginx

Common Use Cases

  1. Software Installation

    # Install a package
    $ sudo apt install git
    $ sudo yum install git
    $ sudo dnf install git
    
  2. Package Updates and Upgrades

    # Update package lists
    $ sudo apt update
    
    # Upgrade installed packages
    $ sudo apt upgrade
    $ sudo yum upgrade
    $ sudo dnf upgrade
    
  3. Package Removal

    # Remove a package
    $ sudo apt remove nginx
    $ sudo yum remove nginx
    $ sudo dnf remove nginx
    

Tips and Tricks

  1. Search for Packages

    # Search for a package
    $ apt search nginx
    $ yum search nginx
    $ dnf search nginx
    
  2. Install from a Specific Repository

    # Install a package from a specific repository
    $ sudo apt install nginx=1.14.0-0ubuntu1.7
    $ sudo yum install nginx-1.14.0-0.el7.centos
    $ sudo dnf install nginx-1.14.0-0.el8.centos
    
  3. Automate Package Management

    # Install multiple packages at once
    $ sudo apt install git vim tmux
    $ sudo yum install git vim tmux
    $ sudo dnf install git vim tmux
    

Best Practices

  1. Keep the System Updated

    # Update and upgrade regularly
    $ sudo apt update && sudo apt upgrade
    $ sudo yum update
    $ sudo dnf upgrade
    
  2. Manage Dependencies

    # Install a package and its dependencies
    $ sudo apt install --no-install-recommends nginx
    $ sudo yum install nginx --setopt=protected_multilib=false
    $ sudo dnf install nginx --allowerasing
    
  3. Secure Package Sources

    # Use trusted package repositories
    $ sudo apt-add-repository ppa:nginx/stable
    $ sudo yum-config-manager --add-repo https://nginx.org/packages/centos/
    $ sudo dnf config-manager --add-repo https://nginx.org/packages/centos/
    

Common Errors and Solutions

  1. Package Not Found

    # Check package name spelling
    $ apt search nginx
    $ yum search nginx
    $ dnf search nginx
    
  2. Dependency Issues

    # Resolve dependencies
    $ sudo apt install -f
    $ sudo yum install -y
    $ sudo dnf install --allowerasing
    
  3. Permission Denied

    # Use sudo for package management
    $ sudo apt install nginx
    $ sudo yum install nginx
    $ sudo dnf install nginx
    

Advanced Usage

1. Scripting Package Management

#!/bin/bash
# Install a list of packages
packages=("git" "vim" "tmux")
for pkg in "${packages[@]}"; do
    sudo apt install -y "$pkg"
    sudo yum install -y "$pkg"
    sudo dnf install -y "$pkg"
done

2. Caching Packages

# Cache packages for offline installation
$ sudo apt-get install -d nginx
$ sudo yum install --downloadonly --downloaddir=/path/to/cache nginx
$ sudo dnf install --downloadonly --downloaddir=/path/to/cache nginx

3. Automating Updates

#!/bin/bash
# Automatically update packages daily
sudo apt update && sudo apt upgrade -y
sudo yum update -y
sudo dnf upgrade -y

Package management is a crucial aspect of system administration and DevOps. Understanding the usage of apt, yum, and dnf commands is essential for effectively installing, updating, and managing software packages in a Linux environment.


Previous Post
Linux uname, df, and du Commands: Gathering System Information
Next Post
Linux kill Command: Terminating Processes Effectively