When you install a package in Debian/Ubuntu using apt or apt-get commands, it downloads the package from one of the official repositories that maintains the package. But different Ubuntu versions use different repositories to begin with. Sometimes, the package you want to install may not be present in any of the default repositories on your system, and you may need to add another repository to your Ubuntu system, in order to be able to download & install the package via apt or apt-get commands. In this article, we will learn how to add repository in Ubuntu. You can also use these steps to add third-party repositories to your system.
How to Add Repository in Ubuntu
There are many ways to add repository in Ubuntu. We will look at two of them. All the repositories in your system are defined in sources.list file at /etc/apt/sources.list, or in separate folders in /etc/apt/sources.list.d/ as .list files.
1. Using add-apt-repository
add-apt-repository is a python script that allows you to add an apt repository to either /etc/apt/sources.list or /etc/apt/sources.list.d/ folder. You can also use it to remove repositories. If it is not available on your system, you can install it with the following command.
$ sudo apt update $ sudo apt install software-properties-common
Here is the basic syntax to add repository.
$ add-apt-repository [options] repository
where repository is a regular repository entry like deb http://repo.tld/ubuntu distro component or PPA repository like ppa:<user>/<ppa-name>
From Ubuntu 18.04 onwards, add-apt-repository command will automatically update package index if you import repository’s public key.
Here is an example to import MongoDB repository’s public key.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
Next, add the MongoDB repository command.
$ sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'
Once the new repository is added, you can use apt-get command to install MongoDB package.
$ sudo apt install mongodb-org
If you want to delete repository, you can do so by adding –remove option.
$ sudo add-apt-repository --remove 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'
Here is an example to add PPA repository.
$ sudo add-apt-repository ppa:jonathonf/ffmpeg-4
Once the PPA repository is installed, you can install packages using apt/apt-get commands.
$ sudo apt install ffmpeg
2. Manually Add Repositories
You can also manually add repositories by opening /etc/sources.list file in a text editor, and adding repositories in the following format.
deb http://repo.tld/ubuntu distro component...
- deb – It is the archive type that can be either deb or deb-src. Deb implies that repository contains
.debpackages while deb-src implies source packages.
- Next entry is the repository URL.
- Third entry is the distribution code name, such as beaver, xenial and so on.
- Next entries are the repository components or categories. By default, Ubuntu repositories are split into four components – main, restricted, universe and multiverse. Generally, third-party repositories have only one category.
Here is an example to add couchdb repository.
$ deb https://apache.bintray.com/couchdb-deb bionic main
You can also use echo command to add the above line without opening the file in text editor.
$ echo "deb https://apache.bintray.com/couchdb-deb $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list
Sometimes you may need to manually download and add the public repository key to your system. You can do so with the following command.
$ curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | sudo apt-key add - Copy
In this article, we have learnt couple of simple ways to add repository in Ubuntu. It is advisable to add repository using add-apt-repository command, instead of doing it manually, to avoid manual errors.