APT Command in Linux [A to Z Guide]

If you have used Debian or Debian-based distributions such as Ubuntu or Linux Mint, then the APT command-line tool needs no introduction. APT, short for Advanced Package Tool is a package management tool for Debian systems. The APT utility helps users to perform a variety of tasks including installing, updating, upgrading, and removing software packages. The APT utility is used interactively, often requiring the user to type ‘Y’ to proceed with the operation such as installing or removing a package.

On older Debian/Ubuntu-based distros, apt-get was used. In newer versions such as Ubuntu 18.04/20.04 and later and Debian 10/Mint 20 apt replaces the old apt-get utility is backward-compatible with apt-get.

In this tutorial, we highlight various use cases of the apt command in Linux.

1) Update APT package index

In Debian/Ubuntu systems repositories are specified in the /etc/apt/sources.list file. The APT package index is a database of all the packages defined in the sources.list file. It’s usually recommended to update the local APT package index to sync the changes made in the repositories. This is especially crucial after a fresh system install and before installing packages.

To update the APT database ( package index file) , run the command.

$ sudo apt update
APT command on Linux
apt update command

2) Upgrade packages using APT command

Apt update command mentioned above will not install or upgrade any packages. So after updating the list apt know which packages need update.

Now, to upgrade installed packages to their latest versions using the command:

$ sudo apt upgrade
upgrade packages using apt
upgrade packages using apt

To upgrade an individual package use the syntax:

$ sudo apt upgrade package-name

The command above only upgrades the package and leaves the rest in their current versions.

3) full-upgrade and dist-upgrade

The apt full-upgrade command upgrades the packages and removes some of the currently installed packages when needed to wholly upgrade the system.

$ sudo apt full-upgrade

The dist-upgrade command, apart from fully upgrading your system, intelligently handles the installation and removal of software packages. Its smart conflict resolution functionality first upgrades the most essential packages and accords lower priority to the less critical packages

$ sudo apt dist-upgrade

It’s also worth mentioning the do-release-upgrade command. This is useful when you want to upgrade a system from one release to another, for example, from Ubuntu 19.04 to Ubuntu 20.04. However, you must first perform a full upgrade by running the two aforementioned commands before invoking it.

That is, sudo apt upgrade followed by sudo apt dist-upgrade and lastly sudo apt do-release-upgrade.

4) Install a software package

To install a package on your system use the apt command as follows:

$ sudo apt install package-name

Suppose you want to install Neofetch, which is a utility for printing out system information, just run the command:

$ sudo apt install neofetch

Additionally, you can install multiple packages on one command by simply passing the packages one after the other as shown.

$ sudo apt install package1 package2 package3 ...

Another commonly asked question is what happens when you try install a package that is already installed? Well, if that happens, nothing bad happens expect that the package will be upgraded to its latest version if a new one is available in the repositories.

5) List installed packages

To have a glance at all the installed packages on your system, run the apt list command shown

$ sudo apt list --installed

To search for a specific command, pipe the results to grep followed by the package to be searched for.

$ sudo apt list --installed | grep apache
apt list package
apt list package

6) Search a package using APT

The apt search command allows you to search and check for the availability of a package in the Ubuntu/Debian repository. In the example below, we are searching for the availability of the Neofetch package in Ubuntu repositories.

$ sudo apt search neofetch
apt-search package name

7) Show information about a package using APT command

Before installing or removing a package, you can seek additional information about a package using the apt show command. For example, to display more information about the neofetch package.

$ sudo apt show neofetch

The output includes detailed information such as the package name, version, maintainer, installation size etc.

APT command on Linux
apt-show command

8) Remove unused packages after installation

Sometimes when installing a package, other dependencies and libraries that are required by the package are installed too. However after installation, these dependencies and libraries are not required any longer and just sit there taking up your disk space.

To remove these dependencies and files and free up some disk space , execute the command.

$ sudo apt  autoremove

When prompted just press ‘Y’ and hit ENTER.

9) Remove installed packages

You can remove a package that is already installed as shown.

$ sudo apt remove package-name

Alternatively, you can specify multiple package names on the same command to remove the packages simultaneoulsy.

$ sudo apt remove package1 package2 package3 ...

Getting rid of a package using the apt remove command leaves behind configuration files of the package. To completely delete the package alongside its configuration files use purge in the place of remove as indicated in the command below.

Conclusion

APT package manager is a useful tool for Debian/Ubuntu systems. It allows you to easily manage your packages using the various ways as highlighted in this guide on how to use the APT command on Linux. Let us know your thoughts.