Package Management

Learn how to install, update, and remove software on Linux using different package managers.

One of Linux's greatest strengths is how it handles software installation and updates through package management systems. Unlike Windows or macOS, where you typically download installers from websites, Linux distributions use centralized package repositories that contain thousands of pre-configured software packages.

In this part, we'll explore how package management works and how to use the most common package managers.

Package Management Concepts

Before diving into specific package managers, let's understand some key concepts:

What is a Package?

A package is a compressed archive containing:

  • The software's files (binaries, libraries, documentation)
  • Metadata (name, version, description)
  • Dependencies (other packages required)
  • Installation scripts (configure the software during installation)
  • Configuration files

Package Repositories

Repositories (or "repos") are servers that host collections of packages. Distributions maintain official repositories, but third-party repositories can be added for additional software.

Dependencies

Most software relies on other software components (libraries, frameworks, etc.). Package managers automatically resolve these dependencies, installing any required packages.

Common Operations

All package managers provide commands to:

  • Install packages
  • Remove packages
  • Update packages
  • Search for packages
  • List installed packages
  • Show package information

Major Package Management Systems

Linux distributions use different package management systems:

Package Format Package Manager Used by
.deb apt, apt-get, dpkg Debian, Ubuntu, Linux Mint
.rpm yum, dnf, rpm Fedora, RHEL, CentOS, Rocky Linux
.pkg.tar.zst pacman Arch Linux, Manjaro
.xbps xbps Void Linux
(various) portage Gentoo
.apk apk Alpine Linux

We'll focus on the most widely used package managers: apt, dnf/yum, and pacman.

APT Package Manager (Debian/Ubuntu)

The Advanced Package Tool (APT) is used by Debian-based distributions like Ubuntu and Linux Mint.

Configuration

APT's repository sources are defined in /etc/apt/sources.list and files in the /etc/apt/sources.list.d/ directory.

A typical entry looks like:

deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse

This specifies:

  • The repository type (deb for binary packages)
  • The repository URL
  • The distribution codename (jammy for Ubuntu 22.04)
  • The repository components (main, restricted, universe, multiverse)

Basic APT Commands

First, update the package index:

sudo apt update

This refreshes the list of available packages but doesn't install anything.

Installing Packages

Install a package:

sudo apt install package_name

Install multiple packages:

sudo apt install package1 package2 package3

Removing Packages

Remove a package:

sudo apt remove package_name

Remove a package and its configuration files:

sudo apt purge package_name

Remove unused dependencies:

sudo apt autoremove

Upgrading Packages

Upgrade all installed packages:

sudo apt upgrade

Upgrade and remove obsolete packages:

sudo apt full-upgrade

Update and upgrade in one command:

sudo apt update && sudo apt upgrade

Searching for Packages

Search for packages:

apt search keyword

Show package details:

apt show package_name

List installed packages:

apt list --installed

Check if a specific package is installed:

apt list --installed | grep package_name

Working with .deb Files

Sometimes you might need to install a .deb file directly:

sudo apt install ./package.deb

Or using dpkg (lower-level tool):

sudo dpkg -i package.deb
sudo apt install -f  # Fix dependencies if needed

Holding Packages (Preventing Updates)

Prevent a package from being upgraded:

sudo apt-mark hold package_name

Remove the hold:

sudo apt-mark unhold package_name

Adding Repositories

Add a repository using add-apt-repository:

sudo add-apt-repository ppa:team/repository
sudo apt update

Practical APT Example

Let's install and configure the Nginx web server:

# Update package lists
sudo apt update

# Install Nginx
sudo apt install nginx

# Check the status
sudo systemctl status nginx

# Install some common Nginx add-ons
sudo apt install nginx-extras

# View package information
apt show nginx

DNF/YUM Package Manager (Fedora/RHEL/CentOS)

Fedora uses DNF (Dandified YUM), while older RHEL and CentOS versions use YUM. DNF is the newer version with improved dependency resolution. The commands are very similar.

Configuration

Repository configurations are stored in /etc/yum.repos.d/ as .repo files.

A typical repo file contains:

[repository-name]
name=Repository Description
baseurl=http://repo.url/path
enabled=1
gpgcheck=1
gpgkey=http://repo.url/key.asc

Basic DNF/YUM Commands

Check for updates:

sudo dnf check-update  # DNF
sudo yum check-update  # YUM

Installing Packages

Install a package:

sudo dnf install package_name
sudo yum install package_name

