APT Tutorial
This is a heavily oversimplified tutorial to the Advanced Package Tool, which is somewhat of a wrapper of dpkg, the Debian package manager. In this tutorial, I will explain the basics of how to use apt, including installing, downloading, updating, and uninstalling packages.
What’s a “package tool”?
A package tool is generally a piece of software that automates the process of installing, updating, and removing software. Usually, such a package tool is built as a better front-end, or wrapper, of the system’s package manager, a lower-level tool for managing the software on your machine. The package tool generally links to package repositories, including its own, and community-made ones. In the case of apt, it is generally quite a complicated process to get a package on their repository, so often times, developers opt to either make their own package repository, make an installation program to install the software themselves, or host a downloadable .deb file (see here) on a website.
What’s a “.deb file”?
A .deb file is an archive that contains a Debian package. It contains stuff like a list of dependencies and all the files for the package. We wont be going much further into .deb files for this tutorial, except for here, but this is how you generally install a .deb file (replace ~filename.deb` with the actual filename):
sudo dpkg -i filename.debHow to install a package with apt?
Now that we got the basics out of the way, we can jump right into apt’s main function: installing packages.
Now, before we can get into that, you must keep in mind that due to Debian’s main focus on stability, packages don’t often get updated on apt (with the exception of security-sensitive packages like web browsers), resulting in you probably having to compile the package yourself if you absolutely have to get the latest version of a certain package (though that is not recommended for beginner-level Linux “nerds”).
Now, let’s dive into installing a package with apt.
If you have ever used the CLI, or command line interface (more commonly known as the terminal, or terminal emulator), you might recognize the command structure of bash being the command name, followed by the “arguments”.
This is the command to install screenfetch, a popular tool for displaying system information in the terminal (remember to run sudo apt update before installing new packages):
sudo apt install screenfetchYou might notice you get prompted to confirm the install, just press Enter:
Do you want to continue? [Y/n]If you want to bypass this, use the
-yflag like this:sudo apt install screenfetch -y
Let’s dissect this command, we’ll start with sudo.
Adding “sudo” in front of any command will run it as root.
Root is what we call a superuser, a user with unlimited privileges, included in almost every Linux installation. It is possible that you may have gotten an error like this:
testuser is not in the sudoers file.In which case, your current user doesn’t have the right permissions to use the sudo command (there are many fixes available, here is one I found).
Next up is the actual apt command, very straightforward, it just calls the binary from /usr/bin/apt (if you are interested to find where your commands are located, run which <command>)
Now here comes the first argument, the “action”; install.
This is quite straightforward, as it just tells apt you want to install a package.
Last, but not least, is the package name, screenfetch, you can replace this with another package name (e.g., pacman) to install a different package.
Now test out your brand new command:
screenfetchIt should end up looking something like this:
tux@debian:~$ screenfetch
_,met$$$$$gg. tux@debian
,g$$$$$$$$$$$$$$$P. OS: Debian
,g$$P"" """Y$$.". Kernel: x86_64 Linux 6.6.99-09000-gd3ae1caecf39
,$$P' `$$$. Uptime: 2d 19h 42m
',$$P ,ggs. `$$b: Packages: 711
`d$$' ,$P"' . $$$ Shell: bash 5.2.15
$$P d$' , $$P Resolution: 1517x853
$$: $$. - ,d$$' WM: Sommelier
$$\; Y$b._ _,d$P' GTK Theme: CrosAdapta [GTK2/3]
Y$$. `.`"Y$$$$P"' Font: Roboto 10
`$$b "-.__ Disk: 7.7G / 17G (48%)
`Y$$ CPU: Intel Celeron N4500 @ 2x 1.114GHz
`Y$$. RAM: 98MiB / 2737MiB
`$$b.
`Y$$b.
`"Y$b._
`""""How to update a package?
Before we update any packages, keep in mind that update and upgrade are different things, as update will update your repositories, and upgrade will actually update your packages, more about update here.
So let’s say that apt finally updated your favorite package, screenfetch, and you want to try it out, how do you update it?
Updating a package is easy, but before we do that, it is advisable to first run sudo apt update to update the apt repositories on your machine.
Single Package
Here is the straightforward command to update screenfetch (you might get prompted to confirm, use the -y flag to bypass:
sudo apt install screenfetchDid you also notice something fishy? It’s the same command! Apt will automatically check for new updates if you run this command again.
System-wide
Of course you might find yourself needing to update all the packages at the same time (which you should do regularly), in which case this command is for you (again, you might get prompted to confirm, use the -y flag to bypass):
sudo apt upgradeNow you can see the differences, different action, and no package name. This command runs a system-wide update.
System-wide Full
Another important command when updating is this one (you might get prompted to confirm, use the -y flag to bypass):
sudo apt full-upgradeAs you can see, nearly the same, except this one can remove/install packages to resolve dependencies.
How to uninstall a package?
Now let’s say you got tired of screenfetch because people are laughing at your system resources, what you could do is configure screenfetch to not display those numbers, but the easy way is always better, so you decide to just uninstall screenfetch, but how?
There are two ways to “uninstall” a package, and none of them are uninstall.
Confusing, right?
remove
The first way is remove, like this (you will be prompted to confirm, use the -y flag to bypass):
sudo apt remove screenfetchThis is very similar to sudo apt install, except for the action.
This command will just simply uninstall screenfetch from your system, though there might still be some remaining files as evidence of screenfetch being there at some point.
purge
That’s where purge comes in, purge can find and remove these kinds of files to make sure you keep your system nice and clean.
The usage of purge will also feel very familiar (again, you will be prompted to confirm, use the -y flag to bypass):
sudo apt purge screenfetchAs you can see, pretty much same story with the command here; sudo, command, action, package name. Now try running screenfetch again, you will notice it has been deleted:
tux@debian:~$ screenfetch
-bash: /usr/bin/screenfetch: No such file or directoryHow to download a package?
I’ve found myself using this feature of apt very often, as it lets you just download the .deb file from the apt package repositories, letting you, for example, move that .deb file to a device without internet access and installing it (though you might need to deal with some dependencies).
The command is straightforward, and doesn’t need explanation, except it is recommended to run sudo apt update to get the latest versions:
sudo apt download screenfetchThis leaves you with the .deb file in the directory you are in:
tux@debian:~$ ls
screenfetch_3.9.1+20210523-2_all.debAutoremove is cool!
Autoremove is a cool action in apt that automatically finds packages that were dependencies of packages that you installed, that are no longer needed, and deletes them.
Like this (will ask to confirm, use -y flag to bypass):
sudo apt autoremoveNotice that there is no need to name a package, it just works. I usually use autoremove in a mix with update and full-upgrade for updating my system like this (tip, put this in a file like ‘update.sh’ and run it with the command ‘sh update.sh’):
sudo apt update
sudo apt full-upgrade -y
sudo apt autoremove -ySudo apt update?
This is simply put how you reload the package list on your local machine. When you run this, apt downloads the latest package list from all your repositories, which ensures your system knows all the latest versions and packages. Run it like this:
sudo apt updateAgain, no need for package name.
APT versus APT-GET
Apt is a newer tool, targeting usability, while apt-get is the older of the two, mainly used in scripting. With apt-get you would need to also use apt-cache for certain utilities like searching for repositories. In conclusion, it is better for you, a human, to use apt, and let the scripts use apt-get.
Even more commands?
Here are three more useful commands you might just find yourself every now and then:
search
apt search screen-fetchChecks if a package exists, and prints out the description.
show
apt show screenfetchShows information on a package.
list
apt list --installedShows a list of all the installed packages.
Conclusion
To wrap things up; apt, or advanced package tool, is a front-end for dpkg, offering many tools to manage system packages. I hope this tutorial helped you better understand apt, and made you able to use it in your day-to-day life.