“add-apt-repository” Command in Ubuntu 22.04

In Linux-based systems such as Ubuntu 22.04, to add PPA packages to the source list, we can use the “add-apt-repository” command. It can directly import the PPA repositories and the GPG keys.

Ubuntu 22.04 has an official apt repository which provides us with many packages. But sometimes, we need to install some application from any other source rather than Ubuntu. In such a scenario, we can use the “add-apt-repository” command to add new packages or repositories from a third source in /etc/apt/sources.list or in the directory /etc/apt/sources.list.d/ of your system.

In this writeup, we will cover what the add-apt-repository command is, and how to use it to add and remove a repository in Ubuntu 22.04 system.

What is add-apt-repository command in Ubuntu 22.04

The “add-apt-repository” is a command that can help users to download a repository or some packages from a third party in /etc/apt/sources.list or /etc/apt/sources.list.d/ of Ubuntu. It is also used to remove specific packages or repositories from the system.

Syntax

To use the “add-apt-repository” command in the terminal, use the following syntax with the required “option”:

$ add-apt-repository [option] repository

Some important options supported by the “add-apt-repository” command are:

OptionsDescription
–removeIt is used to remove a repository or package.
–helpIt is used to get help messages.
–enable-sourceIt enables downloading packages from the repository.
–yesIt is used to accept all queries.

How to install “add-apt-repository” command in Ubuntu 22.04

For the installation of the add-apt-repository command, firstly use “CTRL+ALT+T” combined key to open a terminal and install the “software-properties-common” package:

$ sudo apt install software-properties-common

After installing the mentioned package, the “add-apt-repository” command should be all ready to use in Ubuntu 22.04 system.

Now, let’s check out the method of adding a repository in Ubuntu 22.04 using the “add-apt-repository” command.

Using add-apt-repository command to add third party repository

In this method, we will demonstrate how you can download a repository from an external source.

Step 1: Adding GPG key

When we want to install a repository directly from the third source rather than a PPA or Ubuntu repository, it is needed to add a GPG key first. Utilize following command to add key:

$ wget -qO- https://download.webmin.com/jcameron-key.asc | sudo tee -a /etc/apt/trusted.gpg.d/jcameron-key.asc

Step 2: Adding Webmin repository

Now, using the “add-apt-repository” command, we will add the Webmin repository to the Ubuntu source list. To do so, use the below-mentioned command:

$ sudo add-apt-repository "deb [arch=amd64] http://download.webmin.com/download/repository sarge contrib"

Step 3: Installing Webmin

Now Ubuntu knows where to go and find a repository and install a specific application. You can install Webmin using the below-given command:

$ sudo apt install webmin -y

Above mentioned method explains the installation of external source repositories using add-apt-repository on Ubuntu 22.04.

Using add-apt-repository command to add PPA repository

We will now utilize the “add-apt-repository” command to add a PPA repository to our Ubuntu 22.04 system. In this method, there is no need to manually add a GPG key as the “add-apt-repository” command can automatically import it. So, for the demonstration purpose, we will add the “ansible” PPA repository.

Step1: Update and Upgrade Ubuntu 22.04 repository

Firstly, we will update and upgrade the Ubuntu repository using the below-given command:

$ sudo apt update && sudo apt upgrade

Step 2: Add ansible repository

In the second step, we will add the ansible PPA repository usingthe “add-apt-repository” following command:

$ sudo add-apt-repository PPA:ansible/ansible

The “add-apt-repository” command automatically imports the GPG key as shown below:

Now we will move toward the procedure of removing a repository using “add-apt-repository” command

Using add-apt-repository command to remove a repository from Ubuntu 22.04

We can remove a repository from Ubuntu apt official repository. To do this, use the “–remove” option that is supported by the “add-apt-repository” command. For the demonstration purpose, we will remove the PPA ansible repository using the below-given command:

$ sudo add-apt-repository --remove ppa:ansible/ansible

This is all about adding the repository from any other sources. We have covered all the essential steps to add a repository from a third party and how we can remove any repository using the “add-apt-repository” command.

Conclusion

In Ubuntu 22.04, the add-apt-repository command is mainly used to add packages and repositories from a third party. To install it, execute the “$ sudo apt install software-properties-common” command on the terminal. Many options are supported by the add-apt-repository command to, add any repository, enable sources, get help, remove any package or repository, and so on. This article covers the essential information and use of add-apt-repository on Ubuntu 22.04 in detail.