How to Use apt Command to Manage Packages in Linux

This guide serves as a quick reference for the apt commands.

Ho to Use APT Command to Manage Packages

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

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. 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, which aim is 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. In fact when apt is used it invoke 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

Keep in mind, that most of the apt commands must be run as a user with sudo privileges.

Fetch Updates

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

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

sudo apt update
apt update

This download 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

In fact, apt list --upgradeable is a hint that is always displayed at the very end of the apt update output.

Upgrade Packages

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

sudo apt upgrade
apt upgrade

Full System Upgrade

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

This is useful when you want to upgrade 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 but it will leave 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 – you can use wildcard (*) in searches.

For example, to find all the packages currently installed on the system which contains 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 of 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 need to 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 an essential part of 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 any feedback, questions or suggestion.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge