APT and dpkg
APT and dpkg#
Concepts#
What Is a Package?#
A package is a bundle containing:
- The program’s files (binaries, libraries, configuration files, documentation)
- Metadata (name, version, description, author)
- Dependencies — other packages this one requires to work
On Debian/Ubuntu, packages use the .deb format. The system that manages these packages has two layers:
| Layer | Tool | Role |
|---|---|---|
| Low-level | dpkg |
Installs, removes, and queries individual .deb files. Does NOT handle dependencies. |
| High-level | apt |
Resolves dependencies, downloads packages from repositories, and calls dpkg underneath. |
In practice, you use apt for almost everything. dpkg is for special cases.
APT — The Package Manager#
Updating the Package Index#
Before installing anything, update your local list of available packages:
sudo apt update
This does not install or upgrade anything — it only downloads the latest list of package names and versions from the repositories. Think of it as refreshing a catalog.
Always run apt update before apt install or apt upgrade.
Searching for Packages#
Although I suggest using a search engine for this for a nicer UX, there is still the option to search through the configured sources.
apt search "web browser" # search by keyword
apt search ^nginx # search by name starting with "nginx"
apt show nginx # detailed info about a package
apt list --installed # list all installed packages
apt list --upgradable # list packages with available upgrades
Installing Packages#
sudo apt install nginx # install a single package
sudo apt install nginx curl wget # install multiple packages
sudo apt install -y nginx # skip confirmation prompt
sudo apt install nginx=1.24.0-2 # install a specific version
When you install a package, APT:
- Resolves all dependencies (packages it requires)
- Downloads the package and its dependencies
- Calls
dpkgto install each one - Runs post-installation configuration scripts
Removing Packages#
sudo apt remove nginx # remove the package (keep config files)
sudo apt purge nginx # remove package AND its config files
sudo apt autoremove # remove unused dependencies
sudo apt autopurge # remove unused dependencies AND their configs
The difference between remove and purge:
remove— deletes the program but leaves configuration files in/etc/intact. You can reinstall and your settings will be preserved.purge— deletes everything, including configuration files. More ruthless than the spanish inquisition.
Upgrading Packages#
sudo apt update # refresh the package list first!
sudo apt upgrade # upgrade all installed packages
sudo apt full-upgrade # upgrade, removing packages if necessary
upgrade— installs newer versions but never removes existing packages. Safe.full-upgrade— may remove packages if needed to resolve dependency conflicts. Used for distribution upgrades.
Other Useful apt Commands#
apt depends nginx # show what nginx depends on
apt rdepends nginx # show what depends on nginx
sudo apt install --reinstall nginx # reinstall a package
apt policy nginx # show version info and repository source
dpkg — The Low-Level Tool#
dpkg works directly with .deb files. You need it when:
- You downloaded a
.debfile manually (e.g., from a vendor’s website) - You need to query installed packages in detail
Installing a .deb File#
# Install a downloaded .deb file
sudo dpkg -i package.deb
# If it fails due to missing dependencies, fix them:
sudo apt install -f
dpkg -i does not resolve dependencies. The apt install -f command tells APT to fix broken dependencies by downloading whatever is missing.
Querying Packages#
dpkg -l # list all installed packages
dpkg -l nginx # check if nginx is installed
dpkg -L nginx # list all files installed by nginx
dpkg -S /usr/bin/curl # which package owns this file?
dpkg --status nginx # detailed info about installed package
dpkg --contents package.deb # list files inside a .deb (before installing)
Removing with dpkg#
sudo dpkg -r nginx # remove (keep configs)
sudo dpkg -P nginx # purge (remove everything)
In practice, prefer apt remove / apt purge because they handle dependencies.
APT Cache#
Downloaded packages are cached in /var/cache/apt/archives/. To free disk space:
sudo apt clean # delete all cached .deb files
sudo apt autoclean # delete only outdated cached .deb files
# See how much space the cache uses
du -sh /var/cache/apt/archives/
apt vs apt-get#
You may see older tutorials using apt-get and apt-cache. The apt command is the modern replacement that combines both:
| Old | New | Notes |
|---|---|---|
apt-get update |
apt update |
Same behavior |
apt-get install |
apt install |
apt shows a progress bar |
apt-get remove |
apt remove |
Same behavior |
apt-get upgrade |
apt upgrade |
Same behavior |
apt-cache search |
apt search |
Same behavior |
apt-cache show |
apt show |
Same behavior |
Use apt for interactive use. Use apt-get in scripts (its output format is more stable and machine-parseable).
Lab#
Exercise 1: Update and Explore#
# Update the package index
sudo apt update
# How many packages are installed?
dpkg -l | tail -n +6 | wc -l
# How many packages have upgrades available?
apt list --upgradable 2>/dev/null | tail -n +2 | wc -l
Exercise 2: Search and Inspect#
# Search for a package
apt search "text editor"
# Get details about a package
apt show nano
apt show vim
# See what nano depends on
apt depends nano
# Check if htop is installed
dpkg -l htop 2>/dev/null
Exercise 3: Install a Package#
# Install htop (an interactive process viewer)
sudo apt install -y htop
# Verify it is installed
which htop
dpkg -l htop
# See what files it installed
dpkg -L htop
# Run it (press q to quit)
htop
Exercise 4: Find Which Package Owns a File#
# What package provides /usr/bin/less?
dpkg -S /usr/bin/less
# What package provides /bin/ls?
dpkg -S /usr/bin/ls
# What package provides /usr/bin/passwd?
dpkg -S /usr/bin/passwd
Exercise 5: Remove a Package#
# Remove htop (keep config)
sudo apt remove htop
# Verify
which htop
# Should return nothing
# Check for leftover config
dpkg -l htop
# Status may show 'rc' (removed, config files remain)
# Purge to remove everything
sudo apt purge htop
# Clean up unused dependencies
sudo apt autoremove
Exercise 6: Install a .deb File Manually#
# Download a .deb file (using bat as an example - a cat replacement with syntax highlighting)
apt download bat
# This downloads the .deb file to the current directory
# Inspect the .deb
ls *.deb
dpkg --contents bat*.deb | head -20
# Install it
sudo dpkg -i bat*.deb
# If there are dependency issues:
sudo apt install -f
# Verify
bat --version 2>/dev/null || batcat --version
# Clean up
sudo apt purge bat
rm -f bat*.deb
Exercise 7: Manage the Cache#
# See cache size
du -sh /var/cache/apt/archives/
# List cached packages
ls /var/cache/apt/archives/ | head -10
# Clean the cache
sudo apt clean
# Verify
du -sh /var/cache/apt/archives/
Review#
1. What is the difference between `apt` and `dpkg`?
apt is the high-level package manager that resolves dependencies, downloads packages from repositories, and handles the full install workflow. dpkg is the low-level tool that installs/removes individual .deb files without resolving dependencies. apt uses dpkg internally.
2. Why should you run `apt update` before `apt install`?
apt update refreshes the local list of available packages and their versions. Without it, APT might try to install an outdated version or not know about newly available packages.
3. What is the difference between `apt remove` and `apt purge`?
remove deletes the package’s files but keeps configuration files in /etc/. purge deletes everything including configuration files. Use remove if you might reinstall later and want to keep your settings; use purge for a clean removal.
4. How do you install a manually downloaded `.deb` file?
sudo dpkg -i package.deb. If it fails due to missing dependencies, run sudo apt install -f to resolve them.
5. How do you find which package installed a specific file?
dpkg -S /path/to/file. For example, dpkg -S /usr/bin/curl tells you the file belongs to the curl package.
6. What does `apt autoremove` do?
It removes packages that were installed as dependencies but are no longer needed by any installed package. This happens when you remove a package that had dependencies — the dependencies may become orphaned.
7. When should you use `apt-get` instead of `apt`?
In scripts. apt-get has a more stable output format that is easier to parse programmatically. For interactive terminal use, apt is preferred.
Previous: Managing Users and Groups | Next: Repositories, PPAs, and Backports