Removing Packages

Remove a package:

sudo dnf remove package_name
sudo yum remove package_name

Upgrading Packages

Update all packages:

sudo dnf upgrade
sudo yum update

Searching for Packages

Search for packages:

dnf search keyword
yum search keyword

Show package information:

dnf info package_name
yum info package_name

List installed packages:

dnf list installed
yum list installed

Working with Groups

DNF/YUM supports package groups, which install multiple related packages:

# List available groups
dnf group list
yum groups list

# Install a group
sudo dnf group install "Development Tools"
sudo yum groupinstall "Development Tools"

Managing Repositories

Enable a repository:

sudo dnf config-manager --enable repository
sudo yum-config-manager --enable repository

Add a repository:

sudo dnf config-manager --add-repo=https://repo.url/repo.repo
sudo yum-config-manager --add-repo=https://repo.url/repo.repo

Working with RPM Files

Install an RPM file directly:

sudo dnf install ./package.rpm
sudo yum install ./package.rpm

Or using the lower-level rpm command:

sudo rpm -i package.rpm

Practical DNF Example

Let's install and examine the Apache web server:

# Check for updates
sudo dnf check-update

# Install Apache
sudo dnf install httpd

# Check information about the package
dnf info httpd

# List installed packages related to Apache
dnf list installed | grep httpd

# Start and enable the service
sudo systemctl start httpd
sudo systemctl enable httpd

Pacman Package Manager (Arch Linux)

Arch Linux and its derivatives use the pacman package manager, known for its simplicity and speed.

Configuration

Pacman's configuration is in /etc/pacman.conf, and repository information is in /etc/pacman.d/mirrorlist.

Basic Pacman Commands

Synchronize the package database:

sudo pacman -Sy

Installing Packages

Install a package:

sudo pacman -S package_name

Install multiple packages:

sudo pacman -S package1 package2 package3

Removing Packages

Remove a package:

sudo pacman -R package_name

Remove a package and its dependencies:

sudo pacman -Rs package_name

Remove a package, its dependencies, and configuration files:

sudo pacman -Rns package_name

Upgrading Packages

Update all packages:

sudo pacman -Syu

The -Syu flags combine:

  • -S: Synchronize packages
  • -y: Refresh package database
  • -u: Upgrade installed packages

Searching for Packages

Search for packages:

pacman -Ss keyword

Show package information:

pacman -Si package_name

List installed packages:

pacman -Q

List installed packages with details:

pacman -Qi package_name

Practical Pacman Example

Let's install a text editor and check its information:

# Update package database
sudo pacman -Sy

# Install neovim
sudo pacman -S neovim

# Check detailed information
pacman -Qi neovim

# List files installed by the package
pacman -Ql neovim | head

Universal Package Formats

Traditional package managers are distribution-specific. Universal package formats aim to work across multiple distributions.

Snap

Developed by Canonical, snaps are self-contained applications that include dependencies:

# Install snapd
sudo apt install snapd  # On Debian/Ubuntu
sudo dnf install snapd  # On Fedora

# List available snaps
snap find keyword

# Install a snap
sudo snap install package_name

# List installed snaps
snap list

# Update all snaps
sudo snap refresh

# Remove a snap
sudo snap remove package_name

Flatpak

Flatpak aims to provide the same application across different Linux distributions:

# Install Flatpak
sudo apt install flatpak  # On Debian/Ubuntu
sudo dnf install flatpak  # On Fedora

# Add the Flathub repository
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

# Install an application
flatpak install flathub org.application.Name

# Run an application
flatpak run org.application.Name

# List installed applications
flatpak list

# Update all applications
flatpak update

# Remove an application
flatpak uninstall org.application.Name

AppImage

AppImage provides portable applications that don't need installation:

  1. Download the .AppImage file
  2. Make it executable: chmod +x application.AppImage
  3. Run it: ./application.AppImage

Compiling Software from Source

Sometimes software isn't available through package managers, and you need to compile it from source:

Basic Compilation Process

# Install compilation tools
sudo apt install build-essential  # On Debian/Ubuntu
sudo dnf groupinstall "Development Tools"  # On Fedora/RHEL/CentOS

# Download and extract source
wget https://example.com/software-1.0.tar.gz
tar -xzf software-1.0.tar.gz
cd software-1.0

# Configure, build, and install
./configure
make
sudo make install

Example: Compiling a Simple C Program

Let's create a simple "Hello World" program and compile it:

