Need to use apt command line utility for package management on Ubuntu Linux LTS and Debian Linux server?

This guide will help you.

APT is a short form for "Advanced Package Tool". It is basically a set of core tools inside Debian and Ubuntu Linux system.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to Manage their Ubuntu and Debian Linux Servers.
In this context, we shall learn apt command examples for new Linux users.

More about APT command and how to use it in Ubuntu and Debian Servers?

APT command is very useful to install apps, delete or remove apps, keep Ubuntu/Debian server up to date and so on. Apt work with dpkg command to install and update the system.
The apt-get command is the first front-end tool. apt is a second tool which overcomes some design mistakes of apt-get command. Hence you should use the apt command. This command provides nicer interface including progress bars and colors at the CLI. It provides the same functionality as the specialized APT tools, like apt-get and apt-cache, but enables options more suitable for interactive use by default.

Apt command examples and how to use it?

APT can be used for installing, upgrading, configuring, and removing apps/programs for a Debian or Ubuntu operating system.

Its Syntax is shown below;

apt [options] command
apt [options] command pkg1
apt [options] command pkg1 pkg2

apt command examples

Now, Let us see how to use the apt command to install security updates or new set of packages on Ubuntu or Debian Linux server.

How to fetch updates on Ubuntu?

To download package information from all configured sources, execute the command:

sudo apt update

How to apply package and security updates

To upgrade all packages currently installed on the system, execute:

sudo apt upgrade

Note that new packages will be installed if required to satisfy dependencies, but existing packages will never be removed.

More about applying package and security updates?

You can execute the following commands to apply all security and package updates;

sudo apt update && sudo apt upgrade

To see the list of packages that can be upgraded on the system, execute:

apt list --upgradable

How to perform full system upgrade on Debian Server?

The full-upgrade command performs the function of upgrade but will remove currently installed packages if this is needed to upgrade the system as a whole. This is useful when packages are kept back from updates or you want to install from Ubuntu version 16.04 to 16.04.1. To do this, execute;

sudo apt full-upgrade

How to install new packages on Ubuntu Server?

To install a new package called nginx, execute;

sudo apt install {pkgNameHere}

For example, lets say you want to install a package by name "nginx", then execute;

sudo apt install nginx

How to remove packages on debian Server?

To delete or remove a package such as "nginx", execute the following command:

$ sudo apt remove {pkgNameHere}

This will delete all files except configuration files on the system.

How to remove packages with the purge option on debian Server?

To remove both package and config files, you can use the purge option.
Removing a package removes all packaged data, but leaves usually small (modified) user configuration files behind, in case the remove was an accident. Just issuing an installation request for the accidentally removed package will restore its function as before in that case. On the other hand you can get rid of these leftovers by calling purge even on already removed packages. To do this, run the following command;

sudo apt purge {pkgNameHere}

For example, to remove a package named Nginx with the purge option, execute;

$ sudo apt purge nginx
$ sudo apt purge nginx nginx-core nginx-common

How to remove packages on debian Server with the autoremove option?

The autoremove option is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime. For example, when you upgrade Linux kernel to 4.1.5, you may not need Linux kernel version 3.8.5. The command to use is given below:

sudo apt autoremove
sudo apt --purge autoremove

How to search packages on Ubuntu Servers?

The search option can be used to search for the given regex. To search for php packages, simply run the following commands:

apt search php

For example;

apt search mysql-5.?
apt search mysql-server-5.?
apt search httpd*
apt search ^apache
apt search ^nginx
apt search ^nginx$

How to find info about packages on Debian Servers?

To show or see information about the given package(s) including its dependencies, installation and download size, sources the package is available from, the description of the packages content and much more. Execute the commands;

apt show {pkgNamehere}

For example;

apt show nginx
apt show sudo

How to List packages on Ubuntu Server?

To list all packages, execute the command:
apt list
For example;

apt list | more
apt list | grep foo
apt list | grep php7-

How to display a list of packages satisfying certain criteria on Ubuntu server?

For example for "nginx" and "php", execute the following command:

apt list nginx
apt list 'php7*'

How to See policy of apt package on Debian Servers?

To do this, execute the command:

apt list -a {pkgNameHere}

For example, run the command;

apt list -a sudo

How to list all installed packages on Ubuntu Server?

The syntax or command to use is:

apt list --installed
apt list --installed | grep {pkgNameHere}

How to list package dependency on Debian Server?

The depends option shows a listing of each dependency a package has and all the possible other packages that can fulfill that dependency. For example:

apt depends {pkgNameHere}
apt depends sudo

How to performs recursive dependency listings similar to apt-cache on Ubuntu Server?

The command or syntax to use is shown below:

apt rdepends {pkgNames}
apt rdepends sudo

How to hold a package On Ubuntu Linux?

Package holding means it can not be upgraded till you run unhold on it again. The command or syntax to use is:

apt hold {pkgName}
apt hold sudo

How to unhold a package on Ubuntu Server?

The command or syntax to use is shown below;

apt unhold {pkgName}
apt unhold sudo

How to modify the source information file on Ubuntu for instance in "/etc/apt/sources.list"?

Simply execute the following command or syntax as shown below;

sudo apt edit-sources

List of apt command options

From the apt(8) command man page, you can see the options such as:
list - list packages based on package names
search - search in package descriptions
show - show package details
install - install packages
remove - remove packages
autoremove - Remove automatically all unused packages
update - update list of available packages
upgrade - upgrade the system by installing/upgrading packages
full-upgrade - upgrade the system by removing/installing/upgrading packages
edit-sources - edit the source information file

[Need to install any Software on Ubuntu Server? We are available to help you today.]