In one minute
- A package manager is Linux's app store for the command line. It installs, updates and removes software.
- Servers need software: nginx, Java, Git, monitoring agents. On Linux you almost never download installers from websites. The package manager gets trusted, signed software and also installs everything it depends on.
- You ask for a package by name. The tool downloads it from an official repository (a trusted online store of packages), checks its signature, installs any other packages it needs (its dependencies), and remembers it so it can update it later.
- 👉 Setting up a new server, building a Docker image, and patching security problems.
Think of it like: the app store on your phone. You do not hunt for apps on random websites. You search the store, tap install, and it keeps them updated.
Two families, two tools
Different Linux families use different package tools. Learn both and you can work on any server or container.
👉 Check which family you are on first with cat /etc/os-release.
| Family | Distributions | Low-level tool | Everyday tool | Package file |
|---|---|---|---|---|
| Debian | Ubuntu, Debian | dpkg | apt | .deb |
| Red Hat | RHEL, Rocky, Alma, Amazon Linux, Fedora | rpm | dnf (older: yum) | .rpm |
| Alpine | Alpine (Docker images) | apk | .apk |
The everyday tool finds dependencies and talks to repositories. You use it 99% of the time.
Remember it: Ubuntu → apt. Red Hat / Amazon → dnf. Alpine → apk.
apt on Ubuntu and Debian
sudo apt updatesudo apt install -y nginx| Command | What it does |
|---|---|
apt update | Refresh the list of available packages. Installs nothing |
apt upgrade | Install newer versions of everything you already have |
apt install -y pkg | Install a package (-y answers "yes" for you, needed in scripts) |
apt remove pkg | Remove a package but keep its config |
apt purge pkg | Remove a package and its config |
apt autoremove | Remove leftover dependencies nobody needs |
apt search word | Find packages |
apt show pkg | Show version, size and description |
apt list --installed | List what is installed |
Always run apt update before apt install on a new machine or container. Otherwise you get "Unable to locate package", because the list is empty or old.
Remember it:
update= refresh the menu.upgrade= actually get the new food.
dnf on RHEL, Rocky and Amazon Linux
sudo dnf install -y git| Command | What it does |
|---|---|
dnf check-update | See what can be updated |
dnf upgrade | Update everything |
dnf install -y pkg | Install |
dnf remove pkg | Remove |
dnf search word / dnf info pkg | Find / describe |
dnf list installed | List installed packages |
dnf history | See past installs and undo them |
On older systems, yum takes the same options.
Versions and pinning
Pinning means locking a package to one exact version.
In production, "latest" is risky. An automatic upgrade can change how an app behaves overnight.
👉 For anything important in production, such as your web server, database or runtime.
apt install nginx=1.24.0-2ubuntu7 # install an exact versionapt-mark hold nginx # stop it upgradingdnf install nginx-1.24.0 # exact version on RHELRepositories
A repository is an online store of packages. Your server has a list of the ones it trusts:
- Debian/Ubuntu:
/etc/apt/sources.listand/etc/apt/sources.list.d/ - Red Hat family:
/etc/yum.repos.d/
When you add one: Some vendors, such as Docker, HashiCorp (Terraform) and Kubernetes, run their own repositories. You add the repository and its signing key once, then install with the normal tool.
In companies, repositories are often passed through Artifactory or Nexus, so servers never talk to the public internet directly. You will set this up in the Artifactory course.
Packages inside Dockerfiles
The same commands are used to install software inside Docker images.
Why the extra care: Every leftover file makes the image bigger and slower to download.
FROM ubuntu:24.04RUN apt-get update \ && apt-get install -y --no-install-recommends curl ca-certificates \ && rm -rf /var/lib/apt/lists/*- Put
updateandinstallin oneRUNline, so the package list is never out of date. --no-install-recommendsskips optional extras.- Deleting
/var/lib/apt/listsremoves the downloaded package list from the image.
Which package owns this file?
👉 You find an unknown program on a server and want to know where it came from.
dpkg -S /usr/sbin/nginx # Debian familyrpm -qf /usr/sbin/nginx # Red Hat familyReal-world scenario: fixing a security problem
A security scan reports a critical OpenSSL bug. You check the installed version, update only that package, then confirm:
apt list --installed | grep nginxsudo apt updateapt list --installed | grep opensslsudo apt install --only-upgrade -y opensslapt list --installed | grep opensslThen restart the services that use it, so they load the fixed version. On many servers at once, you would do this with Ansible, which you will learn later.
Common mistakes
- Skipping
apt update, then getting "Unable to locate package". - Forgetting
-yin scripts, so the script hangs forever waiting for "yes". - Using the wrong family's tool:
apton Amazon Linux, oryumon Ubuntu. Check/etc/os-releasefirst. - Installing from random websites with
curl | sudo bash. Prefer signed packages from trusted repositories.
Practice
- Refresh the package list and install
nginxwithout being asked questions. - Install
gitusing the Red Hat tool. - Check whether
nginxis installed withapt list --installed. - Rewrite this Dockerfile line the right way:
RUN apt-get install curl.
Cheat sheet
| Task | Ubuntu / Debian | RHEL / Amazon Linux |
|---|---|---|
| Refresh list | apt update | dnf check-update |
| Install | apt install -y pkg | dnf install -y pkg |
| Remove | apt remove pkg | dnf remove pkg |
| Upgrade all | apt upgrade | dnf upgrade |
| Search | apt search x | dnf search x |
| List installed | apt list --installed | dnf list installed |
Key takeaways
- A package manager is an app store for the command line.
- Ubuntu uses
apt, Red Hat and Amazon Linux usednf, and Alpine usesapk. - Run
apt updatebefore you install anything. - Add
-yin scripts, and lock important versions in production.