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.
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 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
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.
To download package information from all configured sources, execute the command:
sudo apt update
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.
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
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
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
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.
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
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
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$
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
To list all packages, execute the command:
apt list
For example;
apt list | more
apt list | grep foo
apt list | grep php7-
For example for "nginx" and "php", execute the following command:
apt list nginx
apt list 'php7*'
To do this, execute the command:
apt list -a {pkgNameHere}
For example, run the command;
apt list -a sudo
The syntax or command to use is:
apt list --installed
apt list --installed | grep {pkgNameHere}
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
The command or syntax to use is shown below:
apt rdepends {pkgNames}
apt rdepends sudo
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
The command or syntax to use is shown below;
apt unhold {pkgName}
apt unhold sudo
Simply execute the following command or syntax as shown below;
sudo apt edit-sources
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
This article will show you the different ways to use apt commands with practical examples.