When you search for a package on Linux through the command line, your system's package manager looks for the package in various repositories. By default, every Linux distribution has some official repositories that contain stable packages supported by the distro team.

However, when you try to grab a package not present in the official software repositories, the package manager will display an error. In such situations, what you can do is manually add the third-party repository to your system and then download the package.

Let's see how you can add new repositories to your system's sources list.

On Ubuntu and Debian-Based Distros

Debian-based distributions, including, Ubuntu make use of the Advanced Package Tool (APT) to install and update packages. You can find software repository information in the /etc/apt/sources.list file on your Debian-based Linux installation.

Although you can manually enter repository details in the file, it can quickly become a tiresome job. A better way of adding THEM to your system is by using the add-apt-repository tool.

Install add-apt-repository

You won't find the add-apt-repository utility installed on your system by default. It is a part of the software-properties-common package. To install add-apt-repository using the APT package manager, type:

        sudo apt install software-properties-common
    

Add Repositories Using add-apt-repository

Now that you've installed the package, it's time to add a third-party software repository to your system. The basic syntax for adding repositories is:

        sudo add-apt-repository [options] repository
    

...where repository is the format used by the sources.list file.

The default entry format for the file is:

        deb https://repositoryurl.com distro type
    

For example, to add the Ubuntu universe repository to your system:

        sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe"
    

You can also add a PPA with add-apt-repository using the following command syntax:

        sudo add-apt-repository ppa:user/name
    

...where user and name is the owner name and PPA name respectively.

To add the PHP PPA by Ondrej using add-apt-repository:

        sudo add-apt-repository ppa:ondrej/php
    

Related: What's the Difference Between APT and dpkg in Ubuntu?

Manually Adding Repositories on Fedora and CentOS

Fedora, CentOS, and other RHEL-based distributions use DNF and yum package managers. Unlike APT, DNF has a built-in method config-manager that allows users to add, remove, and disable third-party repositories with ease.

Using DNF Package Manager

To add a new repository using DNF:

        dnf config-manager --add-repo repository
    

...where repository is the URL to the software repository.

To enable a repository, type:

        dnf config-manager --set-enabled repository
    

Using yum-config-manager

Alternatively, you can also use the yum-config-manager utility to add and manage repositories on your system. Yum-config-manager is not installed on RHEL-based distributions by default, so you'll have to install it manually. It is a part of the yum-utils package. To install it, type:

        sudo dnf install yum-utils
    

The format of the yum-config-manager command is similar to DNF. You can add a new repository using yum-config-manager as follows:

        yum-config-manager --add-repo repository
    

Enabling a repository is easy as well.

        yum-config-manager --enable repository
    

Arch Linux and the AUR

If you're using Arch Linux, you might already know that users can't add repositories on Arch. But that doesn't mean you can't download additional packages on your system.

Arch Linux has the AUR, the Arch User Repository, which contains thousands of third-party packages developed by users. You can access the AUR using an AUR package manager. Pacman, which is the default package manager on Arch Linux, can't access the packages stored in the Arch User Repository.

There are several AUR package managers, the most prominent one being yay. You can easily install yay on your system by cloning the git repository.

        git clone https://aur.archlinux.org/yay-git.git
    

Change the permissions of the downloaded folder:

        sudo chmod 777 /yay-git
    

Change the directory and use the makepkg command to install yay:

        cd /yay-git && makepkg -si
    

Learn More: How to Install and Remove Packages in Arch Linux

Managing Your System’s Repository List

From a Linux installation to its daily usage, repositories are responsible for supplying packages to the system. If you're a beginner, the default repositories would be sufficient to cater to your needs. However, knowing how to add repositories will definitely be beneficial for those who want to download third-party packages on their computer.

On Linux, you might have often come across the broken package error while updating or installing new packages. Reinstalling or removing such packages fixes the issue on most Linux systems.