Read in 5 minutes

last updated 

How To Add Apt Repository In Ubuntu

When installing packages using the Ubuntu Software Center or the command line utilities such as apt or apt-get the packages are downloaded from one or more apt software repositories. An APT repository is a network server or a local directory containing deb packages and metadata files that are readable by the APT tools.

While there are thousands of application available in the default Ubuntu repositories, sometimes you may need to install software from a 3rd party repository.

In this tutorial, we will show you two ways to add apt repository on Ubuntu and Debian systems. The first method is by using the add-apt-repository command and the second one is to manually add the repository using a text editor.

Apt Sources

On Ubuntu and all other Debian based distributions, the apt software repositories are defined in the /etc/apt/sources.list file or in separate files under the /etc/apt/sources.list.d/ directory.

The names of the repository files inside the /etc/apt/sources.list.d/ directory must end with .list.

The general syntax of the /etc/apt/sources.list file takes the following format:

deb http://repo.tld/ubuntu distro component...
  • The first entry in the line defines the type of the archive. The archive type can be either deb or deb-src. Deb implies that the repository contains .deb packages while deb-src implies source packages.
  • The second entry is the repository URL.
  • The third entry specifies the distribution code name, such as beaver, xenial and so on.
  • The last entries are the repository components or categories. The default Ubuntu repositories are split into four components - main, restricted, universe and multiverse. Generally, third-party repositories have only one category.

The format for the files under the /etc/apt/sources.list.d/ directory is the same as for the regular sources.list file.

Most repositories are providing a public key to authenticate downloaded packages which need to be downloaded and imported.

To be able to add or remove a repository you need to be logged in as either a user with sudo access or root.

Usually, the instructions about how to enable a certain repository are included in the software documentation.

Installing add-apt-repository (add-apt-repository command not found )

add-apt-repository is a Python script that allows you to add an APT repository to either /etc/apt/sources.list or to a separate file in the /etc/apt/sources.list.d directory. The command can also be used to remove an already existing repository.

If the add-apt-repository is not available on your system you will get an error message saying “add-apt-repository command not found”.

The add-apt-repository utility is included in the software-properties-common package. To install it run the following commands:

sudo apt update
sudo apt install software-properties-common

Adding Repositories with add-apt-repository

The basic syntax of the add-apt-repository command is as follows:

add-apt-repository [options] repository

Where repository can be either a regular repository entry that can be added to the sources.list file like deb http://repo.tld/ubuntu distro component or a PPA repository in the ppa:<user>/<ppa-name> format.

To see all available options of the add-apt-repository command type man add-apt-repository in your terminal.

By default, on ubuntu 18.04 and newer the add-apt-repository will also update the package index if the repository public key is imported.

The package index is a database that holds records of available packages from the repositories enabled in your system.

Let’s say you want to install MongoDB from their official repositories.

First import the repository public key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Add the MongoDB repository using the command below.

sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

The repository will be appended to sources.list file.

You can now install any of the packages from the newly enabled repository:

sudo apt install mongodb-org

If for any reasons you want to remove a previously enabled repository, use the --remove option:

sudo add-apt-repository --remove 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

Adding PPA Repositories

Personal Package Archives (PPA) is a service that allows users to upload Ubuntu source packages that are built and published with Launchpad as an apt repository.

When adding a PPA repository the add-apt-repository command creates a new file under the /etc/apt/sources.list.d/ directory.

For example, to add the Jonathon F’s PPA which provides FFmpeg version 4.x you would run:

sudo add-apt-repository ppa:jonathonf/ffmpeg-4

When prompted press Enter an the repository will be enabled.

Press [ENTER] to continue or Ctrl-c to cancel adding it.

The PPA repository public key will be automatically downloaded and registered.

Once the PPA is added to your system you can install the repository packages:

sudo apt install ffmpeg

The apt command will install the package and all its dependencies.

Manually Adding Repositories

If you want to have more control over how your sources are organized you can manually edit the /etc/apt/sources.list file and add the apt repository line to the file.

For demonstration, we will enable the CouchDB repository and install the software. CouchDB is a free and open-source fault-tolerant NoSQL database maintained by the Apache Software Foundation.

To add the repository open the sources.list file with your text editor:

sudo nano /etc/apt/sources.list

Add the repository line to the end of the file:

/etc/apt/sources.list
deb https://apache.bintray.com/couchdb-deb bionic main

Instead of editing the file with a text editor you can use the following command to append the repository line to the sources.list file:

echo "deb https://apache.bintray.com/couchdb-deb $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list

$(lsb_release -cs) will print the Ubuntu codename. For example, if you have Ubuntu version 18.04 the command will print bionic.

Another option is to create a new the repository file under the /etc/apt/sources.list.d/ directory.

When manually configuring a repository you also need to manually import the public repository key to your system. To do that use either wget or curl:

curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | sudo apt-key add -

The command above should output OK which means that the GPG key has been successfully imported and packages from this repository will be considered trusted.

Before installing the packages from the newly added repository you must update the package index:

sudo apt update

Once the package index is updated you can install packages from the newly added repository:

sudo apt install couchdb

Conclusion

We have shown you how to add apt repositories in Ubuntu. The same instructions apply any Debian based distribution, including Kubuntu, Linux Mint and Elementary OS.

Feel free to leave a comment if you have any questions.