# Create a file named hello.c
echo '#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}' > hello.c

# Compile with gcc
gcc -o hello hello.c

# Run the program
./hello

This basic example doesn't use the ./configure and make process, but demonstrates the principles of compilation.

Managing Libraries

Applications depend on libraries (shared code). Here are some commands to manage them:

# List shared library dependencies of a binary
ldd /bin/ls

# Configure dynamic linker run-time bindings
sudo ldconfig

# Find which package provides a specific file
# On Debian/Ubuntu:
apt-file search filename

# On Fedora/RHEL/CentOS:
dnf provides */filename

Package Management Best Practices

Keep Your System Updated

Regularly update your system to get security patches and bug fixes:

# On Debian/Ubuntu
sudo apt update && sudo apt upgrade

# On Fedora/RHEL/CentOS
sudo dnf upgrade

# On Arch Linux
sudo pacman -Syu

Clean Up Your System

Remove unused packages and clean package caches:

# On Debian/Ubuntu
sudo apt autoremove
sudo apt clean

# On Fedora/RHEL/CentOS
sudo dnf autoremove
sudo dnf clean all

# On Arch Linux
sudo pacman -Rns $(pacman -Qtdq)  # Remove orphaned packages
sudo pacman -Sc  # Clean package cache

Create a List of Installed Packages

Useful for replicating your setup on another system:

# On Debian/Ubuntu
dpkg --get-selections > installed_packages.txt

# On Fedora/RHEL/CentOS
dnf list installed > installed_packages.txt

# On Arch Linux
pacman -Q > installed_packages.txt

Use Version Pinning for Stability

Pin package versions when you need stability:

# On Debian/Ubuntu
echo "package_name hold" | sudo dpkg --set-selections

# On Fedora/RHEL/CentOS
sudo dnf install package_name-specific_version
sudo dnf versionlock add package_name

Troubleshooting Package Management Issues

Handling Broken Packages (Debian/Ubuntu)

Fix broken packages:

sudo apt --fix-broken install

Force a package reconfiguration:

sudo dpkg-reconfigure package_name

Fixing Repository Issues

If you encounter GPG key errors:

# On Debian/Ubuntu
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEY_ID

# On Fedora/RHEL/CentOS
sudo rpm --import https://repo.url/key.asc

Dealing with Package Conflicts

If packages conflict:

# See what would happen without actually installing
apt --simulate install package_name

# Force a specific version
apt install package=version

Setting Up a Local Repository

For environments with multiple Linux systems, setting up a local repository can save bandwidth and provide better control.

Using apt-mirror (Debian/Ubuntu)

# Install apt-mirror
sudo apt install apt-mirror

# Configure in /etc/apt/mirror.list
# Run the mirror process
sudo apt-mirror

Using createrepo (Fedora/RHEL/CentOS)

# Install createrepo
sudo dnf install createrepo

# Create the repository structure
sudo mkdir -p /var/www/html/local_repo
sudo cp *.rpm /var/www/html/local_repo/

# Create the repository metadata
sudo createrepo /var/www/html/local_repo

Working with Cloud Infrastructure

When working with cloud servers, you'll often use package management to set up and maintain your systems. Many cloud providers offer images with pre-installed packages, but you'll need to update and install additional software.

DigitalOcean Example

DigitalOcean makes it easy to create a Linux server with various distributions. Here's a quick way to get started:

  1. Sign up for a DigitalOcean account (use this link to receive $200 in credits for 60 days)
  2. Create a Droplet (virtual server) with your preferred Linux distribution
  3. Connect via SSH and update your system:
# On Ubuntu
sudo apt update && sudo apt upgrade

# On Fedora
sudo dnf upgrade
  1. Install your required packages:
# Install a LAMP stack on Ubuntu
sudo apt install apache2 mysql-server php libapache2-mod-php

# Or on Fedora
sudo dnf install httpd mariadb-server php

The benefit of using cloud infrastructure is that you can quickly create, modify, and destroy servers as needed, using package management to ensure they have the software you require.

Moving Forward

You now understand how to manage software on various Linux distributions. This knowledge is essential for maintaining Linux systems and installing the software you need.

In the next part, we'll explore user and group management, learning how to create and manage user accounts, set permissions, and understand user-related configuration files.

Package management is a fundamental Linux skill that sets it apart from other operating systems. By learning these tools, you've gained the ability to efficiently install, update, and maintain software across different Linux distributions.

Found an issue?