pkg install 下载速度慢怎么解决?(Termux 换源指南)

2024-07-23 Linux 运维

Termux 是 Android 平台上最受欢迎的终端模拟器和 Linux 环境,让手机也能拥有类似 Linux 的包管理体验。pkg 是 Termux 的包管理工具(底层实际调用 apt),但很多用户在首次使用时会发现下载速度极慢,一个几十 MB 的包可能要等十分钟以上。

本文将深入分析原因并提供一整套解决方案。


问题原因:为什么 pkg install 这么慢?

默认情况下,Termux 的软件源指向国外服务器:

# 默认 sources.list 中的源地址(位于美国/欧洲)
deb https://packages.termux.dev/apt/termux-main stable main

国内用户访问这些服务器时:

  • 延迟高:物理距离远,RTT 通常在 200-400ms
  • 带宽受限:国际出口带宽有限,高峰期尤其严重
  • 丢包率高:跨国链路不稳定

通常速度只有 10-50 KB/s,而换到国内镜像后可达 2-10 MB/s,提升 50-200 倍。


解决方案一:termux-change-repo(最简单,推荐)

Termux 官方提供了交互式换源工具:

# 如果尚未安装 termux-tools
pkg install termux-tools

# 运行换源向导
termux-change-repo

运行后会显示一个交互界面:

[ ] Mirrors by BFSU               # 北京外国语大学
[ ] Mirrors by BIT                # 北京理工大学
[ ] Mirrors by NJU                # 南京大学
[ ] Mirrors by USTC               # 中国科学技术大学
[ ] Mirrors by Tsinghua University # 清华大学
[ ] Mirrors by Alibaba Cloud      # 阿里云
[ ] Mirrors by Tencent Cloud      # 腾讯云
[ ] Mirrors by Netease            # 网易
[ ] Default repositories          # 默认源(国外)

操作步骤:

  1. 按空格键选中 1-2 个国内镜像(推荐清华和中科大)
  2. 按回车确认
  3. 自动替换 sources.list 并更新索引

提示:选择离你地理位置最近的镜像通常效果最好。北方用户推荐清华/北外/北理,南方用户推荐中科大/阿里云。


解决方案二:手动编辑 sources.list

如果你更喜欢手动控制,直接编辑源列表文件:

1. 备份原有配置

cp $PREFIX/etc/apt/sources.list $PREFIX/etc/apt/sources.list.bak

2. 编辑 sources.list

# 使用 nano 或 vi 编辑
nano $PREFIX/etc/apt/sources.list

3. 替换为国内镜像源

清华大学镜像源(推荐):

# 主仓库
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main

# x11 仓库(如果使用图形界面)
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-x11 x11 main

# root 仓库(如果使用 root 功能)
deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-root root stable

中国科学技术大学镜像源:

# 主仓库
deb https://mirrors.ustc.edu.cn/termux/apt/termux-main stable main

# x11 仓库
deb https://mirrors.ustc.edu.cn/termux/apt/termux-x11 x11 main

# root 仓库
deb https://mirrors.ustc.edu.cn/termux/apt/termux-root root stable

阿里云镜像源:

deb https://mirrors.aliyun.com/termux/apt/termux-main stable main

4. 更新索引

pkg update
# 或
apt update && apt upgrade

解决方案三:配置代理

如果你有 HTTP/SOCKS5 代理可用,可以让 Termux 通过代理下载。

HTTP 代理

# 临时设置(当前会话有效)
export http_proxy="http://192.168.1.100:7890"
export https_proxy="http://192.168.1.100:7890"

# 永久设置(写入 bashrc)
echo 'export http_proxy="http://192.168.1.100:7890"' >> ~/.bashrc
echo 'export https_proxy="http://192.168.1.100:7890"' >> ~/.bashrc
source ~/.bashrc

apt 专用代理配置

# 创建 apt 代理配置文件
echo 'Acquire::http::Proxy "http://192.168.1.100:7890";' > $PREFIX/etc/apt/apt.conf.d/99proxy
echo 'Acquire::https::Proxy "http://192.168.1.100:7890";' >> $PREFIX/etc/apt/apt.conf.d/99proxy

常见代理方案对比

