If you have ever used Linux-based operating systems, you might have the experience of pulling packages from their default repositories. Most of the time, you will use their package management commands, which are convenient and easy-to-use. They allow you to install and uninstall packages and give you access to many useful packages. But, sometimes, the packages you want to download may not be included in the central repositories of your OS. In that case, you will have to add a new repository to your system so that it can be recognized by the package management tool.
This article explains why adding a repository is needed and how it can be done. Before we start, we need to understand that managing Linux distributions on your own can be a demanding job. Packagecloud helps you to ease the pain of managing your Linux systems and to focus on what matters to you.
Check out packagecloud’s free trial today.
What is a repository?
To better understand the topic, we need to know what repository means and what a repository does. A repository, or repo, is basically a storage system that contains software packages. In this repo, each package is stored with corresponding metadata. The metadata contains information about a package such as available versions, dependencies, license, build time, documentation, and more. Thanks to the metadata, a repository can map your install command to the package when you run a command. A package management tool in your Linux operating system allows you to interact with a repository and manage installed packages.
Why do you want to add a repository?
Most of the time, you can just use default repositories since they contain the most common and popular packages. A repository stores thousands of packages so there will always be plenty of software you can use. However, in edge cases, you might not be able to find a package that suits your needs. You will do some research on the Internet and find that a package exists but in a third-party repository. Those third-party packages are not managed or endorsed by central repos, and you need to understand that there can be good reasons they’re not included. Using a third-party package comes with risk, so make sure you do your research and check if it’s safe to use.
How to add a repository
Each Linux-based operating system has a different package management tool, thus different ways to interact with repositories. If you want to add a third-party repo, you first need to know which operating system you are using since you will have to use disparate methods to add a third-party repo. This section introduces how to perform these operations for the operating systems Ubuntu, Debian, and Fedora. Depending on your OS, there can be more than one way.
The section also explains how to manually add a repository. However, if there is no good reason, try the methods using default package management tools first to reduce the chance of error.
Ubuntu and Debian
Ubuntu and Debian are both Linux distributions, and they share many similarities including their package management tools. To add a repository, we first need to understand how they manage packages.
On Ubuntu and Debian, you can use apt commands to control packages. The management tool refers to either of the locations to find repositories:
- /etc/apt/sources.list (file)
- /etc/apt/sources.list.d/ (files under this directory)
Note that the repository file names have the .list extension. When you add a new one, you should follow this naming rule.
A source repo file contains a list of packages that looks like:
deb http://repo.tld/ubuntu distro component
Each line follows specific rules:
- The first part, deb, describes the archive type. It can be either deb (as above) or deb-scr. While deb means that the repository contains .deb packages, deb-scr means they are source packages.
- The second part is a repo URL.
- The third part defines the distribution code name.
- The last part means the repository components or categories.
The files, sources.list, and the ones under the directory, sources.list.d, follow these same rules. Now that we have learned how the repositories are defined in a system, let’s find out how to add a repo. To follow the instructions, you will need a sudo permission.
In both systems, you can use apt commands to control packages. Thanks to the tool, you can easily add an APT repository to the first list file or a new one under the sources.list.d directory. Using add-apt-repository, you can not only add but also remove an existing repo.
However, you may not have the command installed yet. To install it, execute the commands:
$ sudo apt update $ sudo apt install software-properties-common
Once it’s installed, you can use it. The command has the structure below:
add-apt-repository [options] repository
The repository section can be replaced with a regular repository syntax that can be found in the sources.list file such as:
http://repo.tld/ubuntu distro component
Conversely, it can be a ppa format as:
If you want to know all available options, try:
$ man add-apt-repository
Now that we have learned the basics, let’s try to add a repository. Let’s say that we want to add a game repo called wily-getdeb games.
$ sudo add-apt-repository "deb http://archive.getdeb.net/ubuntu wily-getdeb games"
This new repository will appear in the sources.list file. Now you can download a game that’s available in that repo by:
$ sudo apt install [game name]
If you want to remove the new repository later, you can simply use the
--remove option as below:
$ sudo add-apt-repository --remove 'deb http://archive.getdeb.net/ubuntu wily-getdeb games'
PPA stands for personal package archives. They are designed for Ubuntu and Debian users to install third-party repositories. They are often used to distribute pre-release software packages so that they can be tested.
When you add a PPA repository, it adds a new file under the /etc/apt/sources.list.d/ directory. Let’s say we want to add a LibreOffice repo. You can add it by:
$ sudo add-apt-repository ppa:libreoffice/ppa
Once executed, you will be prompted to either hit enter to continue or Ctrl-c to cancel. Hit enter. When it’s done, you can install a LibreOffice package by running the same sudo apt install command.
Using these two ways, you can add a repository to your Ubuntu or Debian system.
There is another convenient way to add repositories in Debian using reprepro. Check out our dedicated article for more details.
If you want to add a repo with your own rules, you can manually add repos by editing the repository list file, /etc/apt/sources.list. In that file, you can add an apt repository. For example, let’s imagine we want to add the CouchDB repository and install its package. To edit the file, you can run the following command.
$ sudo nano /etc/apt/sources.list
In the file, add the following line:
deb https://apache.bintray.com/couchdb-deb bionic main
Alternatively, you can directly add the line without opening the text editor:
$ echo "deb https://apache.bintray.com/couchdb-deb $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list
You can also add a file under the /etc/apt/sources.list.d/ directory.
you need to register its public repository key. To do this, you can use the curl command below:
$ curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | sudo apt-key add -
When you execute the command, it should return OK to tell you that the key has been registered and the repo will be regarded as a trusted source. Now you can install the packages from the new repo. However, update the package index first by:
$ sudo apt update
Now you can install packages from it.
$ sudo apt install couchdb
Repository Management Commands
After you add new repositories, you can list, disable, and remove them using the following commands.
To list repositories, try the command below. If you modified the files under /etc/apt/sources.list.d/, change the last part accordingly.
$ sudo nano /etc/apt/sources.list
To disable a specific repo, you can simply comment it out using the text editor:
# deb http://archive.ubuntu.com/ubuntu focal multiverse
To remove a repo, you can run:
$ add-apt-repository --remove 'deb [arch=amd64,arm64,ppc64el] http://mirror.mephi.ru/mariadb/repo/10.5/ubuntu focal main'
After modifying your repositories, make sure you apply the changes:
$ sudo apt update
Packagecloud is a cloud-based service for distributing different software packages in a unified, reliable, and scalable way, without owning any infrastructure. You can keep all of the packages that need to be distributed across your organization's machines in one repo, regardless of the OS or programming language. Then, you can efficiently distribute your packages to your devices in a secure way, without having to own any of the infrastructure involved in doing so.
This enables users to save time and money on setting up servers for hosting packages for each OS. Packagecloud allows users to set up and update machines faster and with less overhead than ever before.
Sign up for the packagecloud free trial to get your machines set up and updated easily!
Fedora uses YUM as a package management tool. Since it’s different from the two operating systems above, it has its own way to add a repository.
You can use the YUM install command to add a repository. First, you need to install an .rpm that contains repository information. The command below installs the EPEL repo for CentOS 8.
$ yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
After install, you can enable and disable packages from a repository. For example:
# install a remi repo $ sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm # disable php5.4 from the repo $ sudo yum-config-manager --disable remi-php54 # enable php7.2 from the repo $ sudo yum-config-manager --enable remi-php72
With this method, you can add a new repo and manage its packages.
In this article, we have learned what it means to add a repository to your Linux-based system and how to do that. If you can find packages from default repositories, it is recommended to use them for security. When it’s necessary, you can use the package management tools from your OS. Before you do that, double check if the repository is trustworthy. Thanks to the package tools, you will be able to add one conveniently.
However, these maintenance tasks for multiple operating systems and servers can be demanding. Packagecloud provides extensive and easy features to help users maintain their infrastructures. Visit packagecloud today and try our free trial.