前言

很多开发工具和包管理器的官方源在海外,在国内使用时下载速度可能只有几十 KB/s,严重影响效率。这篇文章汇总了常用软件的镜像源配置方法。

先说明:配置完源之后记得执行对应的更新命令(apt updatebrew update 等),让新源立即生效。


一、Ubuntu / Debian(apt)

自动配置(推荐)

针对 Ubuntu 22.04 及以下版本(传统格式)

/etc/apt/sources.list 中,把官方源替换为镜像源域名。

1
2
3
4
5
6
7
8
9
# 先备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换为清华源
sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list
sudo sed -i 's@//.*security.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list

# 更新
sudo apt update

针对 Ubuntu 24.04 LTS 及以上版本(新版 DEB822 格式)

Ubuntu 24.04 (Noble Numbat) 开始,APT 软件源配置默认改为了新的 DEB822 格式,配置文件路径移到了 /etc/apt/sources.list.d/ubuntu.sources,原来的 /etc/apt/sources.list 默认已被废弃并留空。

1
2
3
4
5
6
7
8
9
# 先备份新版配置文件
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

# 使用 sed 批量替换默认的官方源
sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources
sudo sed -i 's@//.*security.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources

# 更新缓存
sudo apt update

注意其他常见版本的代号对照:

  • Ubuntu 20.04:focal
  • Ubuntu 22.04:jammy
  • Ubuntu 24.04:noble

不确定系统版本的话,可以通过 lsb_release -c 来查看当前系统代号。如果不是 Ubuntu 而是 Debian,换源方式一样,但配置文件的 URL 路径略有不同。

手动配置(清华源)

1. Ubuntu 22.04 (传统格式,写入 /etc/apt/sources.list)

1
2
3
4
5
6
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF

2. Ubuntu 24.04 (DEB822 格式,写入 /etc/apt/sources.list.d/ubuntu.sources)

1
2
3
4
5
6
7
sudo tee /etc/apt/sources.list.d/ubuntu.sources << 'EOF'
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
Suites: noble noble-updates noble-backports noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF

国内常用 apt 镜像站

镜像站 地址
清华 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
阿里云 https://mirrors.aliyun.com/ubuntu/
中科大 https://mirrors.ustc.edu.cn/ubuntu/
华为云 https://repo.huaweicloud.com/ubuntu/
腾讯云 https://mirrors.cloud.tencent.com/ubuntu/

CentOS / RHEL / 麒麟 V10(yum / dnf)

1
2
3
4
5
6
7
8
# 阿里云 CentOS 源(CentOS 7)
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo \
https://mirrors.aliyun.com/repo/Centos-7.repo

# 华为云 openEuler 源(麒麟 V10 服务器版也适用)
sudo sed -i 's|repo.openeuler.org|repo.huaweicloud.com/openeuler|g' \
/etc/yum.repos.d/openEuler.repo
sudo dnf makecache

二、Docker

Docker Hub 镜像加速(最新现状与对策)

[!WARNING]
重要局势说明:自 2024 年中起,由于网络调整,国内几乎所有公开的公共 Docker Hub 镜像加速站(如各大高校源、各大云厂商公开源以及各种旧社区源)都已大面积失效或无法访问。目前直接配置公用 registry-mirrors 的方法失效频率极高。

目前依然生效或推荐的长期解决方案如下:

方案一:利用 GitHub 实时监测的活跃镜像列表

开源社区在 GitHub 上有许多持续追踪并每日测试最新可用 Docker 镜像代理的仓库。你可以去这些仓库查找最新公布的有效域名,并更新到 /etc/docker/daemon.json 中。

  • 常用活跃监测站:dockerpull.org 或在 GitHub 搜索 docker mirror china

编辑 /etc/docker/daemon.json 示例(写入当前仍活跃的第三方加速域名):

1
2
3
4
5
6
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.xuanyuan.me"
]
}
1
2
3
4
# 重载配置并重启 Docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info | grep -A5 "Registry Mirrors" # 验证是否生效

方案二:自建 Cloudflare Workers Docker 代理(最稳定,推荐)

