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:
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.
Example 2: List Available Package Upgrades
If APT finds package upgrades, the following command will list all of them:
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:
Alternatively, the following command will upgrade the whole system by removing, installing, and upgrading packages as needed:
Example 4: Upgrading Specific Packages
If you don’t want to install all the package upgrades but specific ones, use the following command structure:
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:
Example 6: Searching for a Package
To check if a package is available from the package repo, use the following command:
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:
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:
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:
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:
To get a list of all the hold packages, run the following command:
To remove the hold mark from a package, use the following command:
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:
APT should take care of all the necessary dependencies as well.
Example 12: Uninstalling a Package
To uninstall a package, use the following command:
Generally, APT won’t remove the package dependencies. To remove them afterward, run the following command:
We can also instruct APT to perform both of these actions in a single command:
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:
We can filter this output using grep to check if a package with a particular name/pattern is installed:
Learn more about grep.
Example 14: Package Details
Before installing a package from the repo, we can check detailed info about it:
Example 15: Downloading a Package from Repo
To download a package from the package repo(s) without installing it, use the following command:
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:
Alternatively, we can manually edit it using any text editor:
In the case of Ubuntu, to auto select the nearest mirror, update the repo URLs with the following one:
After updating sources.list, you have to update the APT cache:
Bonus: APT Documentation
The following command will print a quick help page:
To learn more about all the available options with in-depth explanations, check out the man page:
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!