apt Linux Command with Examples

December 3, 2024

Introduction

apt is an interactive command-line tool for managing deb packages on different Linux distributions. The package manager installs, removes, updates, and upgrades deb packages.

This tutorial will teach you how to use the apt Linux command with examples.

apt Linux Command with Examples

Prerequisites

apt Command Syntax

The basic apt syntax is:

apt command
apt [options] command [packages]

Add at least one command to run the apt tool successfully. To manage a specific package, include the name of the package in the command.

The apt utility supports several options for modifying its output.

apt Command Options

apt allows the use of different options to customize the process. Some common arguments are:

OptionDescription
-d, --download-onlyDownloads a package but does not continue with the installation.
-b, --compile, --buildEnables users to compile a source package into a binary package.
--no-install-recommendsEnsures only the essential dependencies are installed.
--install-suggestsTells the package manager to also install suggested packages in addition to the main package and its dependencies.
--no-downloadDoes not download any packages and uses the ones already downloaded.
--assume-noAnswers no to all prompts.
-m, --ignore-missingAllows apt to ignore packages that cannot be downloaded due to missing files or unavailable repositories.
--fix-missingSkips packages that are missing from the repository or unavailable for download.
-yAnswers yes to prompts without interrupting the process.
-f, --fix-brokenTries to fix broken dependencies.
-s, --simulateDoes not alter the system, only displays what the output will be.
-V, --verbose-versionsProvides detailed version information for packages during installation, upgrade, or removal.
-q, --quietReduces or silences the apt output verbosity.
--show-progressShows the percentage completed for each package being downloaded.
--no-show-upgradedSuppresses the display of information about upgraded packages during an installation or upgrade process.
--no-upgradePrevents the package upgrade if it is already installed on the system.
--only-upgradeUpgrades a package only if it is already installed.
--reinstallReinstalls a packgage.
-h, --helpPrints a help guide and leads to an Easter Egg.

Most Common apt Commands

The apt tool works with many commands. The following table provides the most common examples.

CommandDescription
updateGets the info about the latest versions of available packages but does not install any upgrades.
upgradeDownloads the up-to-date package versions and upgrades installed packages to the new version.
full-upgradeUpgrades the currently installed packages and removes packages not needed for the full system upgrade.
installInstalls a specified package from the repository.
reinstallReinstalls a package that is already installed on the system
cleanClears out the local cache where package files are stored after they are downloaded and installed.
removeDeletes a package but leaves configuration files.
purgeDeletes a package and any configuration files.
autoremoveRemoves no longer required dependencies.
checkChecks for broken dependencies.
downloadDownloads a package without installing it.
listLists all available packages or packages according to specific criteria.
searchSearches for packages whose name or description contains the search term.
showPrints details about a specific package.
edit-sourcesAllows users to edit package sources in a text editor.

apt commands require sudo to complete when root permissions are necessary to read, write, or execute the files.

When you try to install some tools without sudo, the following error message appears:

apt install mysql-server
apt install mysql-server terminal output

apt Linux Examples

Linux systems already have a primary package manager for deb files called dpkg. Still, apt is a more straightforward way to handle deb packages. The apt command-line utility manages packages automatically and installs and removes dependencies as needed.

The following text presents several ways to use the apt command.

Update Packages with apt update

The apt update command updates the local repository with package metadata (info about the latest versions available) and prints the list of upgradeable packages. Always execute the update command before upgrades or installations to ensure you get the latest version.

sudo apt update
terminal output sudo apt update

Upgrade Packages with apt upgrade

Upgrade installed packages to the latest versions with apt upgrade. When you run the command without the package name, apt upgrade affects all installed packages:

sudo apt upgrade
terminal output for sudo apt upgrade

To upgrade a specific package, for example, lsof, append the name:

sudo apt upgrade lsof
terminal output for sudo apt upgrade lsof

The update and upgrade commands also print output when executed together. To run these commands in one step and avoid being asked to confirm the process, use the -y flag:

sudo apt update && sudo apt upgrade -y

Full-Upgrade Packages with apt full-upgrade

