DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Linux & BashLesson 10 of 16
Your progress · 0%
On this page
  1. How a package manager works
  2. Everyday commands
  3. What's installed, and who owns this file?
  4. Repositories and trust
  5. Updating a production server
  6. snap and other installers
↖ Course roadmap
Lesson 10 · Package management

Packages: install, update and trust software

How apt and dnf install software with its dependencies, where packages come from, how to update a server safely, find what owns a file, pin versions, and when snap fits.

30 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 6 complete

How a package manager works

Before package managers. Installing software on Linux used to mean: find the project's website, download a .tar.gz of source code, run ./configure && make && make install, and watch it fail because it needs a library you don't have. So you download that library, which needs two more. Hours later it works, but nobody wrote down what was installed where. Updating means doing it all again, and removing it cleanly is close to impossible, because the files are scattered across the system and nothing remembers which ones belong to which program. This was called "dependency hell".

What a package manager does instead. It turns all of that into one command, and it keeps a record. A package is a program bundled with its files, its configuration and a list of what it needs to run. A repository is a server full of packages, published by your distribution or a vendor. The package manager downloads the right packages, installs what they depend on, and remembers every file it put on disk, so it can update or remove them cleanly later.

update refreshes the catalogue; install picks, downloads, verifies and unpacks.© Diagram: DevOps Academy

update refreshes the catalogue; install picks, downloads, verifies and unpacks.© Diagram: DevOps Academy

Two families cover almost every server:

Debian, UbuntuRHEL, Rocky, Alma, Fedora, Amazon Linux
Package file.deb.rpm
High level (repositories, dependencies)apt (apt-get, apt-cache in scripts)dnf (yum on older systems)
Low level (one package file)dpkgrpm
Repositories/etc/apt/sources.list.d//etc/yum.repos.d/

Use the high-level tool almost always. dpkg -i file.deb or rpm -i file.rpm install one file and don't fetch its dependencies, which is how servers end up half-broken.

Everyday commands

The high-level package tool handles repositories and dependencies. On Debian and Ubuntu, use apt at an interactive terminal and apt-get or apt-cache in scripts where stable output matters. On RHEL-family systems, use dnf. Refreshing the package index does not install upgrades; it only updates the catalogue used to choose candidate versions.

Taskapt (Debian, Ubuntu)dnf (RHEL family)
Refresh the listssudo apt update(automatic; sudo dnf makecache)
Searchapt search nginxdnf search nginx
Detailsapt show nginxdnf info nginx
Installsudo apt install nginxsudo dnf install nginx
Removesudo apt remove nginxsudo dnf remove nginx
Remove with its configsudo apt purge nginx(config files are kept as .rpmsave)
Upgrade everythingsudo apt upgradesudo dnf upgrade
Remove unused dependenciessudo apt autoremovesudo dnf autoremove
$ apt-cache policy curl                 # installed version, and the version apt would install
curl:
  Installed: 8.18.0-1ubuntu2.7
  Candidate: 8.18.0-1ubuntu2.7
$ apt-get -s install tree               # -s: simulate. Shows what would happen, changes nothing
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
$ sudo apt install -y tree              # -y: don't ask (for scripts)
sudo apt install -y tree
  • sudorun the next command as root
  • aptDebian/Ubuntu package manager

apt is for people: progress bars and colour. In scripts, use apt-get and apt-cache: their output stays stable between versions. Set DEBIAN_FRONTEND=noninteractive so no package stops a script to ask a question.

Simulate a change before installing on a sensitive host, and read which packages will be added or removed. Use purge when you also intend to remove Debian package configuration; ordinary remove may retain it. A common mistake is using dpkg -i for a package with dependencies: the low-level tool does not resolve and download missing packages for you.

Linux Crash Course - The apt CommandLearn Linux TV · Watch on YouTube ↗

What's installed, and who owns this file?

When two copies of a tool exist, the name alone does not tell you which package installed the executable or which version is active. Package databases answer that question and help you distinguish managed software from a manually copied binary.

$ dpkg -l | grep -c '^ii'               # how many packages are installed
549
$ dpkg -l curl                          # one package: ii = installed
ii  curl           8.18.0-1ubuntu2.7 amd64        command line tool for transferring data
$ dpkg -S /usr/bin/curl                 # which package put this file here?
curl: /usr/bin/curl
$ dpkg -L curl                          # every file a package installed
$ apt list --upgradable                 # what has a newer version waiting
$ rpm -qa | sort                        # RHEL family: everything installed
$ rpm -qf /usr/sbin/nginx               # RHEL family: which package owns a file
dpkg -l | grep -c '^ii'
  • |pipe: send output to the next command
  • grepfind lines that match a pattern
  • -ccount matching lines
apt list --upgradable
  • aptDebian/Ubuntu package manager
rpm -qa | sort
  • sortsort lines

dpkg -S and rpm -qf answer the classic question "where did this binary come from?", which matters when two versions of a tool are installed and the wrong one runs.

Use dpkg -L or rpm -ql to inspect a package's files, and check the executable selected by PATH when a command behaves unexpectedly. A file under /usr/local/bin may not belong to a package at all. A common mistake is deleting a package-owned file directly; the package manager may restore it during an update or continue to report the package as installed.

Repositories and trust

Your distribution's repositories have most software you need. A vendor repository may provide a newer version, but it also becomes a trusted source of code that can run as root during package installation. Adding one means installing its signing key, configuring the repository and telling apt which key may sign its packages.

$ sudo install -d -m 0755 /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
$ echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
    | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update
