In this article I discuss how to use the modern Debian based Advanced Package Tool known on the command line interface as apt. To aid the discussion I provide examples of running several of the common package management commands on an Ubuntu 18 LTS server with explanations of what each command is doing.
To start the discussion of software package management for the Debian based Ubuntu operating system there is a conversation to be had about two similarly named command line utilities that are widely scattered across tutorials on the web. I'm talking about apt-get and apt.
Both utilities are designed to handle similar package management operations with apt being newer and arguably more user friendly. In this article I will be using the recommended apt utility but, as it turns out for most of the commands I show you can actually substitute apt with apt-get and obtain the same results.
One of the first things one should do, at the very least just for curiousity sake, is to figure out what software packages are available on the system. You can do this using apt list and optionally filtering the output by passing arguments of --installed and --upgradable. If you omit any flags you will get all available packages (installed or not).
List all packages in the packages index.
# apt list Listing... 0ad/bionic 0.0.22-4 amd64 0ad-data/bionic 0.0.22-1 all 0ad-data-common/bionic 0.0.22-1 all 0install/bionic 2.12.3-1 amd64 0install-core/bionic 2.12.3-1 amd64 0xffff/bionic 0.7-2 amd64 2048-qt/bionic 0.1.6-1build1 amd64 2ping/bionic-updates 4.1-1ubuntu1 all 2to3/bionic-updates 3.6.7-1~18.04 all 2vcard/bionic 0.6-1 all 3270-common/bionic 3.6ga4-3 amd64 ...
List only packages that are installed.
# apt list --installed Listing... accountsservice/bionic,now 0.6.45-1ubuntu1 amd64 [installed,automatic] acl/bionic,now 2.2.52-3build1 amd64 [installed,automatic] acpid/bionic,now 1:2.0.28-1ubuntu1 amd64 [installed,automatic] adduser/bionic,now 3.116ubuntu1 all [installed,automatic] apparmor/bionic-updates,bionic-security,now 2.12-4ubuntu5.1 amd64 [installed,automatic] apport/bionic-updates,bionic-security,now 2.20.9-0ubuntu7.7 all [installed,automatic] apport-symptoms/bionic,now 0.20 all [installed,automatic] apt/bionic-updates,now 1.6.12 amd64 [installed,automatic] apt-utils/bionic-updates,now 1.6.12 amd64 [installed,automatic] ...
Note the words now and [installed] in the lines of output which are the key pieces of information to indicate if something is installed or not. For example, if I want to check to see if a particular package, the acl package, is installed I can do the following.
# apt list acl acl/bionic,now 2.2.52-3build1 amd64 [installed,automatic]
List packages available for upgrades.
# apt list --upgradable
Cloud computing has brought the ability to spin up inexpensive Linux servers to anyone with a credit card and a SSH enabled terminal. This is especially true for folks like me, Software Developers, but like it or not, in that moment I am in the position of a system admin (shriek!). Luckily I had a much more experienced mentor early on get me started in the right direction with a lot of this sysadmin stuff. One key peice of advise was, "always begin by updating your new server's software". What I didn't fully understand when I was first told of the importance of updating the server's software packages is that there is an important difference between updating and upgrading them.
Updating software packages in Ubuntu means simply that the list of available packages get updated meaning that newer versions are included. This does not mean that the newer versions get installed over any existing ones. Updating is a precursor to what comes next which is to upgrade which is the process of installing newer versions of the packages as described in this StackOverflow post.
First update the packages list.
# apt update
If you want to know what packages are now available to be upgraded use the previously discussed list command.
# apt list --upgradable
Then follow this with upgrading already installed packages to the newer versions brought in with the previous update command.
# apt upgrade
You use the install sub command with apt to install a package. For example, if I wanted to install the nginx package I can first check to see if it is included in the list of available packages like so.
# apt list nginx Listing... Done nginx/bionic-updates,bionic-security 1.14.0-0ubuntu1.6 all
Note the absence of [installed] indicating that it is available but not installed so, I can install it with the following.
# apt install nginx Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: grub-pc-bin Use 'apt autoremove' to remove it. The following additional packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core Suggested packages: libgd-tools fcgiwrap nginx-doc ssl-cert The following NEW packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx nginx-common nginx-core 0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded. Need to get 2461 kB of archives. After this operation, 8210 kB of additional disk space will be used. Do you want to continue? [Y/n] y ...
To search for a term within a package name or description I can use the search sub command with apt. For example, if I want to search for mentions of the database package postgres I do so like this.
# apt search postgres Sorting... Full Text Search... akonadi-backend-postgresql/bionic 4:17.12.3-0ubuntu3 all PostgreSQL storage backend for Akonadi algol68g/bionic 2.8-2build1 amd64 Implementation of Algol 68 as defined by the Revised Report aolserver4-nspostgres/bionic 4.5+20110709-2 amd64 AOLserver 4 module: Postgres connector apgdiff/bionic 2.5.0~alpha.2-75-gcaaaed9-1 all Another PostgreSQL Diff Tool autopostgresqlbackup/bionic 1.0-7 all Automated tool to make periodic backups of PostgreSQL databases ...
To look at software package information you use the show sub command in conjunction with apt so, if I want to look up the information for the earlier installed nginx package that is done like so.
# apt show nginx Package: nginx Version: 1.14.0-0ubuntu1.6 Priority: optional Section: web Origin: Ubuntu Maintainer: Ubuntu Developers <firstname.lastname@example.org> Original-Maintainer: Debian Nginx Maintainers <email@example.com> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 44.0 kB Depends: nginx-core (<< 1.14.0-0ubuntu1.6.1~) | nginx-full (<< 1.14.0-0ubuntu1.6.1~) | nginx-light (<< 1.14.0-0ubuntu1.6.1~) | nginx-extras (<< 1.14.0-0ubuntu1.6.1~), nginx-core (>= 1.14.0-0ubuntu1.6) | nginx-full (>= 1.14.0-0ubuntu1.6) | nginx-light (>= 1.14.0-0ubuntu1.6) | nginx-extras (>= 1.14.0-0ubuntu1.6) Homepage: http://nginx.net Supported: 5y Download-Size: 3596 B APT-Manual-Installed: yes APT-Sources: http://mirrors.digitalocean.com/ubuntu bionic-updates/main amd64 Packages Description: small, powerful, scalable web/proxy server Nginx ("engine X") is a high-performance web and reverse proxy server created by Igor Sysoev. It can be used both as a standalone web server and as a proxy to reduce the load on back-end HTTP or mail servers. . This is a dependency package to install either nginx-full (by default), nginx-light or nginx-extras. N: There is 1 additional record. Please use the '-a' switch to see it
To uninstall a package you use the remove sub command so, if I wanted to remove the nginx package I do so as follows.
# apt remove nginx Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: fontconfig-config fonts-dejavu-core grub-pc-bin libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core Use 'apt autoremove' to remove them. The following packages will be REMOVED: nginx 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 44.0 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 90784 files and directories currently installed.) Removing nginx (1.14.0-0ubuntu1.6) ...
In this article I have demonstrated how to use the apt command for common software package management tasks as well as explained when and why you might use each one.