常用包管理工具镜像源配置

镜像站点介绍

镜像站 (Mirror site) 存储原始服务器内容的完整拷贝,以加速不同地理位置用户对原始资源的访问。

国内开源镜像站主要有阿里云 mirrors.aliyun.com、网易 mirrors.163.com 以及各大高校自建镜像站等。

镜像源配置

本次基于清华大学开源镜像站 TUNA 记录常用包管理工具源的配置方法。

CentOS 7 Base

首先备份 CentOS-Base.repo

1
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

修改文件 /etc/yum.repos.d/CentOS-Base.repo,写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

最后更新软件包缓存即可

1
sudo yum makecache

CentOS 7 EPEL

EPEL (Extra Packages for Enterprise Linux) 是由 Fedora Special Interest Group 维护的 Enterprise Linux (RHEL、CentOS) 中经常用到的包。

首先从 CentOS Extras 源安装 epel-release

1
sudo yum install epel-release

强制使用 TUNA 的 EPEL 镜像,可修改 /etc/yum.repos.d/epel.repo,将 baseurl 开头的行取消注释,并注释 mirrorlist 开头的行。

下面,只需要把配置文件中的 http://download.fedoraproject.org/pub 源链接替换为 https://mirrors.tuna.tsinghua.edu.cn 即可。

修改结果为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

Ubuntu 18.04 LTS

首先备份 sources.list

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

修改文件 /etc/apt/sources.list,写入

1
2
3
4
5
6
7
8
9
# 默认注释了源码镜像以提高 apt update 速度
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

Python PyPI

pip 可通过参数 -i 手动指定临时镜像源

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

将目标源设为默认

修改或新建配置文件 ~/.pip/pip.conf (Linux) 或 C:\Users\$username\pip\pip.ini (Windows) 并写入

1
2
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

R语言 CRAN 及 Bioconductor

修改或新建配置文件 ~/.Rprofile (Linux) 或 $R_HOME\etc\Rprofile.site (Windows) 并写入

1
2
options(repos="https://mirrors.tuna.tsinghua.edu.cn/CRAN")
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")

Conda

修改或新建配置文件 ~/.condarc (Linux) 或 C:\Users\$username\.condarc (Windows) 并写入

1
2
3
4
5
6
7
8
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- default
show_channel_urls: true
report_errors: false

通过修改配置文件即变更默认源为指定镜像源而无需额外参数指定。


Writing Enriches Life.