Cyberithub

8 Useful apt-add-repository command examples in Linux[Cheat Sheet]

Advertisements

In this article, I will take you through 8 Useful apt-add-repository command examples in Linux. I am sure you must be aware of downloading and installing packages from Ubuntu Repository but sometimes there are some packages which will not be available on Ubuntu Repository. For that you need to add a separate repository in your /etc/apt/sources.list file so that you can download the package from that repository using apt tool. While there are many ways to do that but the most simplest way is through apt-add-repository command. This command makes easy for us to add any repository information. We will see how by going through few of the real world examples in below section. More about apt-add-repository.

8 Useful apt-add-repository command examples in Linux[Cheat Sheet]

8 Useful apt-add-repository command examples in Linux

Also Read: How to Install Hashicorp Vault on Ubuntu 20.04 LTS

Example 1: How to Add a Repository using apt-add-repository 

If you want to add a repository information to /etc/apt/sources.list file so that you can download a package from that repository using apt package manager then you need to add it by using apt-add-repository <URL> command. For example, here we are adding Hashicorp repository to /etc/apt/sources.list file using apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" command as shown below.

root@localhost:~# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 https://apt.releases.hashicorp.com focal InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:5 https://packages.microsoft.com/repos/edge stable InRelease
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Ign:7 http://ppa.launchpad.net/oguzhaninan/stacer/ubuntu focal InRelease
Get:8 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease [17.5 kB]

 

Example 2: How to Remove a Repository using apt-add-repository

If you want to remove a repository information from /etc/apt/sources.list file then you need to use -r option with apt-add-repository command.

root@localhost:~# apt-add-repository -r "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

 

Example 3: How to Update Package Cache

After adding the repository if you want to update the package cache with packages from that repository then you can use -u option with apt-add-repository command. This will save you from running apt update command.

root@localhost:~# apt-add-repository -u "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://apt.releases.hashicorp.com focal InRelease
Hit:5 https://packages.microsoft.com/repos/edge stable InRelease
Ign:6 http://ppa.launchpad.net/oguzhaninan/stacer/ubuntu focal InRelease
Hit:7 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:8 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease [17.5 kB]

 

Example 4: How to Show Debug Information on the Output

If you want to show the debugging information on the output then you need to use -m option with apt-add-repository command as shown below.

root@localhost:~# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" -m
START SOURCES.LIST:
# deb cdrom:[Ubuntu 20.04.2 LTS _Focal Fossa_ - Release amd64 (20210204)]/ focal main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.

deb http://in.archive.ubuntu.com/ubuntu/ focal main restricted

# deb-src http://in.archive.ubuntu.com/ubuntu/ focal main restricted

## Major bug fix updates produced after the final release of the

## distribution.

 

Example 5: How to Assume Yes to all the Queries

If you want to auto answer yes to all the queries asked over the command prompt then -y will be the good option to use. For example, here we are trying to add ppa:ansible/ansible repository and update all the packages from this repository using apt-add-repository --update ppa:ansible/ansible command.

root@localhost:~# apt-add-repository --update ppa:ansible/ansible
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or Ctrl-c to cancel adding it.

As you can see from the above output, it is asking you to either Press ENTER or Ctrl-c to proceed. So here if you add -y then it will automatically take this input as yes and will proceed with the command.

root@localhost:~# apt-add-repository --update ppa:ansible/ansible -y

 

Example 6: How to Use Alternate Keyserver using apt-add-repository

If you want to use any alternate Keyserver then you can specify the details with --keyserver option. For example, here we are using an alternate Keyserver  hkp://keys.openpgp.org in performing below apt-add-repository operation.

root@localhost:~# apt-add-repository --keyserver=hkp://keys.openpgp.org --update ppa:ansible/ansible -y

 

Example 7: How to Check all the options available with apt-add-repository command

If you want to check all the options available with apt-add-repository command then you need to use apt-add-repository --help command as shown below.

root@localhost:~# apt-add-repository --help
Usage: apt-add-repository <sourceline>

apt-add-repository is a script for adding apt sources.list entries.
It can be used to add any repository and also provides a shorthand
syntax for adding a Launchpad PPA (Personal Package Archive)
repository.

<sourceline> - The apt repository source line to add. This is one of:
a complete apt line in quotes,
a repo url and areas in quotes (areas defaults to 'main')
a PPA shortcut.
a distro component

 

Example 8: How to Check the Man Page of apt-add-repository command

If you want to check the man page of apt-add-repository command then you need to use man apt-add-repository command as shown below.

root@localhost:~# man apt-add-repository
add-apt-repository(1) General Commands Manual add-apt-repository(1)

NAME
add-apt-repository - Adds a repository into the /etc/apt/sources.list or /etc/apt/sources.list.d or removes an existing one

SYNOPSIS
add-apt-repository [OPTIONS] REPOSITORY

DESCRIPTION
add-apt-repository is a script which adds an external APT repository to either /etc/apt/sources.list or a file in /etc/apt/sources.list.d/ or re‐
moves an already existing repository.

The options supported by add-apt-repository are:

-h, --help Show help message and exit

-m, --massive-debug Print a lot of debug information to the command line

Leave a Comment