Art, Programming, Linux

Andrew's notes

Home Posts Tags

Essential APT Commands: A Quick Reference

Looking for a handy reference guide to navigate APT commands efficiently? This article provides an overview of essential APT commands, enabling you to install, update, remove, and troubleshoot software packages on your Linux system.

Published on February 14, 2024 834 words 4 min read Linux

Image by Jiawei Zhao

Contents

The well known APT or Advanced Package Tool is a commandline package manager used in Debian based linux distributions. It provides commands for searching, managing and querying information about packages. In this article we will illustrate its basic commands and options.

Installing Packages #

The most basic functionality of any package manager is to be able to install packages. Apt allows for the installation of single or multiple packages as well as .deb files. To install a single package with apt we use the following structure.

sudo apt install packageName

Sometimes we want to install a specific version of a package:

sudo apt install apache2=2.4.55-1ubuntu2

To install multiple packages we simply add another package name to our command.

sudo apt install packageName1 packageName2

If you have already downloaded a .deb package locally and you wish to install it you simply provide apt with the package’s location on your system instead of its package name.

sudo apt install /home/andrew/Downloads/obsidian_1.5.3_amd64.deb

To reinstall a package use the reinstall option.

sudo apt reinstall packageName

To view all packages available to the system use list. To view all installed packages you use list --installed.

sudo apt list
sudo apt list --installed

To see if a package is installed or not with apt you can copy the following command. The -qq flag is an option to query the following package. Using -q returns the same results. Alternatively you can use grep to filter through the results of apt list --installed via pipe.

sudo apt -qq list packageName
sudo apt list --installed | grep packageName

You can search for a package in the repositories using the search option and to view the details of a package you use the show option.

sudo apt search packageName
sudo apt show packageName

To view all the dependencies of a package:

sudo apt depends packageName

To locate an installed package type the following command. The -L flag is the option that lists all directories associated with the package.

sudo dpkg -L packageName

Which package provides a file ? #

Sometimes we are trying to install a package only to be met with some errors about some missing files. We can search apt for those files in order to find out which package provides them and install them. To do so we need the apt-file package.

apt-file search fileName

Updating Packages #

The update option of the apt tool allows us looks for updates for all our installed packages and returns a list with all packages that can be updated. We can then run the upgrade option to upgrade some or all. The upgrade command by default upgrades all packages except for those that create a problem with their dependencies. To upgrade these packages you need to use dist-upgrade instead of upgrade.

sudo apt update
sudo apt upgrade

To upgrade just a specific package you use the following syntax:

sudo apt upgrade packageName

Pinning Packages #

It is possible to “pin” packages to specific versions so that they do not update even if a new version becomes available. To do so you need to create a file in /etc/apt/preferences.d/ and specify three things - the package, the version, and the pin priority. In this case we’ll call the file repository-priority and use nano to edit the files.

nano /etc/apt/preferences.d/repository-priority 

Add the following lines:

Package: example-package 

Pin: version 1.2.3 

Pin-Priority: 1001

NOTE: Pin priority is a setting which is very useful when multiple repositories offer the same package. It determines which repository gains what priority.

Removing Packages #

To remove a package we use the remove option. Similar to installing multiple packages, to remove more than one simply add its package name to the commandline.

sudo apt remove packageName1 packageName2

To remove unnecessary packages we can use the autoremove option. This option uninstalls any dependency that is not required by any installed packages.

sudo apt autoremove

To uninstall a package alongside its configuration files we use the purge option. You can purge more than one package in a single command like you can install or remove multiple packages. The formatting remains the same, simply add its package name to the command.

sudo apt purge packageName1 packageName2

Managing Repositories #

To add repositories to APT there are two steps we need to follow with the first being optional and depends on the repository. The first is to add a GPG key associated with the repository to our system and the second is to add the repository itself. The form of the command to add a repository looks like this:

add-apt-repository [OPTIONS] [Repository URL]

Suppose you want to add the wine repository to debian and then remove it. To add it you fill in the repository URL with wine’s repository. Always apt update after adding a repository.

sudo add-apt-repository 'deb https://dl.winehq.org/wine-builds/debian bookworm main'

To remove it you add --remove as an option.

sudo add-apt-repository --remove 'deb https://dl.winehq.org/wine-builds/debian bookworm main'

If you want to remove all the unused old repositories then you can use the following command:

sudo apt autoclean

---

If you found this post useful please consider donating a small amount to support my effort in creating more content like this.