如果你拥有个人的域名和 Cloudflare 账号,自建专属私有代理镜像是目前最稳定、完全不受公用源关停影响的终极方案。

  1. 创建 CF Worker:登录 Cloudflare,创建一个新的 Worker。
  2. 部署代理脚本:在 Worker 代码中部署开源的 Docker 代理脚本(GitHub 搜索 cloudflare-docker-proxy 即可一键部署)。
  3. 绑定自定义域名:在 Cloudflare 中为该 Worker 绑定你的个人自定义域名(国内可直连的域名)。
  4. 配置 Docker:将你绑定的自定义域名(如 https://docker.yourdomain.com)配置到上述 /etc/docker/daemon.jsonregistry-mirrors 数组中即可。

Docker 镜像拉取备用方案

如果不想修改全局 daemon.json,可以直接通过目前依然存活的第三方中转代理拉取,然后再通过 docker tag 还原镜像原名:

1
2
3
4
5
6
7
# 方式1:通过 1panel 开源社区中转站拉取
docker pull docker.1panel.live/library/nginx:latest
docker tag docker.1panel.live/library/nginx:latest nginx:latest

# 方式2:使用其他存活的中转代拉站
docker pull docker.1ms.run/library/nginx:latest
docker tag docker.1ms.run/library/nginx:latest nginx:latest

三、pip(Python)

临时使用

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

永久配置

1
2
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 这条命令会写入 ~/.config/pip/pip.conf(Linux)或 %APPDATA%\pip\pip.ini(Windows)

可选的国内源

地址
清华 https://pypi.tuna.tsinghua.edu.cn/simple
阿里云 https://mirrors.aliyun.com/pypi/simple/
中科大 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 https://pypi.douban.com/simple/
华为云 https://repo.huaweicloud.com/repository/pypi/simple

配合 conda 使用

1
2
3
4
5
6
7
8
9
# conda 本身换源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

# conda 环境中用 pip 安装时也会读取上面配置的 pip 镜像源
conda create -n myenv python=3.11 -y
conda activate myenv
pip install numpy # 自动走清华源

四、npm / yarn / pnpm(Node.js)

npm

1
2
3
4
5
6
7
8
9
10
11
# 永久配置
npm config set registry https://registry.npmmirror.com

# 验证
npm config get registry

# 临时使用
npm install --registry=https://registry.npmmirror.com

# 恢复官方源
npm config set registry https://registry.npmjs.org

yarn

1
yarn config set registry https://registry.npmmirror.com

pnpm

1
pnpm config set registry https://registry.npmmirror.com

可选源

地址
npmmirror(原淘宝镜像) https://registry.npmmirror.com
华为云 https://repo.huaweicloud.com/repository/npm/
腾讯云 https://mirrors.cloud.tencent.com/npm/

配合 nrm 快速切换

1
2
3
4
npm install -g nrm
nrm ls # 列出可用源和延迟
nrm use taobao # 切换到 npmmirror
nrm test # 测速

五、Maven / Gradle(Java)

Maven

编辑 ~/.m2/settings.xml(没有就创建):

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>

Maven 可选源

地址
阿里云 https://maven.aliyun.com/repository/public
华为云 https://repo.huaweicloud.com/repository/maven/
腾讯云 https://mirrors.cloud.tencent.com/nexus/repository/maven-public/

Gradle

在项目的 build.gradle 中把 repositories 部分的 mavenCentral() 替换为:

1
2
3
4
5
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/spring' }
google()
}

Gradle 全局配置可以修改 ~/.gradle/init.gradle,对所有项目生效,但一般按项目配置就够了。


六、Homebrew(macOS / Linux)

Homebrew 在国内慢的根源主要有两个方面:一是拉取 Formula(软件配方描述 JSON)所涉及的 Homebrew API 请求;二是下载预编译二进制包(Bottles)所使用的资源地址。

[!NOTE]
关于 Homebrew 4.0+ 的重大变动:自 4.0 版本起,Homebrew 默认开启了 API Mode。它不再在本地 Git 克隆巨大的 homebrew-corehomebrew-cask 仓库,而是直接调用官方 API 下载编译好的 JSON 索引。
因此,旧版教程中通过 git -C "$(brew --repo homebrew/core)" 的换源命令在现代全新安装的 Homebrew 上会报 目录不存在 的错误。

1. 现代化换源方案(针对 Homebrew 4.0+,最推荐)

针对开启了 API Mode(默认)的现代 Homebrew,仅需设置环境变量即可完美加速:

将以下变量写入你的 Shell 配置文件(如 ~/.zshrc~/.bashrc):

1
2
3
4
5
6
7
8
# 替换 Homebrew API 源 (下载 Formula JSON 描述)
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"

# 替换 Homebrew Bottles 预编译二进制包源
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

# 替换 brew.git 本体远程仓库地址(仅当更新 brew 主程序慢时选配)
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"

写入后,运行 source ~/.zshrc(或对应配置文件)使其立即生效。


2. 传统 Git 克隆模式换源(仅当禁用了 API Mode 时需要)

如果你主动配置了 HOMEBREW_NO_INSTALL_FROM_API=1 禁用了 API 模式,且完整克隆了 core 仓库,才需要通过 Git 命令进行源替换:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 替换 brew.git 本体
git -C "$(brew --repo)" remote set-url origin \
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换核心仓库 (若目录存在)
git -C "$(brew --repo homebrew/core)" remote set-url origin \
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换 Cask 仓库 (若目录存在)
git -C "$(brew --repo homebrew/cask)" remote set-url origin \
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 替换后执行更新
brew update

3. 安装 Homebrew 时使用国内加速脚本

如果你尚未安装 Homebrew,由于网络限制,使用官方脚本很难成功。强烈推荐使用国内开发者维护的一键极速安装脚本(支持自动选择并配置国内主流镜像源,极速省心):

1
/bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"

七、Cargo(Rust)

1
2
3
4
5
6
7
8
9
10
11
12
# 编辑 ~/.cargo/config.toml,没有就创建
mkdir -p ~/.cargo
cat > ~/.cargo/config.toml << 'EOF'
[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"
EOF

八、Composer(PHP)

1
2
3
4
5
6
7
8
# 全局配置
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

# 项目级配置(只影响当前项目)
composer config repo.packagist composer https://mirrors.aliyun.com/composer/

# 恢复官方源
composer config -g --unset repos.packagist

九、RubyGems(Ruby)

1
2
3
4
5
6
7
8
9
# 查看当前源
gem sources -l

# 移除官方源,换成国内源
gem sources --remove https://rubygems.org/
gem sources --add https://gems.ruby-china.com/

# Bundler 也可配置镜像
bundle config mirror.https://rubygems.org https://gems.ruby-china.com

十、Go Modules

1
2
3
4
5
6
7
8
9
10
# 设置 GOPROXY
go env -w GOPROXY=https://goproxy.cn,direct

# 其他可选
# 阿里云: https://mirrors.aliyun.com/goproxy/
# 七牛: https://goproxy.cn
# 官方: https://proxy.golang.org

# 关闭代理(直连)
go env -w GOPROXY=direct

十一、HuggingFace(模型下载)

1
2
3
4
5
6
7
8
9
10
# 环境变量方式(推荐)
export HF_ENDPOINT=https://hf-mirror.com

# 配合 hf_transfer 加速下载
pip install hf-transfer
export HF_HUB_ENABLE_HF_TRANSFER=1

# 在 Python 中设置
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

十二、GitHub 加速

git clone 加速

1
2
3
4
5
6
7
8
9
10
11
12
13
# 方式1:用镜像站
git clone https://ghproxy.com/https://github.com/user/repo.git

# 方式2:用 gitclone.com
git clone https://gitclone.com/github.com/user/repo.git

# 方式3:配置 git 全局代理(如果你有梯子)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

GitHub Releases 下载

1
2
# 通过镜像下载
wget https://ghproxy.com/https://github.com/user/repo/releases/download/v1.0/file.tar.gz

常用源速查表

软件 配置命令
apt (Ubuntu) sudo sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
Docker /etc/docker/daemon.json 中配置 registry-mirrors
pip pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
npm npm config set registry https://registry.npmmirror.com
Maven ~/.m2/settings.xml 添加阿里云 mirror
Homebrew 替换 brew 和 core 的 git remote
Cargo ~/.cargo/config.toml 中加入中科大镜像
Composer composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
RubyGems gem sources --add https://gems.ruby-china.com/
Go go env -w GOPROXY=https://goproxy.cn,direct
HuggingFace export HF_ENDPOINT=https://hf-mirror.com
GitHub git clone https://ghproxy.com/https://github.com/...

这篇文章列举的源地址在写的时候都是有效的,但镜像站偶尔会变更域名或下线,如果发现某个源失效了,搜”软件名 + 国内镜像 + 当前年份”通常能找到最新的可用地址。