What it is
apt is the command-line package manager for Debian-based Linux distributions, used for installing, upgrading, and removing software packages.
Installation
apt is typically pre-installed on Debian, Ubuntu, and their derivatives. If for some reason it’s missing (highly unlikely on a standard installation), you would first need to ensure apt-utils is installed:
Debian/Ubuntu:
sudo apt update
sudo apt install apt
Core Concepts
- Package: A
.debfile containing the program files, metadata, and installation scripts. - Repository (Repo): A server that hosts packages.
aptconsults configured repositories to find and download software. - Source List (
sources.listandsources.list.d/): Configuration files that tellaptwhich repositories to use. - Package Index: A local cache of package information (names, versions, descriptions) from the configured repositories. This is updated by
apt update. - Dependency: A package that another package requires to function correctly.
aptautomatically handles installing dependencies. - Pinning: A mechanism to force
aptto prefer or avoid specific versions of packages from certain repositories.
Commands / Usage
Managing Package Lists
-
Update the package index:
sudo apt updateFetches the latest package information from all configured repositories.
-
Upgrade installed packages:
sudo apt upgradeInstalls newer versions of all currently installed packages, without removing existing packages or installing new ones that aren’t dependencies.
-
Perform a full upgrade (dist-upgrade):
sudo apt full-upgradeInstalls newer versions of all currently installed packages, and may install new packages or remove existing ones if necessary to resolve dependencies (e.g., during a distribution upgrade).
Installing Software
-
Install a specific package:
sudo apt install firefoxDownloads and installs the
firefoxpackage and any necessary dependencies. -
Install multiple packages:
sudo apt install vim git htopInstalls
vim,git, andhtopalong with their dependencies. -
Install a specific version of a package:
sudo apt install firefox=115.0.2+build1-0ubuntu0.22.04.1Installs a precise version of
firefoxif available in the configured repositories. -
Install packages from a file list:
sudo apt install $(cat ~/packages.txt)Installs all packages listed in
~/packages.txt, one per line.
Removing Software
-
Remove a package:
sudo apt remove thunderbirdRemoves the
thunderbirdpackage but leaves configuration files behind. -
Remove a package and its configuration files:
sudo apt purge geditRemoves the
geditpackage and its associated configuration files. -
Remove unused packages and dependencies:
sudo apt autoremoveRemoves packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.
Searching for Software
-
Search for a package by name or description:
apt search text-editorLists packages whose names or descriptions contain "text-editor".
-
Show detailed information about a package:
apt show nanoDisplays the package name, version, dependencies, description, and other details for
nano. -
List files owned by a package:
apt-file list nanoLists all files installed by the
nanopackage. (Requiresapt-fileto be installed:sudo apt install apt-file && sudo apt-file update). -
Find which package owns a specific file:
apt-file search /etc/nginx/nginx.confShows which installed package provides the file
/etc/nginx/nginx.conf. (Requiresapt-file).
Package Sources and Configuration
-
View the list of configured repositories:
cat /etc/apt/sources.listDisplays the content of the main
aptsources file. -
View files in the sources.list.d directory:
ls /etc/apt/sources.list.d/Lists additional
.listfiles, often used for third-party repositories. -
Add a repository (often requires
software-properties-common):sudo add-apt-repository ppa:graphics-drivers/ppaAdds the specified Personal Package Archive (PPA) to your sources.
apt updatemust be run afterwards. -
Remove a repository:
sudo add-apt-repository --remove ppa:graphics-drivers/ppaRemoves the specified PPA.
apt updatemust be run afterwards.
Advanced Actions
-
Mark a package as manually installed:
sudo apt-mark manual firefoxPrevents
apt autoremovefrom consideringfirefoxfor removal if it was installed as a dependency. -
Mark a package as automatically installed:
sudo apt-mark auto htopMarks
htopas an automatically installed dependency, making it eligible for removal byapt autoremove. -
Hold a package at its current version:
sudo apt-mark hold unattended-upgradesPrevents
apt upgradeorapt full-upgradefrom upgradingunattended-upgrades. -
Un-hold a package:
sudo apt-mark unhold unattended-upgradesAllows
unattended-upgradesto be upgraded normally. -
Clean the local package cache:
sudo apt cleanRemoves downloaded package files (
.debfiles) from/var/cache/apt/archives/. Frees up disk space. -
Remove obsolete package files:
sudo apt autocleanRemoves downloaded package files that can no longer be downloaded and are largely useless. Less aggressive than
apt clean.
Common Patterns
-
Install a package and its dependencies, then remove it and its dependencies:
sudo apt install neofetch sudo apt remove neofetch sudo apt autoremoveA common way to temporarily install and then clean up software.
-
Upgrade the system and clean up afterward:
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt cleanA comprehensive command to update, upgrade, remove unneeded packages, and clean the cache. The
-yflag automatically confirms prompts. -
Install a list of packages from a file:
cat ~/my_dev_packages.txt | xargs sudo apt install -yAlternative to the
$(cat ...)method, useful if the list is very long. -
Reinstall a package:
sudo apt update && sudo apt --reinstall install package-nameUseful if a package’s files are corrupted or missing.
-
Install a package without interactive prompts:
sudo apt install -y package-nameThe
-yflag answers "yes" to all prompts, useful in scripts. -
Download a package without installing it:
apt-get --download-only install package-nameDownloads the
.debfile to/var/cache/apt/archives/but doesn’t install it.
Gotchas
apt upgradevsapt full-upgrade:apt upgradeis safer and will not remove packages.apt full-upgrade(formerlydist-upgrade) is more aggressive and can remove packages to resolve complex dependency changes, especially during major OS version upgrades. Always understand the difference.sudorequirement: Mostaptcommands that modify the system (install, remove, update) require root privileges, hence the common use ofsudo.- Repository order: The order of repositories in
/etc/apt/sources.listand/etc/apt/sources.list.d/can matter. If a package exists in multiple repositories,aptwill typically prefer the one listed earlier. - Third-party PPAs: While convenient, adding PPAs from untrusted sources can introduce security risks or system instability. Always verify the source of a PPA.
apt-getvsapt:aptis the newer, more user-friendly command-line interface.apt-getis the older, more established tool. For most interactive use,aptis preferred due to its progress bar and friendlier output.apt-getis often used in scripts for backward compatibility. They generally perform the same core functions.- Broken packages: Sometimes, interrupted installations or conflicting repositories can leave the package manager in a "broken" state.
sudo apt --fix-broken installcan often resolve these issues.