Ubuntu 添加存储库

Ubuntu Add Repository

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 updatesudo 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:

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


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.


使用 Ubuntu 软件中心或命令行实用程序(如 apt 或 apt-get)安装软件包时,软件包是从一个或多个 apt 软件存储库下载的。 APT 存储库是一个网络服务器或本地目录,其中包含可由 APT 工具读取的 deb 包和元数据文件。

虽然默认 Ubuntu 存储库中有数千个应用程序可用,但有时您可能需要从 3rd 方存储库安装软件。

在本教程中,我们将向您展示在 Ubuntu 和 Debian 系统上添加 apt 存储库的两种方法。第一种方法是使用 add-apt-repository 命令,第二种方法是使用文本编辑器手动添加存储库。

在 Ubuntu 和所有其他基于 Debian 的发行版上,apt 软件存储库在 /etc/apt/sources.list 文件或 /etc/apt/sources.list.d/ 目录下的单独文件中定义。

/etc/apt/sources.list.d/ 目录中的存储库文件的名称必须以 .list 结尾。

/etc/apt/sources.list 文件的一般语法采用以下格式:

deb http://repo.tld/ubuntu 发行版组件…
行中的第一个条目定义存档的类型。归档类型可以是 deb 或 deb-src。 Deb 表示存储库包含 .deb 包,而 deb-src 表示源包。
第二个条目是存储库 URL。
最后的条目是存储库组件或类别。默认的 Ubuntu 存储库分为四个组件 – main、restricted、universe 和 multiverse。通常,第三方存储库只有一个类别。
/etc/apt/sources.list.d/ 目录下的文件格式与常规 sources.list 文件的格式相同。


为了能够添加或删除存储库,您需要以具有 sudo 访问权限的用户或 root 用户身份登录。


安装 add-apt-repository (add-apt-repository command not found )
add-apt-repository 是一个 Python 脚本,允许您将 APT 存储库添加到 /etc/apt/sources.list 或 /etc/apt/sources.list.d 目录中的单独文件。该命令还可用于删除已经存在的存储库。

如果您的系统上没有 add-apt-repository,您将收到一条错误消息,提示“找不到 add-apt-repository 命令”。

add-apt-repository 实用程序包含在 software-properties-common 包中。要安装它,请运行以下命令:

sudo apt 更新
sudo apt install software-properties-common
使用 add-apt-repository 添加存储库
add-apt-repository 命令的基本语法如下:

add-apt-repository [选项] 存储库
其中存储库可以是可以添加到 sources.list 文件(如 deb http://repo.tld/ubuntu 发行版组件)的常规存储库条目,也可以是 ppa:<user>/<ppa-name> 格式的 PPA 存储库.

要查看 add-apt-repository 命令的所有可用选项,请在终端中键入 man add-apt-repository。

默认情况下,在 ubuntu 18.04 和更新版本上,如果存储库公钥被导入,add-apt-repository 也会更新包索引。


假设您想从他们的官方存储库安装 MongoDB。


sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
使用以下命令添加 MongoDB 存储库。

sudo add-apt-repository ‘deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse’
存储库将附加到 sources.list 文件。


sudo apt install mongodb-org
如果出于任何原因要删除以前启用的存储库,请使用 –remove 选项:

sudo add-apt-repository –remove ‘deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse’
添加 PPA 存储库
Personal Package Archives (PPA) 是一项服务,允许用户上传使用 Launchpad 作为 apt 存储库构建和发布的 Ubuntu 源包。

添加 PPA 存储库时,add-apt-repository 命令会在 /etc/apt/sources.list.d/ 目录下创建一个新文件。

例如,要添加提供 FFmpeg 版本 4.x 的 Jonathon F 的 PPA,您将运行:

sudo add-apt-repository ppa:jonathonf/ffmpeg-4
出现提示时按 Enter 将启用存储库。

按 [ENTER] 继续或 Ctrl-c 取消添加。
PPA 存储库公钥将是自动的