Read in 4 minutes

last updated 

How to use apt Command in Linux

apt is a command-line utility for installing, updating, removing and otherwise managing deb packages on Ubuntu, Debian, and related Linux distributions. It combines the most frequently used commands from the apt-get and apt-cache tools with different default values of some options.

apt is designed for interactive use. Prefer using apt-get and apt-cache in your shell scripts as they are backward compatible between the different versions and have more options and features.

Most of the apt commands must be run as a user with sudo privileges.

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

Updating package index (apt update)

The APT package index is basically a database that holds records of available packages from the repositories enabled in your system.

To update the package index run the command below. This will pull the latest changes from the APT repositories:

sudo apt update

Always update the package index before upgrading or installing new packages.

Upgrading packages (apt upgrade)

Regularly updating your Linux system is one of the most important aspects of overall system security.

To upgrade the installed packages to their latest versions run:

sudo apt upgrade

The command will not upgrade those packages that require removal of installed packages.

If you want to upgrade a single package, pass the package name:

sudo apt upgrade package_name

It is always a good idea to configure automatic security updates.

Full Upgrading (apt full-upgrade)

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

sudo apt full-upgrade

Be extra careful when using this command.

Installing packages (apt install)

Installing packages is as simple as running the following command:

sudo apt install package_name

If you want to install multiple packages specify them as a space-separated list:

sudo apt install package1 package2

To install local deb files provide the full path to file. Otherwise, the command will try to retrieve and install the package from the APT repositories.

sudo apt install /full/path/file.deb

Removing Packages (apt remove)

To remove an installed package type the following:

sudo apt remove package_name

You can also specify multiple packages, separated by spaces:

sudo apt remove package1 package2

The remove command will uninstall the given packages but it may leave some configuration files behind. If you want to remove the package including all configuration files use purge instead of remove :

sudo apt purge package_name

Remove Unused Packages (apt autoremove)

Whenever a new package that depends on other packages is installed on the system, the package dependencies will be installed too. When the package is removed the dependencies will stay on the system. This leftover packages are no longer used by anything else and can be removed.

To remove the unneeded dependencies use the following command:

sudo apt autoremove

Listing Packages (apt list)

The list command allows you to list the available, installed and upgradeable packages.

To list all available packages use the following command:

sudo apt list

The command will print a list of all packages including information about the versions and architecture of the package. To find out whether a specific package is installed you can filter the output with the grep command.

sudo apt list | grep package_name

To list only the installed packages type:

sudo apt list --installed

Getting a list of the upgradeable packages may be useful before actually upgrading the packages:

sudo apt list --upgradeable

This command allows you to search for a given package in the list of the available packages:

sudo apt search package_name

If found the command will return the packages which name matches the search term.

Package Information (apt show)

The information about the package dependencies, installation size, the package source and so on might be useful before removing or installing a new package.

To retrieve information about a given package, use the show command:

sudo apt show package_name

Conclusion

Knowing how to manage packages is an essential part of Linux system administration.

To learn more about the apt command open your terminal and type man apt.

Feel free to leave a comment if you have any questions.