Sometimes you may need to add repository in Ubuntu/Debian Linux, in case the package you want to install using apt-get or apt command is not present in official Ubuntu Repositories. In such cases, you will need to add their repository to your list of sources used by Ubuntu to download packages. In this article, we will look at how to add repository in Ubuntu/Debian Linux.
How to Add Repository in Ubuntu/Debian Linux
Ubuntu/Debian systems store list of sources in /etc/apt/sources.list.d/ in a set of .list files. If the repository for your desired package is not present in any of these files, then Ubuntu/Debian will be unable to download your package via apt or apt-get command.
To add repository in Ubuntu/Debian Linux, you need to be logged in as root or user with sudo privileges. There are two ways to add repository – using add-apt-repository or by manually updating sources.list files.
1. Using add-apt-repository
The most common and recommended way to add repository is to use add-apt-repository command. Open terminal and run the following command to install it.
sudo apt update$
sudo apt install software-properties-common
Now you can easily add repository with following syntax
$ sudo add-apt-repository <repo_name>
Here is an example to add personal package archive (PPA) repository for Libre Office software. A PPA repository is a third-party repository built & distributed by developers via Ubuntu’s Launchpad website. In this case, just use their Launchpad reference in place of <repo_name> above.
$ sudo add-apt-repository ppa:libreoffice/ppa
If it is not a PPA repository, in some cases, you may need to add its public key first and only then add the repository. Here is an example to add MongoDB’s public key and then add its repository.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'
2. Manually update sources.list
You may also manually update or create sources.list file in /etc/apt
$ sudo vi /etc/apt/sources.list
In this file, you can add a repository in following format
deb <Repository_URL> <distribution_code_name> <repository_component>
Here is an example,
deb http://archive.getdeb.net/ubuntu wily-getdeb games
If you want to add multiple repositories, add one repository per line. Save and close the file.
Instead of opening the sources.list file, you may also add the above line using echo and tee commands as shown.
echo "deb http://archive.getdeb.net/ubuntu wily-getdeb games" | sudo tee -a /etc/apt/sources.list
That’s it. Now the new repository will be added to your system’s list of sources.