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
install
: Install a packageupdate
: Update package listsupgrade
: Upgrade installed packagesremove
: Remove a packagesearch
: Search for a packageshow
: Display package information
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
install
: Install a packageupdate
: Update a packageupgrade
: Upgrade all installed packagesremove
: Remove a packagesearch
: Search for a packageinfo
: Display package information
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
install
: Install a packageupdate
: Update a packageupgrade
: Upgrade all installed packagesremove
: Remove a packagesearch
: Search for a packageinfo
: Display package information
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
-
Software Installation
# Install a package $ sudo apt install git $ sudo yum install git $ sudo dnf install git
-
Package Updates and Upgrades
# Update package lists $ sudo apt update # Upgrade installed packages $ sudo apt upgrade $ sudo yum upgrade $ sudo dnf upgrade
-
Package Removal
# Remove a package $ sudo apt remove nginx $ sudo yum remove nginx $ sudo dnf remove nginx
Tips and Tricks
-
Search for Packages
# Search for a package $ apt search nginx $ yum search nginx $ dnf search nginx
-
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
-
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
-
Keep the System Updated
# Update and upgrade regularly $ sudo apt update && sudo apt upgrade $ sudo yum update $ sudo dnf upgrade
-
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
-
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
-
Package Not Found
# Check package name spelling $ apt search nginx $ yum search nginx $ dnf search nginx
-
Dependency Issues
# Resolve dependencies $ sudo apt install -f $ sudo yum install -y $ sudo dnf install --allowerasing
-
Permission Denied
# Use sudo for package management $ sudo apt install nginx $ sudo yum install nginx $ sudo dnf install nginx
Related Commands
rpm
: Low-level package management (Red Hat-based)dpkg
: Low-level package management (Debian-based)snap
: Package management for Snap packagesflatpak
: Package management for Flatpak packages
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.