1. Overview

In Linux, dependencies are libraries, packages, or modules installed in order to run a program smoothly. Sometimes, when we install a package, its dependencies are not installed automatically or may be missing. So, as a Linux administrator, knowing how to install these dependencies is essential.

In this tutorial, we’ll discuss how to install a package and its dependencies using the dpkg, apt, and gdebi commands on the command line. We’ll focus on the Debian and Ubuntu Linux distributions.

2. Using the dpkg Command

dpkg is a package management tool used in Debian-based Linux distributions such as Ubuntu, Debian, and Linux Mint. We use it to install, remove, and manage individual Debian packages.

In order to install a package using dpkg, we first need to download it from a trusted source or repository and save it in a convenient location on our system. To demonstrate, we’ll be installing the vlc package.

First, let’s navigate to the /tmp directory:

$ cd /tmp

Next, let’s go to pkgs.org and search for the vlc package. Then, we’ll use the wget command to download this package and save it in the /tmp directory:

$ wget http://archive.ubuntu.com/ubuntu/pool/universe/v/vlc/vlc_3.0.16-1build7_amd64.deb

After downloading the package, we’ll install it:

$ sudo dpkg -i vlc_3.0.16-1build7_amd64.deb 
Selecting previously unselected package vlc.
(Reading database ... 225168 files and directories currently installed.)
...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...

In the example above, we pass the downloaded vlc_3.0.16-1build7_amd64.deb package as an argument to the dpkg command. This installs vlc on our system.

During installation, dpkg may throw an error if there are any missing dependencies. Once this happens, we can use the apt command to solve the issue:

$ sudo apt install -f
Reading package lists... Done
Building dependency tree... Done
...

The above command will install any missing dependencies for the installed package.

3. Using the apt Command

apt is a higher-level tool used to install and manage software packages on Debian-based Linux distributions. This command can install, update, and manage packages, including their dependencies.

This command follows a general syntax:

$ apt [options] command

Let’s see how to install a package and its dependencies.

First, we must make sure that all the packages installed on our system are up-to-date:

$ sudo apt update
Hit:1 http://ke.archive.ubuntu.com/ubuntu jammy InRelease
...
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

Once we know all of our packages are up-to-date, we can go ahead and install the vlc package and its dependencies:

$ sudo apt install vlc
Reading package lists... Done
Building dependency tree... Done
...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libvlc-bin:amd64 (3.0.16-1build7) ...

In the example above, we’ve been able to install the vlc package along with any other dependencies it needs to function correctly. When we use this command, it automatically resolves any missing dependencies and installs them.

4. Using the gdebi Command

gdebi is a simple and easy-to-use package installer used to install packages and their dependencies on Debian-based systems. It automatically detects and installs all the necessary dependencies needed for a package to run properly.

By default, gdebi is not installed on most Debian-based systems. Hence, we first need to install it:

$ sudo apt-get install gdebi

Once we’ve successfully installed gdebi on our system, we can use it to install packages and their dependencies with ease.

Given we’re in the directory where our package is located, we can install it:

$ sudo gdebi vlc_3.0.16-1build7_amd64.deb
Reading package lists... Done
Building dependency tree... Done
...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...

In the example above, we install the package along with its dependencies.

5. Conclusion

In this article, we discussed how we can install a package and its dependencies using the dpkg, apt, and gdebi commands. Using these tools takes only a few steps.

Furthermore, we discovered that the dpkg command requires us to resolve dependency issues manually, while the apt and gdebi commands handle them automatically.