What is APT?
APT stands for Advanced Packaging Tool – it is a collection of tools that help with package management. Software written for Linux and available to the end-user via repository is called a package. It does not matter if you are downloading a shared library that is used by many programs or a stand-alone program. It all comes in form of a package. Apt is what helps you download and install that software.
How a package is made available to the end-user?
The process of adding software to repository looks like this
- Software is written and released on an open source platform
- Software is packaged for a particular distribution
- Package with software is added to that distribution’s repository
- Package is ready for installation via repository
It is a bit more complex than the above process, but more or less this is how the cycle looks like. Once a particular package gets added to repository, you can simply download the software.
But where all those packages actually are, where is the physical data? There are many institutions that are happy to lend their servers and host those resources. Just take a look at the list of Debian mirrors. All those servers host software packages, and if one of them is down, apt simply uses a different server to grab data.
A treasure map to software – sources list
In order to know where those servers are, apt uses a file called sources.list. Think of it as a treasure map, but instead of treasure you get software.
The file can be found in the following location
/etc/apt/sources.list
Unless you know what you are doing, you should not modify this file.
You can read more about sources list on Debian wiki – here. However, for now it is enough that you know from where you get the software.
Information vs action
apt can be used with or without administrative privileges. If you want to use apt in order to obtain information, you do not need root access or sudo. However, in order to modify anything, you need to be logged as root or prefix the command with sudo.
Commands below will not be prefixed with sudo. Any command that modifies files will need to be executed as root or with sudo.
Using apt to manage packages
The syntax of apt is quite simple, take a look
apt [option1] [option2]
Using apt on its own prints a brief description and a collection of most commonly used options
- update – update list of available packages
- upgrade – upgrade the system by installing / upgrading packages
- autoremove – remove automatically all unused packages
- install [option] – install packages
- remove [option] – remove packages
- purge [option] – completely remove package
- reinstall [option] – reinstall packages
Updating your system with apt
One of the most common uses of apt is
apt update
This command simply updates your local software database. If there are newer versions of currently installed packages then apt will print out a message informing you about it. You can check what can be upgraded with
apt list --upgradable
apt will then print out a nice list of what can be upgraded. If you want to upgrade those packages, it is as simple as issuing
apt upgrade
The process of updating the repository index and upgrading all packages should be performed as the first step after setting up any system.
Installing a package
In order to install a package you can simply issue the following command
apt install $some_software
Provided you have internet access, $some_software will be installed.
Usually there are many other packages installed as well, those are called dependencies. In order to minimize the amount of space that a particular program takes, it is common to use shared libraries.
If for example you are using two video players it would be unwise to install two sets of codecs, one set is enough and both players can make use of them. This is a rudimentary example, but this is the philosophy of dependencies.
Uninstalling a package
If you want to remove a package from your machine it is as easy as
apt remove $some_software
This, however, removes only the specified package. Furthermore, configuration files created by that package are left as they are, and no dependencies are being removed. This is done in case you decide to reinstall the package later or not to break other packages using dependencies.
Purging a package
Purge works identical to remove, however, it removes configuration files as well
apt purge $some_package
Purge is useful if you are confident you will no longer use certain package, or if you want to remove everything and get a fresh configuration files.
Removing orphaned dependencies
Removing a package often leaves dependencies that we installed alongside the main package. If that package was the only one using those dependencies they will be orphaned and are no longer needed.
By default apt does not remove dependencies alongside the main package since they might be used by other packages. This is done in order to prevent a package from being unusable.
However, if a dependency is no longer needed by any package it is then safe to remove it.
This is done by issuing the following command
apt autoremove
Note! After performing an update, apt will alert the user that there are dependencies that are no longer needed and can be safely removed.
Reinstalling a package
If for some reason a package is not behaving as intended, you can simply reinstall that package. It is uncommon, but happens.
To reinstall a package issue the following command
apt reinstall $some_package
apt vs apt-get
Whenever tutorials tell you to install software, they are either using apt or apt-get, but what is the difference? In essence, for the end user there is none whatsoever.
apt is a high-level interface for managing packages, which means it is just easier to use and does not have more specialized functions that apt-get has. If you are just installing and removing software apt and apt-get can be used interchangeably.
Conclusion
Using apt is quite simple, however, it is vital to know how to properly install and remove software from your server. After all, without software your server will be quite useless.
On the other hand, if you no longer need a certain package it is best to remove it. Any software that you introduce on your server is a potential security risk. You might not be aware of a proverbial “hole” in a particular program, which can be exploited by malicious parties. To mitigate such risk simply uninstall everything you no longer need. If not for security reasons alone, which should be reason enough, then for the sake of disk space as well.