Skip to content

Linux Package Management Guide for Beginners

โ† Back to Linux Commands


In Linux, we don't usually download .exe or .msi files from websites like we do in Windows. Instead, we use Package Managers. This guide will explain what they are and how to use them effectively.


๐Ÿ“ฆ What is a Package?

A Package is a compressed archive that contains all the files needed for a specific software to run. It includes: * The executable program. * Configuration files. * Information about what other software (dependencies) it needs to work.

๐Ÿ› ๏ธ What is a Package Manager?

A Package Manager is a tool that automates the process of installing, upgrading, configuring, and removing software. It handles: 1. Downloading: Finding and downloading the package from a central server called a Repository. 2. Dependencies: Automatically installing any other software required by the package. 3. Integrity: Checking if the package is safe and hasn't been tampered with.


๐Ÿ›๏ธ Common Package Managers by Distribution

Different Linux families use different package managers:

Linux Family Distribution Examples Package Manager Package Format
Debian Ubuntu, Debian, Mint apt .deb
Red Hat RHEL, CentOS, Fedora, Oracle Linux yum / dnf .rpm
Alpine Alpine Linux apk .apk

Tip

yum is being replaced by dnf in newer Red Hat-based systems, but the commands are almost identical.


๐Ÿš€ Basic Workflow: Installing Software

Before installing software, you should always update your local "index" of available packages. Think of this like refreshing a catalog.

1. Update Package Index

# Ubuntu/Debian
sudo apt update

# Red Hat/CentOS
sudo yum check-update

# Alpine
apk update

2. Search for a Package

If you're not sure of the exact name:

# Ubuntu/Debian (using --names-only to filter results)
apt search --names-only figlet

# Red Hat/CentOS
yum search figlet

3. Install a Package

# Ubuntu/Debian
sudo apt install figlet -y

# Red Hat/CentOS
sudo yum install figlet -y

# Alpine
apk add figlet

Note

The -y flag stands for "Yes," which automatically accepts the installation without asking for confirmation.


๐Ÿงน Removing Software

When you no longer need a program, you can remove it easily.

# Ubuntu/Debian
sudo apt remove figlet

# Red Hat/CentOS
sudo yum remove figlet

# Alpine
apk del figlet

Why do I see 'No such file or directory'?

After removing a program, if you try to run it again in the same shell session, you might see bash: /usr/bin/figlet: No such file or directory instead of command not found. This is because the shell "remembers" (hashes) the path to the program. Since the file is gone but the memory remains, it gives this specific error. You can clear this memory by running hash -r.


๐Ÿ” Examples in Action

Using yum (Oracle Linux / CentOS)

[opc@new-k8s ~]$ sudo yum install -y figlet
Complete!

[opc@new-k8s ~]$ figlet DevOps
 ____                 ___               
|  _ \  _____   _____|  _ \ ___  ___    
| | | |/ _ \ \ / / _ \ |_) / _ \/ __|   
| |_| |  __/\ V / (_) |  __/ (_) \__ \   
|____/ \___| \_/ \___/|_|   \___/|___/   

Using apt (Ubuntu)

root@ubuntu:/# apt update
root@ubuntu:/# apt install figlet
root@ubuntu:/# figlet --version
FIGlet Copyright (C) 1991-2012 by Glenn Chappell and Ian Chai

๐Ÿ› ๏ธ Advanced: Managing Repositories

Beyond basic installation, you often need to manage where software comes from. These locations are called Repositories.

๐Ÿง Debian/Ubuntu (APT) Repositories

APT stores its configuration in the /etc/apt/ directory. In modern Ubuntu versions, the primary repository list has moved from the legacy sources.list file to a newer, more structured format:

  1. /etc/apt/sources.list.d/ubuntu.sources: The primary file containing default repositories (uses the DEB822 format).
  2. /etc/apt/sources.list.d/: A directory for third-party repositories. Modern configurations use the .sources extension, while legacy ones use .list.

The Problem: Package Not Found If you try to install a tool like Docker or Ansible on a fresh system, you will likely see an error because they aren't in the default "catalog":

$ sudo apt install docker-ce ansible
Reading package lists... Done
E: Unable to locate package docker-ce

The Solution: Add the Repository There are two main ways to add a repository to your system:

Method A: Using add-apt-repository (Convenient) The easiest way is using the automated command, which handles both the key and the source:

# Add a PPA (Personal Package Archive) - e.g., Latest Ansible for automation
sudo add-apt-repository ppa:ansible/ansible

Method B: Manual Configuration (DEB822 Format) Modern Linux distributions recommend adding repositories manually. This involves two steps: adding a security key (GPG) and defining the source in the DEB822 format.

# 1. Download the security key and store it in a trusted keyring
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# 2. Create the repository configuration file
cat <<EOF | sudo tee /etc/apt/sources.list.d/docker.sources
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(lsb_release -cs)
Components: stable
Signed-By: /usr/share/keyrings/docker-archive-keyring.gpg
EOF

This newer format uses multi-line blocks with specific fields: * Types: Usually deb for binary packages. * URIs: The web address where the software is downloaded from. * Suites: Your specific Linux version (e.g., noble) or update channels. * Components: Categories like main (official), universe (community), or multiverse. * Signed-By: The path to the GPG key used to verify the software.

After: Successful Installation Once the repo is added and the index is updated, you can install the tools:

$ sudo apt update
$ sudo apt install docker-ce ansible -y
...
Setting up docker-ce (26.1.0-1~ubuntu.24.04~noble) ...
Setting up ansible (10.0.0-1ppa~noble) ...
Complete!

Important

Modern Ubuntu uses GPG keys in /usr/share/keyrings/ to verify package integrity. Always ensure you add the repository's key before updating.


๐ŸŽฉ Red Hat/CentOS (YUM/DNF) Repositories

The Problem: Package Not Found Without the repo, dnf cannot find specialized software:

$ sudo dnf install docker-ce
Error: Unable to find a match: docker-ce

The Solution: Add the Repo File Red Hat-based systems manage repositories using .repo files located in: * /etc/yum.repos.d/

A typical repository file (/etc/yum.repos.d/docker-ce.repo) looks like this:

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

Adding a Repository (Command Line): Alternatively, you can use dnf config-manager to add it automatically:

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

After: Successful Installation Now dnf can see the packages from the new source:

$ sudo dnf makecache
$ sudo dnf install docker-ce -y
...
Installed:
  docker-ce-3:26.1.0-1.el9.x86_64
  containerd.io-1.6.31-3.1.el9.x86_64

Complete!


๐Ÿ“Š Quick Comparison: Repository Commands

Action Debian/Ubuntu (apt) Red Hat/CentOS (yum/dnf)
Add Repository add-apt-repository dnf config-manager --add-repo
Repo Location /etc/apt/sources.list.d/ /etc/yum.repos.d/
Clean Cache apt clean dnf clean all
Build Cache apt update dnf makecache

๐Ÿง  Quick Quiz โ€” Package Management

#

Which package manager is used by Ubuntu?


๐Ÿ“ Want More Practice?

Test your knowledge with a full set of questions:

๐Ÿ‘‰ Start Package Management Quiz (15 Questions)


๐Ÿ“ฌ DevopsPilot Weekly โ€” Learn DevOps, Cloud & Gen AI the simple way.
๐Ÿ‘‰ Subscribe here