15 APT Command Examples on Linux

0

In this guide, we will demonstrate a number of ways of using the APT command on Linux.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components:

  • A properly configured Linux distro that uses APT as the package manager, for example, Debian, Ubuntu, Linux Mint, Devuan, etc.
  • Basic understanding of the CLI and package management.

The APT Command

Any Linux distro comprises a number of packages. To manage these numerous packages in an efficient manner, almost all distros use one or more package managers.

APT is one such package manager. It’s a CLI tool that can install, uninstall, and manage DEB packages on distros like Debian, Ubuntu, and Debian/Ubuntu-based ones.

If an APT command is to make system-level changes, it must run with root privilege (with the help of the sudo command).

Using the APT Command

Example 1: Updating the List of Available Packages
Before APT can work with packages, it needs a working database of all the available packages.

To generate the most up-to-date database, run the following command:

15 apt command examples on linux 15 APT Command Examples on Linux 15 APT Command Examples on Linux

Here, APT will fetch the latest package database from the package repo(s). If any package update is available, APT will also print a notification.

Check File Permissions with ls Linux

Example 2: List Available Package Upgrades
If APT finds package upgrades, the following command will list all of them:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149195 91 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149195 91 15 APT Command Examples on Linux

Example 3: Upgrading Packages
If one or more package updates were found, then you can upgrade all of them at once using the following command:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149195 397 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149195 397 15 APT Command Examples on Linux

Alternatively, the following command will upgrade the whole system by removing, installing, and upgrading packages as needed:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 391 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 391 15 APT Command Examples on Linux

Example 4: Upgrading Specific Packages
If you don’t want to install all the package upgrades but specific ones, use the following command structure:

sudo apt –only-upgrade install [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 99 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 99 15 APT Command Examples on Linux

Example 5: Downgrading Packages
Sometimes, a package upgrade may break things. In such a situation, you may want to downgrade the problematic package(s) to an earlier version.

To downgrade a package, run the following command:

apt install [package_name]=[older_package_version]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 953 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 953 15 APT Command Examples on Linux

Example 6: Searching for a Package
To check if a package is available from the package repo, use the following command:

apt search [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 2 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 2 15 APT Command Examples on Linux

Example 7: Installing a Package
If a package exists on the package repo(s) specified in the sources.list, then you can directly install it using the following command:

sudo apt install [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 771 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 771 15 APT Command Examples on Linux

Example 8: Installing a Specific Package Version
The procedure is the same as example #5. If you want to install a specific version of a package, specify it in the following manner:

sudo apt install [package_name]=[package_version]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 616 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149196 616 15 APT Command Examples on Linux

If the package version is not specified, then APT will automatically install the latest package.

Example 9: Listing Available Package Versions
The default package repo(s), in most cases, will host multiple versions of a package. The following command will reveal all the available package versions:

apt-cache policy [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 528 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 528 15 APT Command Examples on Linux

Example 10: Holding a Package
Whenever running the

command, it will check for upgrades for all the installed packages. In certain situations, however, you may want to skip upgrading certain packages for various reasons (stability, compatibility, etc.).

In such a situation, you can mark the target package(s) as hold. Basically, whenever performing automatic package upgrades, APT will skip these packages.

To mark a package as hold, run the following command:

sudo apt-mark hold [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 92 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 92 15 APT Command Examples on Linux

To get a list of all the hold packages, run the following command:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 726 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 726 15 APT Command Examples on Linux

To remove the hold mark from a package, use the following command:

sudo apt-mark unhold [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 361 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 361 15 APT Command Examples on Linux

Example 11: Installing a DEB Package
Debian and Debian-based systems use DEB as the software packaging. All the packages from the package repo(s) also come as DEB files.

To install a DEB package, use the following APT command:

sudo apt install [path_to_deb]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 757 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 757 15 APT Command Examples on Linux

APT should take care of all the necessary dependencies as well.

Example 12: Uninstalling a Package
To uninstall a package, use the following command:

sudo apt remove [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 376 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149197 376 15 APT Command Examples on Linux

Generally, APT won’t remove the package dependencies. To remove them afterward, run the following command:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 946 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 946 15 APT Command Examples on Linux

We can also instruct APT to perform both of these actions in a single command:

sudo apt autoremove –purge [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 883 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 883 15 APT Command Examples on Linux

Note that purging a package will also remove all the package-related configuration files, so exercise caution.

Example 13: Listing Installed Packages
APT keeps track of all the packages installed from the package repo(s) and DEB packages.

The following command will list all the installed packages that APT is keeping track of:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 24 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 24 15 APT Command Examples on Linux

We can filter this output using grep to check if a package with a particular name/pattern is installed:

apt list –installed | grep [pattern]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 507 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149198 507 15 APT Command Examples on Linux

Learn more about grep.

Example 14: Package Details
Before installing a package from the repo, we can check detailed info about it:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 335 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 335 15 APT Command Examples on Linux

Example 15: Downloading a Package from Repo
To download a package from the package repo(s) without installing it, use the following command:

apt download [package_name]

15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 701 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 701 15 APT Command Examples on Linux
15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 443 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149199 443 15 APT Command Examples on Linux

It will download the package as a DEB file in the current directory. You can later install it using APT following the steps demonstrated in example #11.

Bonus: Editing sources.list
The sources.list file contains the URL for all the APT repos. We can open it using APT for editing:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 637 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 637 15 APT Command Examples on Linux

Alternatively, we can manually edit it using any text editor:

sudo vim /etc/apt/sources.list

15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 406 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 406 15 APT Command Examples on Linux

In the case of Ubuntu, to auto select the nearest mirror, update the repo URLs with the following one:

mirror://mirrors.ubuntu.com/mirrors.txt

15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 900 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149200 900 15 APT Command Examples on Linux

After updating sources.list, you have to update the APT cache:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 503 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 503 15 APT Command Examples on Linux

Bonus: APT Documentation
The following command will print a quick help page:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 965 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 965 15 APT Command Examples on Linux

To learn more about all the available options with in-depth explanations, check out the man page:

15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 610 15 APT Command Examples on Linux15 apt command examples on linux 15 APT Command Examples on Linux 1714149201 610 15 APT Command Examples on Linux

Final Thoughts

In this guide, we demonstrated numerous ways of using the APT command. We learned about installing, uninstalling, upgrading, downgrading, and downloading packages on Debian and Debian-based systems.

While APT handles DEB packages, there are other Linux packaging formats, for example, flatpak, snap, etc. These packages are designed to be practically universal Linux packages that can be installed on any Linux system.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More