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
- Linux learning environment with
sudo - Ubuntu/Debian or access to an RHEL-family VM for comparison
How Package Managers Work
| Concept | Meaning |
|---|---|
| Package | .deb (Debian/Ubuntu) or .rpm (RHEL) archive with files + metadata |
| Repository | Mirror server listing available packages |
| Dependency | Other 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):
# Download latest package lists
sudo apt updateUpgrade installed packages:
# Upgrade all packages (review on production servers)
sudo apt upgrade -yInstall packages:
# Install JDK 21, Git, curl, build tools
sudo apt install -y openjdk-21-jdk git curl build-essentialVerify Java (links to Java on Linux):
java -version
javac -versionSearch before install:
# Search package names/descriptions
apt search nginx
# Show details
apt show openjdk-21-jdkRemove:
# Remove package, keep config files
sudo apt remove git
# Remove package and config
sudo apt purge gitList installed:
dpkg -l | grep openjdkCleanup:
# Remove unused dependencies
sudo apt autoremove -y
# Clear downloaded package cache
sudo apt cleanTip
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).
# Refresh metadata
sudo dnf check-update
# Install JDK 21
sudo dnf install -y java-21-openjdk-devel
# Verify
java -versionSearch and remove:
dnf search nginx
sudo dnf remove git
dnf list installed | grep javaapt vs dnf Quick Reference
| Task | Ubuntu (apt) | RHEL (dnf) |
|---|---|---|
| Update index | sudo apt update | sudo dnf check-update |
| Upgrade | sudo apt upgrade | sudo dnf upgrade |
| Install | sudo apt install pkg | sudo dnf install pkg |
| Remove | sudo apt remove pkg | sudo dnf remove pkg |
| Search | apt search word | dnf search word |
| List installed | dpkg -l | dnf list installed |
Package names differ between distros—always search locally.
Common Developer Packages (Ubuntu)
# One-shot dev toolkit
sudo apt install -y \
openjdk-21-jdk \
git \
curl \
wget \
unzip \
build-essential \
ca-certificates| Package | Purpose |
|---|---|
openjdk-21-jdk | Java compile and run |
git | Version control |
curl / wget | HTTP downloads, health checks |
build-essential | gcc, make for native builds |
unzip | Zip 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:
- Download tarball (Node, custom middleware)
- Extract to
/opt/myappor~/tools - Add
bintoPATHin~/.bashrc
# Example layout
/opt/node/bin/nodePrefer apt/dnf when packages are maintained—security updates flow with the distro.
Mini Example: Install and Remove htop
sudo apt update
sudo apt install -y htop
htop
sudo apt remove -y htopInteractive 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).