×

Search anything:

Managing Debian packages with apt

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

apt(Advanced Package Tool) is a package management tool. It is used to install, configure, search for, update, and remove software in a Debian-based Linux system.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Commands.
  4. Summary.
  5. References.

Prerequisites.

  1. dpkg

Introduction.

The apt command is used to manage packages in Debian-based Linux systems, this includes everything from searching for a package, installing it, updating repositories, upgrading packages, removing packages, etc.
We discuss commonly used apt commands.

Syntax.

We write apt commands using the following format:

$ apt [options] command

apt1-1

Commands.

In this section, we discuss commonly used apt commands for managing packages.

1. Updating repositories.

A repository is a remote location where a Linux system downloads and installs software and software updates.

The repositories we are talking about are located in the /etc/apt/sources.list file.
apt2-1

To update repositories with apt we write:

$ sudo apt-get update

apt3

From the output we expect to see things like hit which means that the is no change since the last update, get means that there is a package update and therefore apt will download the details of the update finally the ign means that the package is ignored since there might be an error or other reasons.

2. Upgrading with apt.

The above step just involved updating the repos. We have not installed anything yet. It was a preparation for this step. Here we upgrade the installed packages to their latest versions which we get from the remote repos.

$ sudo apt-get upgrade

apt4

We could also write:

$ sudo apt-get dist-upgrade

The above command handles everything from adding, removing or updating packages.
apt5

We can be specific about this and only upgrade a specific package as follows:

$ sudo apt-get upgrade [package name]

apt6

Notice how we get a confirmation prompt where we have to enter either y or n, we can avoid this by specifying it in the command as follows.

$ sudo apt-get upgrade -y

This also applies to the previous commands.

3. Installing packages.

We have seen how to update repositories and upgrade already installed packages. We can also install new packages using the apt command:

$ sudo apt-get install [package name]

apt7

To install multiple packages, we just list them as follows:

$ sudo apt-get install [package name1] [package name2] [package name3]

The above commands by default will install the latest package release. However, we may need a specific version of the package. For this we have to specify the package name and its version number as follows:

$ sudo apt-get [package name]=[version number]

The commands install and update packages, however, there are situations where we only want to install a package without upgrading anything. For such cases we use the --no-upgrade option as follows:

sudo apt-get install [package name] --no-upgrade

apt10

We can further be specific and only upgrade installed packages during installation instead of installing and upgrading new packages by using the --only-upgrade option as follows:

sudo apt-get install [package name] --only-upgrade

4. Searching remote repositories.

Before installing a package we may need to confirm its existence in a remote repository package database. For this we use the apt-cache command with the search option as follows:

$ sudo apt-cache search [package name]

apt11

What if we are not exactly sure of the package name? We use the showpkg option as follows:

$ sudo apt-cache showpkg [package name]

This will list all packages with the search term within their names.
apt12

A dependency is whereby one package depends on another to function, for example, network applications such as Nmap depending on the low-level networking libraries.
The above command also displays useful information such as a package's dependencies.

We could also write the following command to list a package's dependencies.

$ sudo apt-cache pkgnames [package name]

apt13

We can also list all available packages in the package database and grep the output using the following command:

$ sudo apt-cache pkgnames

apt14

Generally, the apt-cache command queries the package cache of apt. To view its statistics we write:

$ sudo apt-cache stats

apt15

4. Removing packages.

To remove installed packages we use remove option as follows:

$ sudo apt-get remove [package name]

apt16

Removing a package will remove the package but leave its configuration files. To remove everything we have to purge the package from the system. With apt command we purge a package using the following command:

$ sudo apt-get purge [package name]

apt17

To combine the above two commands we use the autoremove option which removes and purges a package from the system.

$ sudo apt-get autoremove [package name]

apt18

5. Viewing package information.

If we want to view details about a package we write:

$ sudo apt-cache show [package name]

apt19

6. Downloading package source code.

We may want to download a package's source code for local compilation or change it. For this task we use the --download-only source option with eth opt-get command as follows:

$ sudo apt-get --download-only source [package name]

To download and unpack the source code we write;

$ sudo apt-get source [package name]

We can also download, unpack and compile all at once using the following command:

$ sudo apt-get --compile source [package name]

6. Cleaning disk space.

After all that, we free up space by removing downloaded .deb packages from the local repo by writing:

$ sudo apt-get clean

apt20

apt21

We can also write:

$ sudo apt-get autoclean

apt22

The above commands delete all .deb files from the /var/cache/apt/archives directory.

We have just looked at the few most common apt commands. For other options or the apt command manual page we write:

$ apt --help

apt23

$ man apt

Summary.

In the background, apt uses dpkg to install packages. Unlike with dpkg which requires us to have downloaded the package locally, apt fetches the package from a remote repo and installs it in the system. Another difference between the two package management tools is that apt installs packages including their dependencies compared to dpkg which only installs the package.

Both are useful in specific situations for example to list all locally installed packages we'd prefer dpkg over apt since the latter lists everything, both local and remote while the former only lists local packages.

References.

  1. To read the manual pages execute the command $ man apt.
Managing Debian packages with apt
Share this