How to Use apt Command to Manage Packages in Linux

How to Use apt Command to Manage Packages in Linux

This article shows you how to use the apt command in Ubuntu, Debian, Linux Mint, or any other Debian or Ubuntu-based distributions, with examples so that you can easily manage packages.

APT is a powerful package management tool that can be used to search, install, update, upgrade, and manage the packages in a Linux operating system.

In addition, it automatically manages package dependencies, installing required software as needed and removing it when no longer required.

Table of Contents

What is apt Command in Linux?

apt (Advanced Package Tool) is a command-line tool that is used for easy interaction with the dpkg packaging system in Debian and Debian-based Linux distributions like Ubuntu. It is a collection of tools distributed in a package named apt, aiming to simplify the process of managing software.

dpkg is responsible for packing the software in an easy-to-install package. It is the Debian packages manager. When apt is used, it invokes the dpkg program to install or remove applications while including additional functions as a dependencies resolution.

In other words, apt acts as a user-friendly layer that interacts with the dpkg packaging system.

How to Use apt Command in Linux

Remember that most of the apt commands must be run as a user with sudo privileges.

Fetch Updates

Before any operations with apt, we must ensure that our local copy of the package database is up-to-date. Without this, the system won’t know whether newer packages are available.

The apt update command downloads up-to-date information about available software packages:

sudo apt update
apt update

This downloads the latest up-to-date software packages and their metadata – package names, version numbers, etc.

List Available Updates

You can view the list of packages that have a newer version ready to be upgraded. For this, run the following command:

apt list --upgradeable
apt list upgradable packages

apt list --upgradeable is a hint always displayed at the end of the output of the apt update command.

Upgrade Packages

Running an apt upgrade will update all the packages that have a new version available. Remember that you need to perform an apt update before the apt upgrade command, so that apt knows that new versions of packages are available.

sudo apt upgrade
apt upgrade

Full System Upgrade

The common difference between an apt upgrade and an apt full-upgrade is that a full-ugrade will remove the installed packages if needed to upgrade the whole system.

This is useful when you upgrading from Debian version 10 to 11, for example.

sudo apt full-upgrade
apt full-upgrade

Commands with full-upgrade require special care to be used safely, and there’s no good reason even for experienced users to run them routinely.

Installing New Packages

Once your packages database is updated, you can install any packages with the apt install command. For example, let’s install Nginx Web Server:

sudo apt install nginx
apt command installing package

If you want to install multiple packages at once, for example, nginx and firewalld, specify them as a space-separated list:

sudo apt install nginx firewalld
apt command installing multiple packages

Removing Packages

To remove (uninstall) an installed package, use the apt remove command. For example, to remove a package called nginx, enter:

sudo apt remove nginx
apt remove package

You can also specify multiple packages, separated by spaces:

sudo apt remove nginx firewalld
apt remove multiple packages

We can also easily remove packages with the apt purge command. The primary difference is that the apt remove command will uninstall the given packages, leaving its configuration files behind.

Whereas apt purge not only removes the package but also removes all configuration files outside the home directory.

sudo apt purge nginx
apt purge

In addition, you can also remove all unwanted packages with the following command:

sudo apt autoremove
apt autoremove

The autoremove option is used to remove packages that were automatically installed to satisfy dependencies for other packages but now are no longer needed as dependencies.

Searching Packages

The apt search command allows you to search for a given package in the list of the available packages. For example, to search for nginx packages, enter:

apt search nginx
Search for packages with apt

Searching Through Installed Packages

The apt list command displays both installed and packages available to install. What if you want to list specific currently installed packages only? Just pass the --installed option to the apt list command. The good news is you can use a wildcard (*) in searches.

For example, to find all the packages currently installed on the system which contain the nginx within their name, run:

apt list --installed nginx*
apt list installed packages

Get Details About Package

The output of the apt search command gives you a brief introduction to the packages. If you want more details, use the apt show command.

apt show nginx
apt show details about a package

It’s important to note that you must give the exact package name to the apt show command. Otherwise, the apt show won’t work.

Conclusion

Knowing how to manage packages with APT is essential to Linux system administration. Of course, there is more to APT, but this should get you started.

Related: How to Use APT with Proxy on Ubuntu and Debian

For more about apt command in Linux, consult its manual page. As always, use the form below for feedback, questions, or suggestions.

Bobby Borisov
Bobby Borisov

Bobby is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *