1. Overview
Almost every operating system comes with a package management system for installing, upgrading, managing, and uninstalling software. The same is true for Ubuntu, which provides many tools for interacting with its package management system.
For installing a package, Ubuntu has a CLI interface for advanced users as well as a Graphical interface for new users. For example, Ubuntu has a powerful CLI tool called APT or Advanced Packaging Tool that serves our purpose.
In this article, we’ll learn about reinstalling the apt package on a Ubuntu 20.04 system.
2. Identifying the Problem
An error commonly encountered on Debian-based operating systems is “apt: command not found“. The error occurs when the apt package is uninstalled from the system. Therefore, to solve this error, we need to reinstall the apt package.
Also, note that apt is a Debian-based package. If you are still getting this error after following this article, you should check your Linux distribution. For instance, many users often try to install the apt package on an unsupported platform like Amazon Linux or CentOS. In most cases, the problem can be solved by using the proper package manager.
3. How to Install the apt Package?
Let’s first check if the apt package is present or not on our system:
$ sudo apt update
sudo: apt: command not found
Since the apt package was removed, we’re getting the command not found message.
Let’s now move on to installing back the apt package. Basically, we can use the default Software Center or an application like Gdebi for installing a .deb format file. However, in the wake of trying to install the apt package from a .deb file, we found that the Gdebi application and Ubuntu’s default Software Center cannot be used as they depend on apt.
Finally, the dpkg utility comes to the rescue and installs the apt package back into our system.
3.1. Using the dpkg Utility
dpkg is a packaging utility for Debian systems. Usually, we use dpkg to install packages already available on the system. Indeed, the apt-get command uses dpkg for installing these packages.
The official Ubuntu repository provides the apt package file for different versions of Ubuntu. We can check out the apt packages here. We have to download the file corresponding to our system OS version and processor architecture.
Since we’re using Ubuntu 20.04 for our example, we’ll download the file for Ubuntu focal 20.04 LTS. This file comes in Debian format, and we can download it using the wget utility:
$ wget http://security.ubuntu.com/ubuntu/pool/main/a/apt/apt_2.0.2ubuntu0.2_amd64.deb
--2022-11-14 10:09:15-- http://security.ubuntu.com/ubuntu/pool/main/a/apt/apt_2.0.2ubuntu0.2_amd64.deb
Resolving security.ubuntu.com (security.ubuntu.com)... 2620:2d:4000:1::19, 2620:2d ...
Connecting to security.ubuntu.com (security.ubuntu.com)|2620:2d:4000:1::19|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1289696 (1.2M) [application/x-debian-package]
Saving to: ‘apt_2.0.2ubuntu0.2_amd64.deb’
apt_2.0.2ubuntu0.2_ 100%[===================>] 1.23M 36.1KB/s in 35s
2022-11-14 10:09:55 (35.6 KB/s) - ‘apt_2.0.2ubuntu0.2_amd64.deb’ saved [1289696/1289696]
After downloading the file, we can continue with installing the package using the dpkg utility. Let’s see how the dpkg utility handles the package installation:
$ sudo dpkg -i apt_2.0.2ubuntu0.2_amd64.deb
Selecting previously unselected package apt.
(Reading database ... 177958 files and directories currently installed.)
Preparing to unpack apt_2.0.2ubuntu0.2_amd64.deb ...
Unpacking apt (2.0.2ubuntu0.2) ...
Setting up apt (2.0.2ubuntu0.2) ...
/etc/systemd/system/timers.target.wants/apt-daily.timer → /lib/systemd/system/apt-daily.timer.
Processing triggers for libc-bin (2.31-0ubuntu9.7) ...
Processing triggers for man-db (2.9.1-1)
The -i flag installs the specified package. Also, to test if the apt package installation is successful, we can try updating the local repository:
$ sudo apt update
Hit:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Now, this command runs smoothly. Finally, the apt package installation is successful on our system. Furthermore, we can now continue using the CLI package manager as well as Ubuntu’s Software Center.
3.2. Resolving Dependencies Issues
While installing the .deb files, we can come across some dependencies issues. In fact, we can fix this by installing these dependencies. Let’s discover the name of dependencies required for the apt package:
$ dpkg -I apt_2.0.2ubuntu0.2_amd64.deb
From the output of the above command, look for the line that says Depends:
Depends: adduser, gpgv | gpgv2 | gpgv1, libapt-pkg6.0 (>= 2.0.2ubuntu0.2),
ubuntu-keyring, libc6 (>= 2.15), libgcc-s1 (>= 3.0), libgnutls30 (>= 3.6.12),
libseccomp2 (>= 2.4.2), libstdc++6 (>= 9), libsystemd0
From here, we can select the dependencies we need to install for the apt package to work.
4. Conclusion
In this article, we’ve seen how to reinstall the apt package if it is unintentionally or otherwise removed from the system. In short, we have seen how we can use the dpkg utility for fixing this issue.