apt-things tutorial: using apt on Linux

As a proud owner of Linux server, even a small home Linux server runing on Cubieboard or Raspberry, you need to have a basic knowledge of package management. For a given software package, or simply a keyword, you need to be able to:

  • update the list of packages and upgrade your server
  • find out is it installed on your Linux server
  • find out is it available for you to install it
  • install a package
  • uninstall a package
  • list files that an installed package contains
  • list files that a not installed package will install

In this text we will use not one but three packages to practice with, since the above tasks can be performed on a list of packages/keywords, which saves typing and time. So, our three guinea pigs are:

  • apache, web server,
  • php, scripting language which runs either standalone or inside a web server,
  • mysql, database engine which works very well with the above two.

With the above three components installed on your server you can, for example, install and run a WordPress blog on your Linux server.

Update the list of packages and upgrade your server

Prior to performing actions related to package installation/uninstallation, you should execute

  • apt-get update to refresh the package metadata, and
  • apt-get upgrade to install the latest versions of installed packages.

It is important to do it, since you want to install new packages on top of a fresh system, not outdated.

Are things installed on my server?

A simple command

apt list --installed | grep 'apache\|php\|mysql' | grep '\[installed\]' | cut -d '/' -f 1

or (broken into multiple lines)

apt list --installed \
  | grep 'apache\|php\|mysql' \
  | grep '\[installed\]' \
  | cut -d '/' -f 1

will tell us which of our components are installed, for example

apache2
mysql-server
php5

if we have all three, or a subset of the above.

This was the most complex way to ask, and it provides the shortest answer. If we opt out cut -d '/' -f 1, i.e. “give me only the first of several ‘/’-delimited fields”, we will get a bit more information:

apt list --installed \
  | grep 'apache\|php\|mysql' \
  | grep '\[installed\]'

would yield

apache2/oldstable,oldstable,now 2.4.10-10+deb8u11 armhf [installed]
mysql-server/oldstable,oldstable,now 5.5.58-0+deb8u1 all [installed]
php5/oldstable,oldstable,now 5.6.30+dfsg-0+deb8u1 all [installed]

Here we see the versions and some additional information about packages. Next step would be to remove the [installed] filter:

apt list --installed \
  | grep 'apache\|php\|mysql'

displays quiet a lot:

apache2/oldstable,oldstable,now 2.4.10-10+deb8u11 armhf [installed]
apache2-bin/oldstable,oldstable,now 2.4.10-10+deb8u11 armhf [installed,automatic]
apache2-data/oldstable,oldstable,now 2.4.10-10+deb8u11 all [installed,automatic]
apache2-utils/oldstable,oldstable,now 2.4.10-10+deb8u11 armhf [installed,automatic]
libapache2-mod-php5/oldstable,oldstable,now 5.6.30+dfsg-0+deb8u1 armhf [installed,automatic]
libdbd-mysql-perl/oldstable,oldstable,now 4.028-2+deb8u2 armhf [installed,automatic]
libmysqlclient18/oldstable,oldstable,now 5.5.58-0+deb8u1 armhf [installed,automatic]
mysql-client-5.5/oldstable,oldstable,now 5.5.58-0+deb8u1 armhf [installed,automatic]
mysql-common/oldstable,oldstable,now 5.5.58-0+deb8u1 all [installed,automatic]
mysql-server/oldstable,oldstable,now 5.5.58-0+deb8u1 all [installed]
mysql-server-5.5/oldstable,oldstable,now 5.5.58-0+deb8u1 armhf [installed,automatic]
mysql-server-core-5.5/oldstable,oldstable,now 5.5.58-0+deb8u1 armhf [installed,automatic]
php5/oldstable,oldstable,now 5.6.30+dfsg-0+deb8u1 all [installed]
php5-cli/oldstable,oldstable,now 5.6.30+dfsg-0+deb8u1 armhf [installed,automatic]
php5-common/oldstable,oldstable,now 5.6.30+dfsg-0+deb8u1 armhf [installed,automatic]
php5-json/oldstable,now 1.3.6-1 armhf [installed,automatic]

which gives us the list of all packages that have one of our keywords in the description. While this information may be usefull, we should focus our attention only to [installed], since they are basic packages that we install, others were added as dependencies.

Are things available to install?

If a package is not installed on your system, you would have to know is it available, and what is its exact name. It is not as simple as in the previous section, so we will go one package at a time, not all three together.

On default Armbian installation, as on almost all others, you will have apache2 already installed, so we will here use php and mysql to practice with.

If you already know the names of the packages, which will most ofthen be the case, you simply ask:

apt-cache -q showpkg php5 mysql-server

and get the output which is several pages long, very detailed and difficult to read. It is useful, though, but you are, most probably, not keed to use it, so you may type the following two commands instead:

apt-cache -q showpkg php5 | head -1; \
apt-cache -q showpkg mysql-server | head -1

and get the following

Package: php5
Package: mysql-server

which means that you will be able to install the above packages.

If you don’t know the exact names of the packages, you have to list the available options and decide which one is yours:

apt-cache search -n '^mysql'

will produce some 15 to 20 lines of output, some of which would be

mysql-client - mysql database client (metapackage depending on the latest version)
mysql-client-5.5 - mysql database client binaries
mysql-common - mysql database common files, e.g. /etc/mysql/my.cnf
mysql-server - mysql database server (metapackage depending on the latest version)
mysql-server-5.5 - mysql database server binaries and system database setup
mysql-server-core-5.5 - mysql database server binaries

with the underlined one as proper choice. You choose the most general name, preferably with metapackage in the description. You don’t choose the ones with version numbers since they are not generic, and they will be replaced one day with higher versions.

Similarly, for

apt-cache search -n '^php'

you get over 400 lines, and before you try to locate the right one by hand, you try

apt-cache search -n '^php' | grep metapackage

and luckily you get only one:

php5 - server-side, HTML-embedded scripting language (metapackage)

If the second try does not return a proper package name – you must dig it from the first one by hand. You may also ask internet for a proper package name.

If you can not locate your package with apt-cache, you may have to search the net for “add apt repository” for the solution.

Installing packages

This one is simple, provided that you know the names of packages to be installed:

apt-get install php5 mysql-server

If the packages are already installed on your system – no harm will be done, they may eventually be updated. Otherwise they will be installed.

Along with the packages you selected for installation, their dependencies will be installed automatically, and you may be asked to confirm this before installation begins.

During install, you may have to answer some questions, for example “root password for mysql server” and similar. Please read the questions carefully, and remember what you answered, since you will need it later on.

Uninstalling packages

You uninstall with apt-get purge, for example:

apt-get purge php5 mysql-server

and follow the prompts. You may be reminded to execute apt-get autoremove if needed, to remove packages that are not used by the system any more. Be free to do so.

List files that an installed package contains

If you are interrested where some package installed itself, and which files it generated, you do it with apt-file -F list command. This command is not installed on Linux distributions by default, so you may have to

apt-get install apt-file
apt-file update

to be able to use it.
Be warned that some packages may contain large number of files, so we recommend to count them first. For example:

apt-file -F list apache2 | wc

will tell that there are slightly more than 180 files. You may want to pipe the list to less for pagination and scrolling:

apt-file -F list apache2 | less -S

This list of files may be useful if you don’t know much about the package. For example, the files that were installed in /etc should be configuration files for the package.

Here is the file list for package apt-file, obtained with apt-file -F list apt-file

apt-file: /etc/apt/apt-file.conf
apt-file: /etc/bash_completion.d/apt-file
apt-file: /usr/bin/apt-file
apt-file: /usr/bin/diffindex-download
apt-file: /usr/bin/diffindex-rred
apt-file: /usr/share/apt-file/apt-file-update.update-notifier
apt-file: /usr/share/apt-file/do-apt-file-update
apt-file: /usr/share/apt-file/is-cache-empty
apt-file: /usr/share/doc/apt-file/README
apt-file: /usr/share/doc/apt-file/changelog.gz
apt-file: /usr/share/doc/apt-file/copyright
apt-file: /usr/share/man/man1/apt-file.1.gz
apt-file: /usr/share/man/man1/diffindex-download.1.gz
apt-file: /usr/share/man/man1/diffindex-rred.1.gz

One interresting way to use this list is to make your own copy of the files, if you want to keep them safe. Since you are working on Linux, you do it with one-line script in your terminal:

apt-file -F list apt-file | cut -d ' ' -f 2 | tar cfz ~/backup-apt-file.tgz -T -

which means: take the package file list, remove the package name from the beginning of each line, and feed the result to tar utility to archive these files.

With this you get the file backup-apt-file.tgz in your home directory. To check if the archive is OK, and what’s in it, execute:

file ~/backup-apt-file.tgz
tar ztvf ~/backup-apt-file.tgz

First command should tell that it is gzip compressed data, the second should list the contents of the archive.

List files that a not installed package will install

There is nothing here to be said here, except that apt-file -F list works for both installed and not (yet) installed packages.

To fill up this section with some content, here is an example: package samba is not installed (yet) on my Cubie. I executed

apt list --installed | grep samba

and got no results, i.e. package was not installed, but

apt-file -F list samba | wc

said that the file list contains 172 lines, and

apt-file -F list samba | head -5

returned first five of them:

samba: /etc/cron.daily/samba
samba: /etc/init.d/nmbd
samba: /etc/init.d/samba
samba: /etc/init.d/samba-ad-dc
samba: /etc/init.d/smbd

so we conclude it works.