代理方案 配置复杂度 速度 适用场景
Clash for Android ⭐⭐ 快 有订阅链接的用户
v2rayNG ⭐⭐ 快 V2Ray/Xray 节点
Termux 内跑 clash ⭐⭐⭐⭐ 快 无其他代理时
SSH 隧道 ⭐⭐⭐ 中 有 VPS 的用户
直接换源 ⭐ 快 首选方案,无需额外工具

解决方案四:使用国内 CDN 加速(F-Droid 版本)

如果你是通过 F-Droid 安装的 Termux,F-Droid 版本支持使用国内 CDN 加速。

配置 F-Droid 镜像

# Termux 的 bootstrap 包可能也需要从国内源下载
# 编辑 $PREFIX/etc/apt/sources.list.d/ 下的其他源文件

# 例如 game 仓库和 science 仓库
echo 'deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-games games stable' > \
    $PREFIX/etc/apt/sources.list.d/game.list

echo 'deb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-science science stable' > \
    $PREFIX/etc/apt/sources.list.d/science.list

解决方案五:多线程下载工具加速

pkg/apt 默认是单线程下载,可以利用 axel 或 aria2 实现多线程:

使用 apt-fast(多线程 apt 封装)

# 安装依赖
pkg install aria2

# 安装 apt-fast
pkg install apt-fast

# 之后用 apt-fast 替代 apt
apt-fast update
apt-fast install python

apt-fast 配置选项

# 编辑 apt-fast 配置
nano $PREFIX/etc/apt-fast.conf

# 关键配置项:
MIRRORS=('https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main')
DOWNLOADER='aria2c'  # 使用 aria2 多线程下载
_MAXNUM=16            # 最大连接数
_MAXCONPERSRV=8       # 每服务器最大连接数
_SPLITCON=8           # 分片数

速度对比实测(下载 python 包,约 50MB)

方法 耗时 速度
默认源 + pkg ~8 分钟 ~100 KB/s
清华镜像 + pkg ~12 秒 ~4 MB/s
中科大镜像 + pkg ~10 秒 ~5 MB/s
阿里云镜像 + pkg ~15 秒 ~3.3 MB/s
清华镜像 + apt-fast ~5 秒 ~10 MB/s

测试环境:Wi-Fi 6,中国移动 500M 宽带,广州地区。结果仅供参考。


类似思路:npm / pip 换源

解决 pkg install 速度问题后,你很可能在 Termux 中安装 npm 或 pip 包时也会遇到同样的慢速问题。换源思路完全一致:

npm 换源

# 查看当前源
npm config get registry

# 换为淘宝镜像
npm config set registry https://registry.npmmirror.com

# 或者使用 nrm 管理多个源
npm install -g nrm
nrm use taobao

pip 换源

# 临时使用镜像
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

# 永久换源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 或编辑 ~/.config/pip/pip.conf
cat > ~/.config/pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

国内外包管理镜像汇总

包管理器 默认源 推荐镜像
pkg/apt (Termux) packages.termux.dev mirrors.tuna.tsinghua.edu.cn
npm registry.npmjs.org registry.npmmirror.com
pip pypi.org pypi.tuna.tsinghua.edu.cn
gem rubygems.org gems.ruby-china.com
cargo crates.io mirrors.tuna.tsinghua.edu.cn

常见问题 FAQ

Q1:换源后 pkg update 报签名错误?

# 清除缓存后重试
pkg clean
pkg update

# 如果仍有问题,尝试
apt update --allow-unauthenticated

Q2:清华镜像连不上?

中科大、阿里云、北外、南大等镜像都可以作为备选。建议在 sources.list 中配置多个源,apt 会自动 fallback。

Q3:Termux 从 Google Play 安装的和从 F-Droid 安装的有区别吗?

有区别。Play 商店版本更新缓慢且因 Google 政策受限,强烈推荐从 F-Droid 安装。

Q4:换源后还需要其他配置吗?

不需要。pkg update 后即可享受加速效果。建议再运行一次 pkg upgrade 确保所有包都是最新版本。


总结

方案 难度 效果 推荐度
termux-change-repo ⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
手动编辑 sources.list ⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐
配置代理 ⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐
apt-fast 多线程 ⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐
F-Droid 版本 + CDN ⭐ ⭐⭐⭐⭐ ⭐⭐⭐⭐

一句话总结:打开 Termux,跑 termux-change-repo,选清华或中科大镜像,问题解决。这是最简单、最有效、最推荐的方案。