Skip to main content

Package Management with APT

Now that you're comfortable with the Linux command line, file operations, and process management, it's time to learn one of the most essential skills for any Ubuntu user: package management. APT (Advanced Package Tool) is the backbone of software installation and management on Ubuntu systems, allowing you to easily install, update, and remove software packages.

Learning Goals:

  • Understand what APT is and how it works
  • Search for available packages in repositories
  • Install, update, and remove software packages
  • Manage package repositories and clean up the system
  • Troubleshoot common package management issues

What is APT?

APT is a package management system used by Debian-based Linux distributions like Ubuntu. It handles the installation, upgrade, and removal of software packages, while automatically managing dependencies—other packages that your desired software needs to function properly.

Think of APT as an "app store" for your Ubuntu system, but one that's much more powerful and flexible. It connects to online repositories containing thousands of free and open-source software packages.

tip

APT actually consists of several tools. The most commonly used are apt (the modern command) and apt-get (the traditional command). We'll focus on apt as it provides more user-friendly output and is now the recommended tool.

Basic APT Commands

Updating Package Lists

Before installing or upgrading packages, you should always update your local package index to ensure you're getting the latest available versions:

Update package lists
sudo apt update

This command downloads the latest package information from all configured repositories but doesn't actually install or upgrade any packages.

Searching for Packages

To find available packages, use the search command:

Search for text editors
apt search text editor

For more specific searches, you can use grep to filter results:

Search for Vim specifically
apt search vim | grep ^vim

Installing Packages

To install a package:

Install Vim text editor
sudo apt install vim

You can install multiple packages at once:

Install multiple packages
sudo apt install htop git curl

Removing Packages

To remove a package while keeping configuration files:

Remove package but keep configs
sudo apt remove vim

To completely remove a package including configuration files:

Remove package and configs
sudo apt purge vim

Managing System Updates

Upgrading Packages

To upgrade all installed packages to their latest versions:

Upgrade all packages
sudo apt upgrade

For major system upgrades (like moving from Ubuntu 20.04 to 22.04), you would use:

Perform full system upgrade
sudo apt full-upgrade
warning

Always backup your important data before performing a full-upgrade as it may remove obsolete packages and could potentially cause issues with custom configurations.

Checking for Upgradable Packages

To see which packages can be upgraded without actually upgrading them:

List upgradable packages
apt list --upgradable

Package Information and Management

Viewing Package Information

Get detailed information about a package:

Show package information
apt show vim

This displays the package description, version, dependencies, size, and more.

Checking if a Package is Installed

Check package installation status
apt list --installed | grep vim

Viewing Package Files

To see which files are installed by a package:

List package files
dpkg -L vim

Managing Repositories

Repository Sources

APT repositories are defined in /etc/apt/sources.list and files in /etc/apt/sources.list.d/. You can view your current repositories:

View repository sources
cat /etc/apt/sources.list

Adding Repositories

To add third-party repositories, you typically use add-apt-repository:

Add a PPA repository
sudo add-apt-repository ppa:example/ppa-name
sudo apt update

System Maintenance

Cleaning Up

Remove downloaded package files that are no longer needed:

Clean package cache
sudo apt autoclean

Remove all downloaded package files:

Remove all package cache
sudo apt clean

Remove automatically installed packages that are no longer needed:

Remove unused packages
sudo apt autoremove

Common Pitfalls

  • Forgetting to update: Always run apt update before installing new packages to ensure you're getting the latest versions
  • Using apt-get instead of apt: While apt-get still works, apt provides better user experience and is now recommended
  • Ignoring dependency issues: If you encounter dependency problems, try sudo apt --fix-broken install
  • Mixing package sources: Avoid adding too many third-party repositories as they can cause conflicts
  • Running out of disk space: Regularly clean package cache with apt autoclean and apt autoremove

Summary

APT is your gateway to the vast ecosystem of Ubuntu software. You've learned how to:

  • Update package lists and search for software
  • Install, upgrade, and remove packages
  • Manage system updates and clean up unused packages
  • Work with repositories and troubleshoot common issues

Mastering APT will make you much more efficient at managing your Ubuntu system and installing the tools you need for development and daily use.

Quiz

Show quiz
  1. What command should you run before installing new packages to ensure you have the latest package information?
  2. How do you completely remove a package including its configuration files?
  3. What is the difference between apt upgrade and apt full-upgrade?
  4. Which command shows detailed information about a package including its description and dependencies?
  5. How can you safely remove downloaded package files that are no longer needed?

Answers:

  1. sudo apt update
  2. sudo apt purge package-name
  3. apt upgrade updates packages without removing others, while apt full-upgrade may remove packages to resolve complex dependencies
  4. apt show package-name
  5. sudo apt autoclean