How to Add a Package Repository to Debian, Ubuntu and Mint

A package repository (repo in short) is a location where software packages are stored and can be retrieved from. It is organized in a special directory tree, and usually contains an index file and the metadata needed by Linux package managers to access the packages in the repo and their dependencies.

Package managers such as APT (Advanced Packaging Tool – the default on Debian-like systems) enable you to automatically install and upgrade packages from a configured repo.

They also feature dependency resolution capabilities and software update checking. Besides, all package managers if not most, enable removing a package that is no longer needed.

Attention: Before we proceed, remember to only add software repositories from sources that you trust!

This guide shows how to add a package repository in Debian and Ubuntu Linux, as well as their derivatives such as Linux Mint.

Understanding APT Repository on Debian and Ubuntu

The APT package manager fetches packages from various repositories. A repository can be binary or source, and automatic or trivial. It can also be online or offline (for example on a CD-ROM) but the former is the more common use. Additionally, a repo can be official (maintained by Debian or Ubuntu/Canonical) or unofficial (third party).

A repository is defined in the /etc/apt/sources.list configuration file or in a file stored under the /etc/apt/sources.list.d directory. Every repo file must end with the .list extension.

$ ls /etc/apt/sources.list.d/
List All APT Repositories
List All APT Repositories

In a repository file, each line has the following syntax.

deb  URI  distribution component1  component2 … componentN
OR
deb-src  URI  distribution   component1  component2 … componentN

The first parameter specifies the archive type (deb – for binary or deb-src – for source files), next is the URI – the root of the repository, then the distribution, and one or more components.

APT Repository File
APT Repository File

Automatic APT Repositories

To define an automatic repository, you must specify one distribution and one or more components and the distribution must not end with a slash.

The following are examples of automatic repo definitions:

deb http://repository.spotify.com  stable  non-free
deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/focal  pgadmin4  main
deb https://download.mono-project.com/repo/ubuntu stable-focal main

Non-Automatic or Trivial APT Repositories

For non-automatic repos, the distribution specifies the relative path to the index files and it must end with a slash. Besides, no components may be specified.

Here are some examples:

deb http://cloud.r-project.org/bin/linux/debian bullseye-cran40/
deb file:///home/tecmint/rep-exact  binary/

Importantly, APT relies on the local package index, a database of available packages from the repositories defined in the /etc/apt/sources.list file and files under the /etc/apt/sources.list.d directory. You can find the package index files under the /var/lib/apt/lists/ directory.

After making changes to the repo configurations, you must always update the local package index before installing or updating packages.

$ sudo apt update
OR
$ sudo apt-get update 
Note: Usually, software owners or maintainers will provide a means either manually or automatically to add the required repository from which you can install their package(s) on your Linux system.

Let’s look at how to add the apt repository, as explained below.

Manually Adding APT Repository on Debian and Ubuntu

To add a repository manually, simply create the repo file under the /etc/apt/sources.list.d directory. We don’t recommend adding all repositories in the /etc/apt/sources.list file.

This example shows how to add the R software environment packages repository.

$ sudo vim /etc/apt/sources.list.d/r-packages.list

Copy and paste the repo configuration into the file and make sure that the parameters such as URI and distribution and components match what is defined in the repository:

deb https://cloud.r-project.org/bin/linux/ubuntu  focal-cran40/
Manually Add APT Repository
Manually Add APT Repository

Then add the repository key using the following wget command.

$ wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc 

Alternatively, you can also add the repo using the echo and tee commands as follows:

$ echo "deb https://cloud.r-project.org/bin/linux/ubuntu  focal-cran40/" | sudo tee /etc/apt/sources.list.d/r-packages.list
$ wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc

Next, you need to update the local package index with the latest changes made in the repositories and then install a package from the repository, as follows:

$ sudo apt update
$ sudo apt install --no-install-recommends r-base

To disable an apt repository, simply comment it out in the repository configuration file as shown in the following screenshot.

Disable APT Repository
Disable APT Repository

Adding APT Repository Using apt-add-repository Command

The add-apt-repository command-line utility is used to add a repository into the /etc/apt/sources.list file or /etc/apt/sources.list.d directory. If a repository already exists, it can remove it using the -r flag.

If the add-apt-repository utility is not available on your system, you can install the software-properties-common package which provides it:

$ sudo apt update
$ sudo apt install software-properties-common

Let’s look at the following example. The first command adds the repo and the second one retrieves the repo key using the wget utility and saves it to the specified file using the tee utility:

$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
$ wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc

If you try to add a repo that already exists, the add-apt-repository command will show the message “Repository already exists” as shown in the following screenshot.

Repository Already Exists
Repository Already Exists

The next step is to update the apt package index and install a package from the repository, as shown before:

$ sudo apt update
$ sudo apt install --no-install-recommends r-base

Adding Personal Package Archives (PPAs)

PPAs are software repositories for publishing user-contributed software packages via Launchpad. This example shows how to add the Shutter screenshot tool’s official PPA.

$ sudo add-apt-repository ppa:shutter/ppa
$ sudo apt-get update
$ sudo apt install shutter

Removing a PPA Repository

To remove a repo using the apt-add-repository utility, use its -r or --remove flag as mentioned earlier:

$ sudo add-apt-repository -r "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

Adding a Package Repository Using GUI Utilities

Last but not least, you can also add a repo to your system using a GUI application. On Ubuntu, open Software & Updates via the Activities search bar.

Then switch to the Other Software tab as highlighted in the following screenshot. Next, click Add and enter the repo configuration and follow the prompts.

Software & Updates Window
Software & Updates Window

On Linux Mint, open your system menu and search for Software Sources, then open it. When asked to authenticate, enter your password to invoke the application with root privileges.

Next, click on the Additional Repositories tab, then click the Add button.

Additional Repositories
Additional Repositories

In the pop window, add the repo and click OK.

Add APT Repository
Add APT Repository

Once the repo has been added successfully, a message will display at the bottom of the window. Click OK to proceed.

APT Repository Added
APT Repository Added

Then wait for the local package index to update. Once it is OK, you can proceed to install packages from the just added repo.

Updating APT Cache
Updating APT Cache

That’s it for now! In this guide, we have shown different ways to add a package repository in Debian and Ubuntu Linux, and their derivatives such as Linux Mint. For any comments or questions, reach us via the feedback form below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.