Installing and Managing Software Packages with APT

Learn how to install, update, and remove software packages with APT, the Debian/Ubuntu package manager. Step-by-step commands, tips, and best practices for Linux and DevOps beginners.

Installing and Managing Software Packages with APT

A quick, practical guide to using APT to install, update, and manage software packages on Debian-based systems. This tutorial focuses on the commands and concepts beginners need to feel confident when working with apt and related tools.

Getting started with APT: update, upgrade, and the basic workflow

Before installing anything, refresh the package index so your system knows what versions are available. The common workflow is: update package lists, upgrade installed packages, then install or remove packages as needed.

Example: update package lists and upgrade safely

# Refresh package lists (reads package info from configured repositories)
sudo apt update

# Upgrade only installed packages to newest versions without removing anything
sudo apt upgrade

# Optional: perform necessary removals/replacements for more complex upgrades
sudo apt full-upgrade

A few notes:

  • apt update downloads the latest package metadata. It does not upgrade software.
  • apt upgrade installs upgrades but won't remove packages; apt full-upgrade (or apt-get dist-upgrade) may remove or replace packages to satisfy dependencies.
  • Use sudo because APT modifies system files.

Searching for and installing packages

You can search the package index and inspect packages before installing. This helps you find the right package names and see descriptions, versions, and dependencies.

Search and inspect:

# Search for packages containing "nginx"
apt search nginx

# Show detailed information about a package
apt show nginx

Install a package:

# Install a package (prompts for confirmation)
sudo apt install nginx

# Install and automatically answer yes to prompts (use with care)
sudo apt install -y nginx

Installing a specific version:

# List available versions
apt list -a packagename

# Install a specific version
sudo apt install packagename=1.2.3-4

Practical tips:

  • Use apt search when you don't know the exact package name (e.g., search for “image viewer”).
  • apt show lists the homepage, dependencies, and installed size.
  • Don’t use -y blindly on unfamiliar operations; it can accept removals you didn’t expect.

Upgrading, removing, and cleaning packages

Managing upgrades and removals keeps your system tidy and secure. APT provides commands to remove unneeded files and to purge configuration left behind by removed packages.

Upgrade and view upgradeable packages:

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

# Upgrade those packages
sudo apt upgrade

Removing and purging:

# Remove a package but keep configuration files
sudo apt remove package-name

# Remove a package and its configuration files
sudo apt purge package-name

Cleaning up:

# Remove packages that were automatically installed and are no longer needed
sudo apt autoremove

# Clean the local repository of retrieved package files
sudo apt clean

Guidance:

  • apt remove keeps config files under /etc; apt purge deletes them.
  • apt autoremove helps avoid disk bloat from orphaned dependencies.
  • apt clean removes cached .deb files; apt autoclean removes only outdated cached packages.

Managing repositories and third-party packages (PPAs)

APT gets packages from repositories listed in /etc/apt/sources.list and files under /etc/apt/sources.list.d/. Adding third-party repositories (or PPAs on Ubuntu) is common for newer versions of software.

Add an official PPA (Ubuntu):

# Add a PPA, refresh metadata, install package
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.1

Add a repository with a signed key (preferred modern approach):

# Example: add a repository with signed-by option
sudo tee /etc/apt/sources.list.d/example.list <<EOF
deb [signed-by=/usr/share/keyrings/example.gpg] https://example.org/apt stable main
EOF

# Import signing key
curl -fsSL https://example.org/keys/example.gpg | sudo gpg --dearmour -o /usr/share/keyrings/example.gpg

# Refresh and install
sudo apt update
sudo apt install example-package

Notes on keys and security:

  • Modern best practice is to store repository keys as keyring files and reference them via signed-by in the sources entry. apt-key is deprecated.
  • Only add repositories you trust; untrusted sources can compromise system security.
  • Use apt update after adding a new source so the package lists become available.

Common Pitfalls

  • Mixing package management tools: Avoid running apt while dpkg is already processing packages (e.g., another apt instance or unattended-upgrades). If operations overlap, you'll see lock errors.
  • Broken packages after interrupted installs: Power loss or interrupted installs can leave packages in a half-configured state.
  • Adding untrusted repositories or using apt-key globally: Using apt-key add to put a key in the global keyring is deprecated and increases risk if the repo is compromised.

Quick fixes for common problems:

# If apt/dpkg is broken or interrupted, try:
sudo dpkg --configure -a
sudo apt --fix-broken install

# If you see lock errors, find and stop other apt processes (use carefully)
ps aux | grep -E 'apt|dpkg'
sudo kill <pid>  # only if you're sure another process is stuck

Additional advice:

  • If a package installation complains about missing dependencies, apt --fix-broken install will attempt to repair the situation.
  • When a repository key is missing, apt update will show a GPG error; follow the repository's instructions to add the key as a keyring and reference it with signed-by.

Next Steps

Practice using a test VM or container:

# Launch a disposable container to experiment safely (requires Docker)
docker run -it --rm ubuntu:24.04 bash
apt update && apt install -y curl

Closing tips: always refresh indexes with apt update before installing, read apt show for package details, and prefer using signed repositories. With these commands and practices you’ll be able to install, upgrade, and maintain software on Debian-based systems with confidence.

👉 Explore more IT books and guides at dargslan.com.