Install and Manage Packages
Master package management on Linux: apt, dnf, and more. Hands-on install, update, and removal steps with troubleshooting for developers.
Focus: install and manage packages
You've written the code, configured the service, and pushed it to a server — but when you try to run it, the command isn't found, or worse, it's the wrong version. Every developer hits this wall: installing and managing packages is the unglamorous skill that determines whether your application runs reliably or collapses into dependency hell. This lesson turns that pain into a superpower, teaching you the mental model, hands-on commands, and troubleshooting instincts for Linux package management — the backbone of any backend platform.
The problem this lesson solves
Picture this: you're deploying a Python Flask app to a fresh Ubuntu server. You type python3 app.py and get ModuleNotFoundError: No module named 'flask'. You run pip install flask — but your system Python is managed by the OS, and a careless pip can break system packages. Or you're on CentOS and apt doesn't exist — you need dnf. Even experienced developers waste hours fighting versions, repositories, and broken dependencies.
The core problem: Linux distributions don't ship every library pre-installed, and installing software manually (compiling from source, downloading tarballs) is brittle and unmanageable. Without a disciplined approach, you'll face:
- Dependency hell — Package A needs version 1.2 of library X, but Package B needs 1.0.
- Security vulnerabilities — Outdated packages expose your server.
- Reproducibility disasters — What works on your laptop fails on the server.
- Wasted time — Guessing commands instead of following a system.
This lesson gives you a repeatable workflow to install and manage packages on Linux, with a focus on the two major families: Debian/Ubuntu (apt) and Red Hat/Fedora (dnf). You'll learn to install, update, remove, and audit packages — plus the hidden traps that bite even experienced engineers.
Core concept / mental model
Think of a package manager as a librarian for your computer. Instead of you browsing every shelf to find a book (a library), the librarian knows exactly where each book is, which books depend on others, and which editions are compatible. You hand the librarian a request: "I need Flask." The librarian checks the catalog (a repository), resolves dependencies (other books needed), and puts everything on your desk — while keeping a record of what you borrowed.
In Linux terms:
- Package — A compressed archive containing executable code, configuration files, and metadata (version, dependencies, checksums).
- Repository — A remote or local server hosting packages, configured in files under
/etc/apt/sources.listor/etc/yum.repos.d/. - Dependency resolution — The package manager figures out which other packages are required and installs them automatically.
- Database — A local state (
/var/lib/dpkgfor apt,/var/lib/rpmfor rpm) tracking what's installed, versions, and checksums.
Two major families dominate:
| Family | Distributions | Package format | Commands |
|---|---|---|---|
| Debian | Ubuntu, Debian, Mint | .deb |
apt, dpkg |
| Red Hat | Fedora, CentOS, RHEL | .rpm |
dnf, yum, rpm |
Pro tip: Many cloud images use Ubuntu (apt) or Amazon Linux (dnf). Learn both — you'll likely encounter both in your career.
How it works step by step
Installing a package isn't magic — it's a controlled series of steps. Here's the mental checklist:
- Update the package index — Your local system needs to know the latest versions and new packages available in repositories. (This is not upgrading packages — it's refreshing the menu.)
- Search for the package — Verify the exact package name before installing.
- Install — The package manager downloads the archive, verifies its integrity (checksum), resolves dependencies, and installs all files in the right places.
- Verify the installation — Check that the binary or library is available and the expected version is present.
- Manage the lifecycle — Update regularly, remove when no longer needed, and audit what's installed.
For apt, the critical commands are:
# 1. Update the package index (always first!)
sudo apt update
# 2. Search for a package
apt search python3-flask
# 3. Install
sudo apt install python3-flask
# 4. Verify
python3 -c "import flask; print(flask.__version__)"
For dnf, the equivalents:
sudo dnf check-update # similar to apt update
sudo dnf search flask
sudo dnf install python3-flask
Both systems store a local database, so you can query what's installed:
# Debian/Ubuntu
dpkg -l | grep flask
# Red Hat/Fedora
rpm -qa | grep flask
Key nuance: The update step is non-destructive — it only refreshes metadata. A common beginner mistake is skipping it, leading to "package not found" errors for new releases.
Hands-on walkthrough
Let's walk through a complete scenario: you're setting up a Python environment for a telemetry app on Ubuntu. We'll install Flask, update it, then remove it cleanly.
Install a package
Open a terminal on your Ubuntu server (or VM) and run:
# Refresh the package index (essential first step)
sudo apt update
# Search for the exact package name
apt search python3-flask
# Install Flask
sudo apt install -y python3-flask
The -y flag assumes yes to prompts — useful for scripting, but be cautious in interactive sessions.
Expected output (simplified):
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
python3-flask python3-werkzeug python3-jinja2 ...
0 upgraded, 5 newly installed, 0 to remove ...
Notice the dependency resolution — apt installs Flask's dependencies (Werkzeug, Jinja2) automatically.
Now verify:
python3 -c "import flask; print(flask.__version__)"
Output:
2.0.1
Update a package
To see available updates and apply them:
# See what can be upgraded
apt list --upgradable
# Upgrade all packages (careful in production!)
sudo apt upgrade
Only upgrade in a test environment first. In production, plan maintenance windows.
Remove a package permanently
sudo apt remove python3-flask
To also remove now-unused dependencies:
sudo apt autoremove
Take a tour of management commands
# List all installed packages
dpkg -l | less
# Check if a specific package is installed
dpkg -s python3-flask
# Show package details (dependencies, description)
apt show python3-flask
Real-world script: audit your system
Here's a quick Bash script to list packages with known security issues (on Ubuntu):
#!/bin/bash
# audit.sh — list packages that can be security-upgraded
sudo apt update
apt list --upgradable | grep -i security
Save it as audit.sh, make it executable, and run it:
chmod +x audit.sh
./audit.sh
Expected output: A list of packages with security fixes available — for example, libssl3/stable-security — prompting you to plan an upgrade.
Compare options / when to choose what
You'll face choices: apt vs. dnf, and package managers vs. language-specific tools (pip, npm). Here's a decision framework:
| Scenario | Choose | Why |
|---|---|---|
| Debian/Ubuntu system | apt |
Native, handles OS-level software |
| Fedora/RHEL system | dnf |
Native, newer than yum, better dependency resolution |
| Python project dependencies | pip (in a venv) |
Isolates project libraries from OS packages |
| Node.js project | npm |
Equivalent for JavaScript |
| Single binary distribution | Download tarball or use snap/flatpak |
When you need a specific version not in repos |
Key principle: For system-level tools (nginx, Postfix, Docker), use the OS package manager. For language-specific libraries, use the language's package manager inside a virtual environment to avoid conflicts.
Pro tip: Never use
sudo pip installon system Python. It overrides OS-managed packages and can break your system. Use a venv or a user-space install (pip install --user).
Troubleshooting & edge cases
Here are the most common failures and how to fix them — you'll encounter these in real life.
Error: E: Unable to locate package python3-flask
Cause: You skipped sudo apt update, or the package name is wrong.
Fix: Run sudo apt update; then search with apt search flask to confirm the exact name. If it still fails, check your repository config (/etc/apt/sources.list).
Error: dpkg: error processing package ... (--configure)
Cause: A previous installation was interrupted or left broken.
Fix: Run sudo dpkg --configure -a to repair, then sudo apt -f install to fix dependencies.
Error: Package has unmet dependencies
Cause: A repository provides a version that conflicts with system libraries.
Fix: Try sudo apt --fix-broken install; if that fails, inspect the dependency details with apt show <pkg> and adjust repository priorities (e.g., use apt pinning).
Warning: apt upgrade wants to remove several packages
Cause: A proposed upgrade conflicts with installed packages.
Fix: Do not force -y blindly. Run sudo apt upgrade --dry-run to preview; then decide whether to accept or hold packages with sudo apt-mark hold <pkg>. In production, test in a staging environment first.
Performance issue: apt update is slow
Cause: Many repositories, or a slow mirror.
Fix: Edit /etc/apt/sources.list to use a local or faster mirror; or enable parallel downloads (Ubuntu 22.04+ supports Acquire::http::Pipeline-Depth in /etc/apt/apt.conf.d/).
What you learned & what's next
You can now install and manage packages with confidence: you understand the mental model of repositories and dependencies, you've run a hands-on install/update/remove cycle, you can compare apt vs. dnf, and you know how to troubleshoot common failures. This skill is the foundation for every other backend platform task — you'll need it for installing runtimes, databases, and monitoring agents.
In the next lesson, you'll build on this by exploring systemd services — you'll learn how to turn an installed application into a managed, auto-starting service. With package management under your belt, you're ready to orchestrate the full lifecycle of a backend service: install it, run it, monitor it, and update it safely.
Practice recap
Create a new Ubuntu VM (or use a container), then practice a complete lifecycle: update the index, search for and install curl, check its version, update all packages with a dry-run, remove curl and run autoremove. Repeat the same with dnf on a Fedora container to solidify the differences. Finally, write a small shell script that checks for security upgrades and prints a warning if any exist.
Common mistakes
- Skipping
sudo apt updatebefore install — you'll get 'Unable to locate package' errors for new or renamed packages. - Using
sudo pip installon system Python — it overrides OS-managed packages and can break your system. - Running
apt upgradewithout checking the change list, possibly removing or breaking packages in production. - Assuming apt works on all Linux distros — you need dnf/yum on Red Hat family, or snap/flatpak for some apps.
Variations
- Use
snapfor confined, automatically updating apps (e.g., Docker, certbot) on Ubuntu. - Use
yuminstead ofdnfon older CentOS/RHEL 7 systems — it's slower but equivalent in purpose. - Use language-specific managers (pip, npm) inside a virtual environment for project isolation.
Real-world use cases
- Automating the setup of a new Ubuntu web server: install nginx, PostgreSQL, and Python dependencies via apt in a bootstrap script.
- Keeping a Fedora-based CI runner up-to-date by running
sudo dnf upgradein a cron job, then checking exit codes. - Rolling back a bad deployment by pinning a package version with
apt-mark holduntil the issue is resolved.
Key takeaways
- Always update the package index before installing anything.
- Use the OS package manager (apt/dnf) for system-level software, and language managers inside virtual environments for project dependencies.
- Learn the distinction between
update(refresh index) andupgrade(apply changes) — they're different. - Resolve dependency issues with
--fix-brokenanddpkg --configure -abefore considering drastic actions. - Audit your packages regularly with
apt list --upgradableand security checks.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.