Server Enthusiast

How to get started with the apt command on Ubuntu/Debian Linux?

Created July 20, 2020

Introduction

If you are just getting started with Ubuntu or any other Debian based Linux distributions, it is more likely than not for you to have used or heard of the apt command.

APT stands for Advanced Package Tool. It is a command-line tool that interacts with the dpkg packaging system. APT is probably the most efficient way of managing software via the terminal for Ubuntu and any other Debian based Linux distributions.

One of the great things about apt is that the default repositories are usually kept quite up to date with the latest stable software packages. It also has great dependency management and ensures system stability.

Prerequisites

Before you get started you would need an Ubuntu or Debian based server.

I would recommend using DigitalOcean if you do not have an account you can use the following link to get a $100 free credit and spin up your own servers in seconds:

DigitalOcean $100 Free Credit

apt update

The apt update command is used to update the package database.

You should always run apt update before trying to install any new package, that way your system would know if there are any newer packages available.

Syntax:

sudo apt update

Output:

Hit:1 https://repos.insights.digitalocean.com/apt/do-agent main InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Get:3 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease [88.7 kB]                                
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                          
Get:5 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 494 kB in 1s (693 kB/s)                                                                 
Reading package lists... Done
Building dependency tree
Reading state information... Done
69 packages can be upgraded. Run 'apt list --upgradable' to see them.

apt upgrade

Unlike apt update, which only updates your local package database with the latest information from the repositories, the apt upgrade command actually upgrades your installed packages.

As you can see in the output above, there are 69 packages which can be upgraded on my Ubuntu server. If I were to run the apt list --upgradable command I would get a list of those packages. This would include things like some Python, PHP and even some system libraries upgrades:

python3-apport/bionic-updates 2.20.9-0ubuntu7.15 all [upgradable from: 2.20.9-0ubuntu7.14]
python3-apt/bionic-updates 1.6.5ubuntu0.3 amd64 [upgradable from: 1.6.5ubuntu0.2]
python3-distupgrade/bionic-updates 1:18.04.37 all [upgradable from: 1:18.04.34]
python3-gdbm/bionic-updates 3.6.9-1~18.04 amd64 [upgradable from: 3.6.8-1~18.04]
python3-problem-report/bionic-updates 2.20.9-0ubuntu7.15 all [upgradable from: 2.20.9-0ubuntu7.14]
python3-software-properties/bionic-updates 0.96.24.32.13 all [upgradable from: 0.96.24.32.11]
python3-update-manager/bionic-updates 1:18.04.11.13 all [upgradable from: 1:18.04.11.10]
software-properties-common/bionic-updates 0.96.24.32.13 all [upgradable from: 0.96.24.32.11]
sosreport/bionic-updates 3.9.1-1ubuntu0.18.04.2 amd64 [upgradable from: 3.6-1ubuntu0.18.04.3]
systemd/bionic-updates 237-3ubuntu10.41 amd64 [upgradable from: 237-3ubuntu10.38]

To actually upgrade those packages just use the following syntax:

sudo apt upgrade

You would get a list of the packages that would be upgraded and you will be asked if you want to continue:

69 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 37.3 MB of archives.
After this operation, 556 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Then just type y and hit enter. Depending on the size and the number of the packages it might take a while to complete.

If you want to skip the confirmation step, you could pass the -y argument:

sudo apt upgrade -y

You could even combine the two commands as follows:

sudo apt update && sudp apt upgrade -y

apt install

Installing a new package with apt is quite straight forward. For example, let's say that we wanted to install PHP on our Ubuntu server, all you need to run is the following:

sudo apt install php

You could also install multiple packages at once by providing a list of the packages that you want to install. For example, let's say that we wanted to install some additional PHP modules, rather than running the apt install command separately for each one, we could use the following:

sudo apt install php-mysql php-common php-mbstring php-xmlrpc

apt search

As you saw in the previous step, if you know the name of a package, installing it is quite straight forward. However, if you are unsure of the exact name of a package, you could use the apt search command to search for packages in the repository.

For example, let's say that you need to install MySQL on your server if you run sudo apt install mysql you would get back Unable to locate package mysql message. This is because there are 2 packages for MySQL, one for the MySQL server and one for the MySQL client, so to get the exact name of the package you could run:

sudo apt search mysql

You would get a list of a lot of packages and some information for each, but if you look closely you would see the ones that you need,

mysql-server/bionic-updates,bionic-security,now 5.7.30-0ubuntu0.18.04.1 all 
  MySQL database server (metapackage depending on the latest version)
...
mysql-client/bionic-updates,bionic-security,now 5.7.30-0ubuntu0.18.04.1 amd64
  MySQL database client binaries

So in order to install MySQL, you would need to run:

sudo apt install mysql-server mysql-client

apt remove

You can remove packages as quickly as you can install them. Let's say that we wanted to remove php, all you need to do is run the following command:

sudo apt remove php

One thing that you need to keep in mind is that apt remove would only remove the binary files and keep the configuration files on your system.

If you wanted to completely remove a package, you should then use the apt purge command.

apt purge

Similar to apt remove the apt purge command would uninstall a package from your system but it would also remove the configuration files associated with that package.

Syntax:

sudo apt purge php

apt autoremove

The apt autoremove is a very handy tool for cleaning your system from any packages and libraries that are no longer in use.

Syntax:

sudo apt autoremove

This is quite handy if you are running low on disk space and need to quickly free up some additional MBs.

What I usually like to do is to add the --purge flag:

sudo apt --purge autoremove

That way any configuration files for the packages will be removed as well.

Conclusion

APT is a great package manager which could make your life a lot easier.

It helps you quickly install stable and tested packages without having to compile anything from source.

Hope that this helps!