Skip to content
$ lynuxpro
← Linux

LESSON 7 OF 26 · BEGINNER · 14 MIN

Package management

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.

FamilyDistributionsLow-level toolEveryday toolPackage file
DebianUbuntu, Debiandpkgapt.deb
Red HatRHEL, Rocky, Alma, Amazon Linux, Fedorarpmdnf (older: yum).rpm
AlpineAlpine (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
SHELLbashutf-8 · 2L
CommandWhat it does
apt updateRefresh the list of available packages. Installs nothing
apt upgradeInstall newer versions of everything you already have
apt install -y pkgInstall a package (-y answers "yes" for you, needed in scripts)
apt remove pkgRemove a package but keep its config
apt purge pkgRemove a package and its config
apt autoremoveRemove leftover dependencies nobody needs
apt search wordFind packages
apt show pkgShow version, size and description
apt list --installedList 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
SHELLbashutf-8 · 1L
CommandWhat it does
dnf check-updateSee what can be updated
dnf upgradeUpdate everything
dnf install -y pkgInstall
dnf remove pkgRemove
dnf search word / dnf info pkgFind / describe
dnf list installedList installed packages
dnf historySee 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 RHEL
OUTPUTterminalutf-8 · 3L

Repositories

A repository is an online store of packages. Your server has a list of the ones it trusts:

  • Debian/Ubuntu: /etc/apt/sources.list and /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/*
NORMALdockerfileutf-8 · 4L
  • Put update and install in one RUN line, so the package list is never out of date.
  • --no-install-recommends skips optional extras.
  • Deleting /var/lib/apt/lists removes 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 family
OUTPUTterminalutf-8 · 2L

Real-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 update
SHELLbashutf-8 · 2L
apt list --installed | grep opensslsudo apt install --only-upgrade -y opensslapt list --installed | grep openssl
OUTPUTterminalutf-8 · 3L

Then 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 -y in scripts, so the script hangs forever waiting for "yes".
  • Using the wrong family's tool: apt on Amazon Linux, or yum on Ubuntu. Check /etc/os-release first.
  • Installing from random websites with curl | sudo bash. Prefer signed packages from trusted repositories.

Practice

  1. Refresh the package list and install nginx without being asked questions.
  2. Install git using the Red Hat tool.
  3. Check whether nginx is installed with apt list --installed.
  4. Rewrite this Dockerfile line the right way: RUN apt-get install curl.

Cheat sheet

TaskUbuntu / DebianRHEL / Amazon Linux
Refresh listapt updatednf check-update
Installapt install -y pkgdnf install -y pkg
Removeapt remove pkgdnf remove pkg
Upgrade allapt upgradednf upgrade
Searchapt search xdnf search x
List installedapt list --installeddnf list installed

Key takeaways

  • A package manager is an app store for the command line.
  • Ubuntu uses apt, Red Hat and Amazon Linux use dnf, and Alpine uses apk.
  • Run apt update before you install anything.
  • Add -y in scripts, and lock important versions in production.