Logically, just like any other computer, we need to install programs. In Linux-based systems, this task is usually done through a package manager. And, of course, Raspbian on Raspberry Pi is no exception.
If you are not used to installing software on Unix-based systems (like Linux), there are certain concepts related to software installation that may be shocking or confusing at first.
Therefore, before getting into the subject and learning how to install and update software with APT, let’s take a moment to explain these concepts briefly.
Packages, dependencies, and repositories
The usual installation system in Linux systems is to use a package manager. This may seem strange to us if we are used to installing through installers, as is the case with Windows.
As we know, an installer is an executable that installs (obvious, right?) a software. The installer is made by the software developer and can follow one of the many “standards” or none at all. In general, the installer is only responsible for the integrity of the installed software.
In the case of Linux, the software installation mechanism is integrated into the operating system. This centralized system uses a single local database, and verifies, manages, and maintains the integrity of all the system’s packages.
We will see that we are constantly talking about ‘packages’. In summary, a package is a file that contains the necessary structure to install, update, and uninstall a certain software conveniently and safely.
Each of the packages may have dependencies, which are other software or packages that it requires to function correctly. For example, this includes components and libraries, and usually in a specific version (or a range of them).
Managing packages and dependencies manually would require a lot of work. For this reason, packages are designed to work within a package system. Of course, as almost always in Linux, there is more than one package system.
In general, each Linux distribution defaults to a package system. Thus, we have DPKG for Debian-based systems (Ubuntu, Raspbian), RPM for RedHat-based systems (Fedora, CentOS), or PACMAN for ArchLinux-based systems, among others.
On the other hand, we have package managers. These are programs that simplify the user’s interaction with a package system. Thus, for DKPG (Debian and derivatives) we have APT, for RPM we have YAST (SuSe), YUM (Fedora and CentOS), or URPMI (Mandriva) among many others.
Where do we get the packages from? This is where the repositories come in. Simply put, repositories are servers designed to host packages, for one or many softwares.
The “main” repositories are maintained by the developers of each Linux distribution, and the community that supports them. But there are also other “additional” repositories created and maintained by the developers of certain software.
Finally, it should be noted that we should only use trusted repositories. Otherwise, we expose ourselves to installing malware on our system.
The APT manager
And finally, we get to APT. As we have said, Debian-based systems (like Raspbian) use the DPGK package system for software installation and updates. The extension of these packages is ‘.deb’.
For its part, APT (Advanced Packaging Tool) is the default package system manager used in Debian-based systems. APT simplifies the use of the DPGK system for the user.
APT is made up of a series of utilities and libraries developed in C and bash. Among them, the most used is APT-GET and, to a lesser extent, APT-CACHE and APT-FILE.
Now let’s go through some of the most used functions of the APT package manager.
How to use APT
Installing packages with APT is very simple. We just have to execute the ‘install’ command from the command line
sudo apt-get install package
Where “package” is the name of the package we want to install. In most cases, we will obtain the name of the package from the developer’s documentation, which will provide the command to execute for the installation.
Before installing a package and its dependencies, APT asks for confirmation and we must respond by pressing ‘y’ to install. This is common for most APT commands. If we want to avoid being asked, we can add the ‘-y’ parameter as follows:
sudo apt-get -y install package
As we install packages, the free space on our SD card will decrease. To check the available space, we can use the ‘df’ command:
Search for packages
It is also possible to search for a package by its name. This search is done against the local cache stored by APT. Therefore, to perform the search, we use the command:
sudo apt-cache search package
APT will show us a list of all the packages that contain the word we searched for.
To remove a previously installed package from the system, we have two options. The most “conservative” is the ‘remove’ command, which removes the package but not its configuration files or dependencies.
sudo apt-get remove package
To completely remove a package, including configuration files and dependencies, we can add the ‘—purge’ parameter or use the ‘purge’ command directly. Therefore, the following two commands are equivalent:
sudo apt-get purge package
sudo apt-get remove --purge package
One of the operations for which we will use APT most frequently is to update the installed packages. Updating our software is important both to obtain new features and to fix possible security vulnerabilities.
In APT, the update process is done in two steps. On one hand, the ‘update’ command updates the local package cache. It looks for new changes, comparing with the information contained in the repositories, but does not download or install any packages.
sudo apt-get update
To actually install the updated packages, which we have previously determined with the ‘update’ command, we use the ‘upgrade’ command.
sudo apt-get upgrade
These two commands are usually used together, one after the other. If we want to run them directly with a single command, we can use:
sudo apt-get -y update && sudo -y apt-get upgrade
Clean APT system
As time goes by and we update the software, it will happen that certain files become obsolete. To avoid unnecessary space losses, it is advisable to perform certain maintenance tasks periodically.
First, we have the ‘autoremove’ command. This uninstalls the packages that were installed as dependencies of other packages, but that are currently no longer required (for example, because the new version of the package no longer uses them). Therefore, these packages are no longer needed and can be uninstalled.
sudo apt-get -y autoremove
On the other hand, it also happens that the information stored in the local cache becomes obsolete. These files are stored in /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.
To prevent it from growing unnecessarily, we have more maintenance tools. It should be noted that they do not uninstall installed software, but only clean the APT local cache files.
Thus, to clean the local cache file we have the ‘autoclean’ command. This removes the downloaded packages that currently cannot be downloaded from the repositories. This means that these packages are obsolete and can be deleted.
sudo apt-get autoclean
Finally, we have the ‘clean’ command that removes all the packages we have downloaded from the local cache. It is used less than the previous one because it is more ‘aggressive’.
sudo apt-get clean
Frequently, the ‘autoremove’ and ‘autoclean’ commands are used together, which we can combine into a single command:
sudo apt-get -y autoremove && sudo apt-get autoclean
Add a repository
As we have said, APT looks for packages in repositories. The addresses of the repositories are configured in lists saved on our computer. In Raspbian, these lists are saved in /etc/apt/sources.list and /etc/apt/sources.list.d/raspi.list
We can add or remove repositories by editing these lists. Or, if we simply want to add a repository, we can use the following command directly:
sudo sh -c 'echo repositoryURL' >> /etc/apt/sources.list
Of course, we should only add secure repositories, such as those of the developers of our distribution or those of trusted software. Otherwise, we run a great risk of installing malware on our system.
So far, the tutorial for installing, uninstalling, and updating software with APT in Raspbian. In the next Raspberry Pi tutorial entry, we will see how to update the Raspian system itself when there is a new version, for which we will use (guess what!) the APT package manager.