apt Linux Command With 13 Examples | phoenixNAP KB (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 13 Examples | phoenixNAP KB (1)

Prerequisites

  • An account withsudo privileges.
  • Access to the terminal.
  • A Debian or Ubuntu system. (This tutorial usesUbuntu 20.04).

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 process. Some common arguments are:

OptionDescription
-d, --download-onlyDownloads a package but does not continue with the installation.
--no-downloadDoes not download any packages and uses the ones already downloaded.
--assume-noAnswers "no" to all prompts.
-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.
-h, --helpPrints a help guide and leads to an Easter Egg.

Note: Learn the difference between APT and APT-GET.

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.
removeDeletes a package but leaves configuration files.
purgeDeletes a package and any configuration files.
autoremoveRemoves no longer required dependencies.
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 atext editor.

apt commands require the use of 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:

How to Use apt

Linux systems already have a primary package manager for deb files, 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.

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 to get the latest version.

sudo apt update
apt Linux Command With 13 Examples | phoenixNAP KB (3)

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
apt Linux Command With 13 Examples | phoenixNAP KB (4)

To upgrade a specific package, append the name:

sudo apt upgrade lsof
apt Linux Command With 13 Examples | phoenixNAP KB (5)

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
apt Linux Command With 13 Examples | phoenixNAP KB (6)

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 distribution release's life cycle end.

sudo apt full-upgrade
apt Linux Command With 13 Examples | phoenixNAP KB (7)

Install Packages with apt install

The apt install command installs a specified package from the repository.

sudo apt install ffmpeg
apt Linux Command With 13 Examples | phoenixNAP KB (8)

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

Remove Packages with apt remove

To remove an installed package, run:

sudo apt remove ffmpeg
apt Linux Command With 13 Examples | phoenixNAP KB (10)

Type y or 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 ffmpeg
apt Linux Command With 13 Examples | phoenixNAP KB (11)

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
apt Linux Command With 13 Examples | phoenixNAP KB (12)

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 my mysql-server, but remove apache2, execute:

sudo apt remove apache2 mysql-server+
apt Linux Command With 13 Examples | phoenixNAP KB (13)

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 easier.

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

apt list | more
apt Linux Command With 13 Examples | phoenixNAP KB (14)

To show only installed packages, filter the output with:

apt list --installed
apt Linux Command With 13 Examples | phoenixNAP KB (15)

To get a list of all upgradeable packages, use:

apt list --upgradeable
apt Linux Command With 13 Examples | phoenixNAP KB (16)

In this case, the output doesn't list any package, meaning that there are no packages to be upgraded.

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
apt Linux Command With 13 Examples | phoenixNAP KB (17)

List Package Dependencies with apt depends

To print all dependencies linked to a package, run:

apt depends lsof
apt Linux Command With 13 Examples | phoenixNAP KB (18)

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
apt Linux Command With 13 Examples | phoenixNAP KB (19)

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

apt search --names-only lsof
apt Linux Command With 13 Examples | phoenixNAP KB (20)

The apt search command prints results if run without sudo as well, as long as 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 Linux Command With 13 Examples | phoenixNAP KB (21)

Conclusion

After going through this guide, you now know how to add, remove, install, and work with packages with the apt Linux command.

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

apt Linux Command With 13 Examples | phoenixNAP KB (2024)

References

Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 5820

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.