sudo install -d -m 0755 /etc/apt/keyrings
  • sudorun the next command as root
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
  • curlmake an HTTP request
  • |pipe: send output to the next command
  • teewrite input to a file and pass it on
  • >write output to a file, replacing it
echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
  • echoprint text
sudo apt update
  • aptDebian/Ubuntu package manager

signed-by= ties that repository to that key only, so a key you add for Docker can't be used to sign packages pretending to be Ubuntu's. Every package is checked against the key before it's installed.

Scope each key to its repository with signed-by= instead of trusting it globally. Follow the vendor's official instructions and review how updates are published. A common mistake is adding a key with a global trust command; that can allow it to authenticate packages from other configured repositories too.

Adding a key is adding trust

A repository can install anything on your server, as root, at every upgrade. Add only vendors you trust, over https, following their official instructions. Never pipe a random script from the internet straight into sudo bash on a production machine.

Updating a production server

Updates fix security holes, so servers must get them. They also change software under running apps. An upgrade can restart a service in the middle of the day, change a default setting, or bring a new major version of a database. So treat updates as a planned change, not a background chore. The routine below does that:

  1. Look first. See exactly which packages will change.
  2. Hold the risky ones. apt-mark hold freezes a package (a database, say) at its current version, so it's upgraded later, in its own maintenance window, with a backup taken first.
  3. Upgrade the rest.
  4. Check whether a reboot is needed. A new kernel only takes effect after a reboot, and Ubuntu leaves a marker file when one is due.

The commands, in that order:

$ sudo apt update && apt list --upgradable     # 1. see what would change
$ sudo apt-mark hold postgresql-16             # 2. hold anything that needs a planned window
$ sudo apt upgrade                             # 3. upgrade the rest
$ ls /var/run/reboot-required 2>/dev/null && cat /var/run/reboot-required.pkgs   # 4. kernel update?
$ sudo apt-mark unhold postgresql-16           # when its window comes
sudo apt update && apt list --upgradable
  • sudorun the next command as root
  • aptDebian/Ubuntu package manager
  • &&run the next command only if this one worked
ls /var/run/reboot-required 2>/dev/null && cat /var/run/reboot-required.pkgs
  • lslist files
  • catprint a file
CommandUpgradesRemoves or adds packages
apt upgradeeverything that can be upgraded safelynever removes
apt full-upgradealso changes that need adding or removing packagesmay remove
dnf upgradeeverythingmay replace packages

Test upgrades on a staging server first, upgrade one production server at a time behind a load balancer, and keep unattended-upgrades for security fixes only, so urgent patches still apply by themselves.

Apply updates in small batches behind a load balancer, then check service health before continuing. A held package is a temporary scheduling decision, not a reason to skip its security updates forever; track the planned upgrade window. A common mistake is running full-upgrade without reviewing removals, which can remove packages to complete dependency changes.

snap and other installers

snap packages (Ubuntu) bundle a program with all its dependencies and update themselves. They're convenient for desktop and developer tools (sudo snap install --classic code), and some Ubuntu server tools ship this way. On servers, prefer the distribution's packages or the vendor's repository: they integrate with systemd and your normal update process.

Some tools ship as a single binary (kubectl, terraform) or a vendor install script. Download them from the official site, check the published checksum (sha256sum -c, from the Text processing lesson), and put them in /usr/local/bin, which is for software the package manager doesn't manage.

In production

Write down what you installed and why, or better, install it with code: Ansible, a Dockerfile, or a cloud-init script. A server built by hand from memory can't be rebuilt exactly, and one day it will need to be.

⌘
Mini mission

Audit a server's packages before a change

Before upgrading a server, you want to know where its software comes from. Record which repositories it uses, how many packages are installed, what would be upgraded, and which package provides the curl binary. Save it to one report.

Input: An Ubuntu or Debian serverOutput: ~/packages.txt
Reveal one safe solution
$ cat > ~/packages-report.sh <<'EOF'
#!/bin/bash
echo "== repositories"; grep -rh "^URIs\|^deb " /etc/apt/sources.list /etc/apt/sources.list.d/ 2>/dev/null | sort -u
echo "== installed: $(dpkg -l | grep -c '^ii') packages"
echo "== upgradable"; apt list --upgradable 2>/dev/null | tail -n +2
echo "== held"; apt-mark showhold
echo "== curl comes from"; dpkg -S "$(command -v curl)"
EOF
$ bash ~/packages-report.sh > ~/packages.txt
$ cat ~/packages.txt
== repositories
URIs: http://archive.ubuntu.com/ubuntu/
URIs: http://security.ubuntu.com/ubuntu/
== installed: 549 packages
== upgradable
== held
== curl comes from
curl: /usr/bin/curl
cat > ~/packages-report.sh <<'EOF'
  • catprint a file
  • >write output to a file, replacing it

Newer Ubuntu releases describe repositories in .sources files (URIs: lines); older ones use one-line deb … entries. The script reads both. An empty "upgradable" list right after apt update means the server is current.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

apt install says "Unable to locate package" for a package you know exists. What do you run first?

Go deeper

Official reading for this lesson

  • Docsapt(8) manualUbuntu manpages ↗
  • GuideUbuntu Server: package managementUbuntu ↗
  • DocsDNF command referenceDNF ↗
  • GuideUnattended upgrades on UbuntuUbuntu ↗
  • GuideLPIC-1 objectives 102.4 and 102.5Linux Professional Institute ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousServices with systemd: run, restart, read the logsNext lesson →Disks and filesystems: add, mount, grow and repair