Skip to main content

📦 Lesson 5.1: apt — The Ubuntu Package Manager

One command to install them all, one command to find them, one command to update them all, and in the terminal bind them.

🎯 Learning Objectives

  • Understand what a package manager is and why it matters
  • Search for, install, update, and remove packages with apt
  • Keep your entire system up to date
  • Inspect package details and dependencies
  • Clean up unused packages and cached files

Estimated Time: 40 minutes

📑 In This Lesson

What Is a Package Manager?

On Windows, you download .exe installers from random websites. On macOS, you drag apps from .dmg files. On Linux, you use a package manager — a tool that downloads, installs, updates, and removes software from a central, trusted source called a repository.

graph LR A["You
(the user)"] -->|"sudo apt install htop"| B["apt
(package manager)"] B -->|"Downloads from"| C["Ubuntu Repositories
(trusted servers)"] C -->|"htop + dependencies"| B B -->|"Installs to"| D["Your System
(/usr/bin, /usr/lib, etc.)"] style A fill:#3b82f6,stroke:#2563eb,color:#fff style B fill:#6366f1,stroke:#4338ca,color:#fff style C fill:#22c55e,stroke:#166534,color:#fff style D fill:#f59e0b,stroke:#b45309,color:#fff

Why is this better?

  • Security — packages are signed and verified; no shady download sites
  • Dependencies — if software needs other libraries, apt installs those too
  • Updates — one command updates everything on your system
  • Clean removal — apt can remove software and all its associated files

🐧 Repositories

A repository (or "repo") is a server hosting thousands of pre-built packages. Ubuntu's default repos contain over 60,000 packages — from command-line tools to full desktop applications. Your system knows which repos to check because they're listed in /etc/apt/sources.list.

apt vs apt-get

You'll see both apt and apt-get in tutorials. Here's the deal:

apt apt-get
Intended forHumans at the terminalScripts and automation
Progress barYesNo
Color outputYesNo
Combined commandsapt list, apt searchNeeds apt-cache separately
StabilityMay change output formatStable (script-safe)

✅ Rule of Thumb

Use apt when typing commands yourself. Use apt-get in shell scripts (it won't change its output format between versions). This course uses apt throughout.

Updating the Package Index

Before installing anything, always refresh your local list of available packages. This doesn't install updates — it just downloads the latest catalog.

# Refresh the package index from all configured repositories
sudo apt update

Sample Output

Hit:1 http://us.archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
...
Reading package lists... Done
Building dependency tree... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.

⚠️ Why sudo?

Most apt commands that change your system require sudo (superuser privileges). Reading/searching is fine without it, but installing, removing, and updating need root access.

Searching for Packages

# Search by name and description
apt search image editor

# Search for an exact package name
apt search --names-only htop

# List all installed packages
apt list --installed

# List packages that can be upgraded
apt list --upgradable

💡 Tip: Narrow Your Search

Searches can return hundreds of results. Pipe through grep or less to filter:

# Find image editors — show only lines with "editor"
apt search image | grep -i editor

# Browse results page by page
apt search python | less

Installing Packages

# Install a single package
sudo apt install htop

# Install multiple packages at once
sudo apt install htop tree curl wget

# Install without being prompted (auto-yes)
sudo apt install -y neofetch

# Install a specific version
sudo apt install package_name=version_number

What Happens During Install

Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
  libncursesw6 libnl-3-200 libnl-genl-3-200
The following NEW packages will be installed:
  htop libncursesw6 libnl-3-200 libnl-genl-3-200
0 upgraded, 4 newly installed, 0 to remove and 15 not upgraded.
Need to get 287 kB of archives.
Do you want to continue? [Y/n]

apt shows you exactly what it will install (including dependencies) and asks for confirmation.

Reinstalling a Package

# Fix a broken installation by reinstalling
sudo apt install --reinstall package_name

Upgrading Your System

The two-step update dance you should run regularly:

# Step 1: Refresh the package index
sudo apt update

# Step 2: Upgrade all installed packages
sudo apt upgrade

# Or combine them:
sudo apt update && sudo apt upgrade -y

upgrade vs full-upgrade

Command What It Does When to Use
apt upgrade Upgrades packages; never removes anything Regular weekly updates
apt full-upgrade Upgrades packages; may remove obsolete ones to resolve conflicts Major version upgrades, kernel updates

✅ Good Habit

Run sudo apt update && sudo apt upgrade -y at least once a week. This keeps your system secure with the latest patches.

Removing Packages

# Remove a package (keeps config files)
sudo apt remove package_name

# Remove a package AND its config files
sudo apt purge package_name

# Remove unused dependencies left behind
sudo apt autoremove
graph TD A["sudo apt remove htop"] --> B["Removes: /usr/bin/htop
Keeps: config files in /etc"] C["sudo apt purge htop"] --> D["Removes: /usr/bin/htop
AND config files in /etc"] E["sudo apt autoremove"] --> F["Removes: orphaned
dependencies no longer needed"] style A fill:#3b82f6,stroke:#2563eb,color:#fff style C fill:#f59e0b,stroke:#b45309,color:#fff style E fill:#22c55e,stroke:#166534,color:#fff

💡 When to Use purge

Use remove if you might reinstall later (your settings are preserved). Use purge for a clean slate — useful when a config file is causing problems and you want to start fresh.

Inspecting Packages

# Show detailed information about a package
apt show htop

# Check if a package is installed
apt list --installed | grep htop

# See which package provides a specific file
dpkg -S /usr/bin/htop

# List all files installed by a package
dpkg -L htop

# Check package dependencies
apt depends htop

# Check what depends ON a package (reverse dependencies)
apt rdepends htop

Example: apt show htop

Package: htop
Version: 3.3.0-4
Priority: optional
Section: utils
Maintainer: Ubuntu Developers
Installed-Size: 372 kB
Depends: libc6, libncursesw6, libnl-3-200, libnl-genl-3-200
Homepage: https://htop.dev/
Description: interactive processes viewer
 Htop is an ncursed-based process viewer similar to top,
 but it allows one to scroll the list vertically and
 horizontally to see all processes and their command lines.

Cleaning Up

Over time, cached package files and orphaned dependencies accumulate. Here's how to reclaim space:

# Remove orphaned dependencies
sudo apt autoremove

# Clear the local package cache (downloaded .deb files)
sudo apt clean

# Remove only outdated cached packages (keeps current versions)
sudo apt autoclean

# See how much space the cache is using
du -sh /var/cache/apt/archives/

⚠️ apt clean vs apt autoclean

clean removes all cached .deb files — you'd need to re-download them if you reinstall. autoclean only removes outdated versions, which is generally safer.

Other Distros

apt is specific to Debian-based distros (Ubuntu, Linux Mint, Pop!_OS). Other distros have their own package managers:

🐧 Package Manager Comparison

Distro Family Package Manager Install Example
Ubuntu / Debian / Mintaptsudo apt install htop
Fedora / RHEL / Rockydnfsudo dnf install htop
Arch / Manjaropacmansudo pacman -S htop
openSUSEzyppersudo zypper install htop
Alpineapkapk add htop

The concepts are the same — update, search, install, remove — only the command names differ.

Exercises

🏋️ Exercise 1: Update and Explore

  1. Update your package index: sudo apt update
  2. Check how many packages can be upgraded: apt list --upgradable
  3. Count your installed packages: apt list --installed | wc -l

🏋️ Exercise 2: Install and Inspect

  1. Search for neofetch: apt search neofetch
  2. View its details: apt show neofetch
  3. Install it: sudo apt install -y neofetch
  4. Run it: neofetch (displays system info in a fancy way)
  5. Find where it was installed: dpkg -L neofetch | head

🏋️ Exercise 3: Remove and Clean

  1. Remove neofetch: sudo apt remove neofetch
  2. Verify it's gone: which neofetch (should return nothing)
  3. Clean up orphaned dependencies: sudo apt autoremove
  4. Check cache size: du -sh /var/cache/apt/archives/
  5. Clean outdated cache: sudo apt autoclean

🏋️ Exercise 4: The Full Update Routine

Practice the complete update workflow you should run weekly:

# Refresh package index
sudo apt update

# Upgrade all packages
sudo apt upgrade -y

# Remove orphaned dependencies
sudo apt autoremove -y

# Clean outdated cache
sudo apt autoclean

This is a great candidate for a shell script — you'll learn how to create one in Module 8!

Knowledge Check

❓ Question 1

What should you run before installing a new package?

❓ Question 2

What's the difference between apt remove and apt purge?

❓ Question 3

Which command upgrades installed packages but never removes any?

❓ Question 4

What does sudo apt autoremove do?

Summary

🎉 Key Takeaways

  • apt is Ubuntu's package manager — it handles installs, updates, and removals from trusted repositories
  • Always run sudo apt update before installing or upgrading
  • apt search finds packages; apt show gives details; apt install installs them
  • apt remove keeps config files; apt purge removes everything
  • apt autoremove and apt autoclean keep your system tidy
  • Run sudo apt update && sudo apt upgrade weekly to stay secure

🍎 Mac Equivalent: Homebrew

macOS doesn't have apt. The closest equivalent is Homebrew (brew). Here's how the most common commands compare:

Ubuntu (apt)macOS (brew)
sudo apt updatebrew update
sudo apt upgradebrew upgrade
sudo apt install gitbrew install git
sudo apt remove gitbrew uninstall git
apt search gitbrew search git
apt show gitbrew info git
sudo apt autoremovebrew autoremove

Note: Homebrew doesn't require sudo for most operations.

🚀 What's Next?

apt handles packages from Ubuntu's official repos, but there's more — Snap, Flatpak, AppImage, and PPAs let you install software from additional sources. We'll cover all of them in the next lesson.