apt Basics

apt is the package manager for Debian, Ubuntu, and derivatives. You'll use it constantly.

Update Package Lists

Before installing anything, update your package index:

Terminal
$sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB] Fetched 119 kB in 1s (119 kB/s) Reading package lists... Done Building dependency tree... Done 45 packages can be upgraded.

This downloads the latest package information from repositories. It doesn't install anything yet.

Always Update First

Running apt install without apt update might install outdated versions or fail to find packages.

Install Packages

Terminal
$sudo apt install htop
Reading package lists... Done Building dependency tree... Done The following NEW packages will be installed: htop 0 upgraded, 1 newly installed, 0 to remove. Do you want to continue? [Y/n] y Setting up htop (3.0.5-7build2) ...

Install multiple packages at once:

Terminal
$sudo apt install git curl wget vim
(installs all four)

Skip Confirmation

Terminal
$sudo apt install -y nginx
(installs without asking)

-y automatically answers "yes" - useful in scripts.

Remove Packages

Terminal
$sudo apt remove htop
The following packages will be REMOVED: htop Do you want to continue? [Y/n]

This removes the package but keeps configuration files.

Remove with Config Files

Terminal
$sudo apt purge htop
(removes package AND config files)

Use purge when you want a clean removal.

Clean Up Unused Dependencies

Terminal
$sudo apt autoremove
The following packages will be REMOVED: libfoo libbar 0 upgraded, 0 newly installed, 2 to remove.

Removes packages that were installed as dependencies but are no longer needed.

Upgrade Packages

Terminal
$# Update package lists
$sudo apt update
45 packages can be upgraded.
$
$# Upgrade all packages
$sudo apt upgrade
The following packages will be upgraded: base-files bash coreutils...

update vs upgrade

  • apt update - Downloads package information (what's available)
  • apt upgrade - Actually installs newer versions

Full Upgrade

Terminal
$sudo apt full-upgrade
(may remove packages if needed for upgrade)

full-upgrade can remove packages to resolve conflicts. Regular upgrade never removes anything.

Check If Package Is Installed

Terminal
$apt list --installed | grep nginx
nginx/jammy,now 1.18.0-6ubuntu14 amd64 [installed]
$
$# Or use dpkg
$dpkg -l | grep nginx
ii nginx 1.18.0-6ubuntu14 amd64 small, powerful web server

The Classic Combo

Terminal
$sudo apt update && sudo apt upgrade -y
(update lists, then upgrade everything)

Run this regularly to keep your system current.

Knowledge Check

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

Quick Reference

CommandPurpose
apt updateRefresh package lists
apt upgradeUpgrade installed packages
apt install pkgInstall a package
apt remove pkgRemove (keep configs)
apt purge pkgRemove completely
apt autoremoveClean unused deps
apt list --installedShow installed

Key Takeaways

  • Always apt update before installing
  • apt install package to install
  • apt remove keeps configs, apt purge removes all
  • apt autoremove cleans up orphaned dependencies
  • apt update && apt upgrade keeps system current

Next: finding the right packages to install.