Package Management with apt and dnf

Introduction

Linux distros install software through package managers that download signed packages from repositories and track dependencies. On Ubuntu you use apt; on Rocky/Alma/Fedora you use dnf. This chapter installs common developer tools and maps commands between families—same role as Homebrew on macOS, but system-wide.

Prerequisites

How Package Managers Work

ConceptMeaning
Package.deb (Debian/Ubuntu) or .rpm (RHEL) archive with files + metadata
RepositoryMirror server listing available packages
DependencyOther packages required automatically

You run one install command; the manager resolves dependencies—unlike manual JAR hunting, but similar idea to Maven/Gradle resolving libraries.

Debian / Ubuntu: apt

Refresh index (after mirror or first boot):

bash
# Download latest package lists
sudo apt update

Upgrade installed packages:

bash
# Upgrade all packages (review on production servers)
sudo apt upgrade -y

Install packages:

bash
# Install JDK 21, Git, curl, build tools
sudo apt install -y openjdk-21-jdk git curl build-essential

Verify Java (links to Java on Linux):

bash
java -version
javac -version

Search before install:

bash
# Search package names/descriptions
apt search nginx
 
# Show details
apt show openjdk-21-jdk

Remove:

bash
# Remove package, keep config files
sudo apt remove git
 
# Remove package and config
sudo apt purge git

List installed:

bash
dpkg -l | grep openjdk

Cleanup:

bash
# Remove unused dependencies
sudo apt autoremove -y
 
# Clear downloaded package cache
sudo apt clean

Tip

Production Changes

On live servers, test upgrades in staging. apt upgrade can restart services depending on packages—read prompts on desktop; use maintenance windows on VPS.

RHEL Family: dnf

Rocky Linux, AlmaLinux, Fedora use dnf (yum is often a symlink).

bash
# Refresh metadata
sudo dnf check-update
 
# Install JDK 21
sudo dnf install -y java-21-openjdk-devel
 
# Verify
java -version

Search and remove:

bash
dnf search nginx
sudo dnf remove git
dnf list installed | grep java

apt vs dnf Quick Reference

TaskUbuntu (apt)RHEL (dnf)
Update indexsudo apt updatesudo dnf check-update
Upgradesudo apt upgradesudo dnf upgrade
Installsudo apt install pkgsudo dnf install pkg
Removesudo apt remove pkgsudo dnf remove pkg
Searchapt search worddnf search word
List installeddpkg -ldnf list installed

Package names differ between distros—always search locally.

Common Developer Packages (Ubuntu)

bash
# One-shot dev toolkit
sudo apt install -y \
  openjdk-21-jdk \
  git \
  curl \
  wget \
  unzip \
  build-essential \
  ca-certificates
PackagePurpose
openjdk-21-jdkJava compile and run
gitVersion control
curl / wgetHTTP downloads, health checks
build-essentialgcc, make for native builds
unzipZip archives

Node.js is often installed via NodeSource, nvm, or distro nodes—not always apt install nodejs (may be old). See Node Linux install.

Install from Source or Tarball (When Needed)

Some tools ship official binaries outside apt:

  1. Download tarball (Node, custom middleware)
  2. Extract to /opt/myapp or ~/tools
  3. Add bin to PATH in ~/.bashrc
bash
# Example layout
/opt/node/bin/node

Prefer apt/dnf when packages are maintained—security updates flow with the distro.

Mini Example: Install and Remove htop

bash
sudo apt update
sudo apt install -y htop
htop
sudo apt remove -y htop

Interactive htop shows processes—preview of the next chapter.

FAQ

apt install vs apt-get?

Both work; apt is the modern friendly front-end. Scripts often use apt-get for stability in automation.

Permission denied without sudo?

Installing system packages requires root—use sudo.

Wrong Java version after install?

Run update-alternatives --config java on Debian/Ubuntu if multiple JDKs exist.

Snap vs apt?

Ubuntu pushes some apps as snap (snap install). This course focuses on apt for servers; snaps behave differently (paths, updates).

What comes next?

Processes and systemd.