The command upgrades all installed packages. It also removes any packages if that is needed to upgrade the whole system. The full-upgrade is often done at the end of the distribution release's life cycle.

sudo apt full-upgrade
sudo apt full-upgrade terminal output

Install Packages with apt install

The apt install command installs a specified package from the repository. For example, to install ffmpeg, run:

sudo apt install ffmpeg
sudo apt install ffmpeg terminal output

Before running apt install, update and upgrade packages to get the latest versions.

Only Download Packages with apt download

The apt download-only feature allows users to use deb files without installing them. To download packages without starting the installation, run:

sudo apt download apache2
terminal output for sudo apt download apache2

Remove Packages with apt remove

To remove an installed package, run:

sudo apt remove lsof 
sudo apt remove lsof  terminal output

Type y after being prompted, and the command removes the package.

Remove All Configuration Files with apt purge

The remove command deletes the specified packages. Still, the command doesn't always remove all configuration files. Delete the package and configuration files, with purge:

sudo apt purge lsof
sudo apt purge lsof terminal output

Remove Unused Dependencies with apt autoremove

Package dependencies often remain on the system even when a package is removed. To remove the unneeded dependencies and save space, use:

sudo apt autoremove
sudo apt autoremove terminal output

Install and Remove Packages with one apt Command

Using apt with + or - suffixes added to the package names allows users to install and remove packages with one command. For instance, to install lsof, but remove apache2, execute:

sudo apt remove apache2 lsof+
sudo apt remove apache2 lsof+ terminal output

List Packages with apt list

When executed without arguments, apt list prints the names and details of all available, installed, and upgradeable packages. Since the output is extensive, pipe the command with less or more to navigate the output more easily.

For instance, pipe apt list with more to move through the terminal one page at a time:

apt list | more
apt list | more terminal output

To show only installed packages, filter the output with:

apt list --installed
apt list --installed terminal output

To get a list of all upgradeable packages, use:

apt list --upgradeable
apt list --upgradeable terminal output

Narrow the search even more by printing only a list of packages satisfying certain criteria. For instance, list packages containing the term lsof with:

apt list lsof
terminal output for apt list lsof

List Package Dependencies with apt depends

To print all dependencies linked to a package, run:

apt depends lsof
apt depends lsof terminal output

Search Packages with apt search

The apt search command scans names and descriptions of available packages for a specified search term. For instance, find all packages containing the term lsof:

sudo apt search lsof
sudo apt search lsof terminal output

Assuming the search term is mentioned in many packages, the output is extensive. To narrow down the search, use the --names-only flag:

sudo apt search lsof --names-only
sudo apt search lsof --names-only terminal output

The apt search command prints results if run without sudo as well, as long as the user has access to the packages in question.

Get Package Info with apt show

To display details about the package like the dependencies, content description, download and installation size, sources, etc., use:

apt show lsof
apt show lsof terminal output

Conclusion

This guide explained how to add, remove, install, and work with packages with the apt Linux command. Follow the examples from the guide to test all the commands.

Next, the difference between APT and Snap or APT and YUM.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use APT Package Manager
August 22, 2024

The APT package manager is a powerful tool for managing software on Debian-based Linux distributions. It simplifies installing, updating, and removing packages by automating the retrieval and configuration of...
Read more
How to Use apt-get reinstall on Debian and Ubuntu
October 22, 2020

When packages are accidentally removed or modified on Debian or Ubuntu, reinstalling them usually resolves the problem. The --reinstall flag is an apt-get command shortcut for reinstalling packages using the command line...
Read more
How to Fix 'add-apt-repository command not found' on Ubuntu & Debian
March 21, 2024

This guide shows you how to fix the 'add-apt-repository command not found' error in Ubuntu. This specific Ubuntu error appears when trying to add a new software repository and indicates that the command is not installed on your...
Read more
How to Use the Apt-Get Command in Linux
May 6, 2019

Advanced Package Tool (APT) is a package management system used on Debian, Ubuntu and other Linux distributions. It is quite powerful, allowing you to search for, install, manage, update, and remove software. Learn apt-get comm...
Read more