Lesson 7 of 12 · 8 min · Processes and services

Installing and updating software with package managers

Covers: Ubuntu 24.04 LTS (apt 2.7, dpkg 1.22) and Rocky Linux 9 (dnf 4). Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • Why Linux software comes from repositories and packages instead of downloaded installers
  • The apt commands on Debian and Ubuntu and the dnf commands on RHEL-family systems
  • How to find which package owns a file and what a package installed
  • Holding versions, adding repositories safely, and what to avoid on production

In simple words

Installing software on Linux works like an app store that you use by typing. You name what you want, and the system downloads it from a trusted source, along with anything else it needs to run. This lesson covers installing, updating and removing software the safe way.

Words to know

  • Package: one piece of software, bundled so it can be installed and removed cleanly.
  • Repository: the trusted online store that packages are downloaded from.
  • Dependency: another package that a program needs before it can work.
  • Package manager: the tool that installs, updates and removes packages: apt on Ubuntu, dnf on Red Hat systems.

Packages, repositories and dependencies

A package is an archive holding a program's files plus metadata: name, version, and its dependencies (the other packages it needs). A repository is a server with thousands of signed packages and an index. A package manager reads the index, works out all dependencies, downloads, verifies signatures, installs, and records what it put where, so it can later upgrade or remove everything cleanly.

This beats downloading installers for three reasons: dependencies are solved for you, security fixes arrive through one upgrade, and every file belongs to a known package.

Family Package file Low-level tool High-level tool
Debian, Ubuntu .deb dpkg apt / apt-get
RHEL, Rocky, Alma, Fedora, Amazon Linux 2023 .rpm rpm dnf (older: yum)
Alpine (common in containers) .apk apk

The low-level tool installs a single file with no dependency solving. You almost always use the high-level one.

apt on Debian and Ubuntu

sudo apt-get update              # refresh the package index (lists what is available)
sudo apt-get install -y nginx    # install (-y = answer yes)
sudo apt-get upgrade -y          # upgrade installed packages
sudo apt-get remove nginx        # remove the program, keep config files
sudo apt-get purge nginx         # remove program and its config
sudo apt-get autoremove          # remove dependencies nothing needs now
apt-cache policy tree            # installed and candidate versions, and which repo
apt-cache search "web server"    # search names and descriptions

update and upgrade are different: update only downloads the list; upgrade installs newer versions. A fresh container has an empty index, so apt-get install fails with "Unable to locate package" until you run update.

Check what would happen first with a simulation:

apt-get install -s tree | grep -A1 "NEW packages"
The following NEW packages will be installed:
  tree

apt is the friendly interface for people, apt-get is the stable one for scripts. Use apt-get (and DEBIAN_FRONTEND=noninteractive) in scripts and Dockerfiles. In a Dockerfile always chain apt-get update && apt-get install -y ... in the same RUN, and clean the lists afterwards, as the Docker course shows.

Where repositories are configured: /etc/apt/sources.list.d/ (on Ubuntu 24.04 the file ubuntu.sources):

Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse

noble is the release (24.04), noble-updates the later fixes, and the components describe licensing and support (main is officially supported free software).

Asking questions about what is installed

dpkg -S /usr/sbin/nginx     # which package owns this file?
dpkg -L tree                # which files did the package install?
dpkg -l nginx               # version and state of a package ("ii" = installed)
apt list --installed | wc -l
nginx: /usr/sbin/nginx

Holding a version

Sometimes an upgrade must not touch one package (a database client tied to a server version, for example):

sudo apt-mark hold tree
apt-mark showhold
sudo apt-mark unhold tree

dnf on RHEL-family systems

The same ideas with different words:

sudo dnf install -y tree          # refreshes metadata automatically
sudo dnf upgrade -y
sudo dnf remove tree
dnf search web server
dnf info nginx
dnf repolist                      # configured repositories
rpm -qf /usr/bin/tree             # which package owns this file?
rpm -ql tree                      # files in the package
dnf versionlock add tree          # hold a version (needs the versionlock plugin)

Repositories live in /etc/yum.repos.d/*.repo. On Rocky Linux 9, rpm -qf /usr/bin/tree answers with a name like tree-1.8.0-10.el9.x86_64: name, version, release, distribution tag, architecture.

Adding third-party repositories safely

Official repositories carry what the distribution ships, often older versions. Vendors like Docker and HashiCorp provide their own repositories. Adding one means trusting that vendor with root on your machine, so:

  1. Take the instructions from the vendor's official documentation (the Docker and Terraform courses give those links).
  2. Verify the repository signing key is added in the keyring and only trusted for that repository.
  3. Avoid curl ... | sudo sh from unknown sources. Download, read, then run.

Other ways software arrives

  • Language package managers: pip, npm, cargo. Use virtual environments or project folders; do not mix them with sudo pip install on a system Python.
  • Containers: the image already contains its dependencies (the Docker course).
  • Snap, Flatpak: self-contained packages with their own update channels. Common on Ubuntu desktops; servers use them less.
  • Compiling from source: last resort. It bypasses the package database, so updates and removal are manual.

Common mistakes

  • Forgetting apt-get update before install in a fresh machine or container.
  • Unattended upgrade on production without a test and a rollback plan. Kernel and library upgrades can change behaviour; stage them.
  • Mixing repositories (adding a newer Ubuntu's sources) that replace system libraries and break things. Use pinning or a container instead.
  • Piping unknown scripts to a root shell.

Try it yourself

  1. In a fresh ubuntu:24.04 container run apt-get install -y tree and read the error. Fix it and install the package.
  2. Use dpkg -L tree and dpkg -S to find what the package installed and prove that /usr/bin/tree belongs to it.
  3. Simulate removing a package with apt-get remove -s. What else would be removed?
  4. In a rockylinux:9 container install tree with dnf and run rpm -qf /usr/bin/tree.

Check yourself

What is the difference between apt-get update and apt-get upgrade?

update refreshes the list of available packages from the repositories; upgrade installs newer versions of installed packages based on that list. You normally run them in that order.

What does purge do that remove does not?

remove deletes the program but keeps system-wide configuration files; purge also deletes the configuration. User data in home directories stays in both cases.

How do you find out which package a file belongs to?

dpkg -S /path on Debian and Ubuntu, rpm -qf /path on RHEL-family systems.

Why prefer packages from the distribution over downloading and compiling?

Dependencies are handled, updates and security fixes come through the package manager, and removal is clean because the manager knows every file.

Interview questions

How do you prevent a package from being upgraded?

On Debian/Ubuntu apt-mark hold <package>; on RHEL-family systems the dnf versionlock plugin (or exclude= in the repo or dnf configuration).

What is a repository and how does the system know a package is trustworthy?

A repository is a server holding packages and an index. The index and packages are signed; the package manager verifies the signatures against keys configured on the machine before installing.

What does a good Dockerfile do with apt-get?

Runs apt-get update and apt-get install -y --no-install-recommends in one RUN so the cache is not stale, then deletes /var/lib/apt/lists/* in the same layer to keep the image small.

Summary

  • Packages carry software plus dependency data; repositories provide signed packages; the manager installs, upgrades and removes cleanly.
  • Debian family: apt-get update, install, remove/purge, dpkg -S/-L. RHEL family: dnf install, remove, rpm -qf.
  • Hold versions deliberately, stage upgrades, and only add third-party repositories from the vendor's official documentation.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Linux for DevOps for DevOps Engineer and scores every answer, with what to fix.