编程

告别龟速下载:用南京大学镜像站装好 PyTorch 环境

管某 9 分钟阅读

告别龟速下载:用南京大学镜像站装好 PyTorch 环境

实测于 2026-08,所有路径均已验证可用。

为什么你装 PyTorch 总是卡住

pip install torch 默认从 PyPI 官方源下载,conda 默认从 repo.anaconda.com 下载——这些服务器都在国外。torch 的 wheel 动辄 800MB~2GB,跨海传输要么慢到怀疑人生,要么直接 ReadTimeoutError

解决办法:换国内镜像源。今天的主角是南京大学开源软件镜像站(mirrors.nju.edu.cn)——国内高校镜像之一,覆盖 PyPI、Anaconda、npm、操作系统发行版等,对教育网和部分运营商线路尤其友好。

先看看 NJU 镜像站有什么(实测)

路径 状态 用途
https://mirrors.nju.edu.cn/pypi/web/simple PyPI 包镜像(torch / torchvision / torchaudio 都在)
https://mirrors.nju.edu.cn/anaconda/pkgs/main conda 默认源
https://mirrors.nju.edu.cn/anaconda/cloud/conda-forge conda-forge 源
https://mirrors.nju.edu.cn/anaconda/cloud/pytorch pytorch channel(conda 装 GPU 版的关键)
https://mirrors.nju.edu.cn/anaconda/cloud/nvidia ❌ 404 没有 nvidia channel,CUDA 依赖需走官方

方案一:pip 装 CPU 版(最快,3 条命令)

1
2
3
4
5
6
# 临时换源(只对本次生效)
pip install torch torchvision torchaudio -i https://mirrors.nju.edu.cn/pypi/web/simple

# 或者永久换源(推荐,一劳永逸)
pip config set global.index-url https://mirrors.nju.edu.cn/pypi/web/simple
pip install torch torchvision torchaudio

注意:PyPI 上 Linux 平台的 torch 是 CPU 版 wheel(不捆绑 CUDA)。没有独显、或者只是跑跑 CPU 推理/小模型,这个方案最省事。

方案二:conda 装(CPU / GPU 都能用)

1. 配置 NJU 镜像源

写入 ~/.condarc

1
2
3
4
5
6
7
8
9
10
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.nju.edu.cn/anaconda/pkgs/main
- https://mirrors.nju.edu.cn/anaconda/pkgs/r
- https://mirrors.nju.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.nju.edu.cn/anaconda/cloud
pytorch: https://mirrors.nju.edu.cn/anaconda/cloud

或直接用命令生成:

1
2
3
4
5
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.nju.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.nju.edu.cn/anaconda/pkgs/r
conda config --set custom_channels.conda-forge https://mirrors.nju.edu.cn/anaconda/cloud
conda config --set custom_channels.pytorch https://mirrors.nju.edu.cn/anaconda/cloud

2. 安装

1
2
3
4
5
6
7
# CPU 版
conda create -n pytorch python=3.11
conda activate pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch

# GPU 版(CUDA 12.1,NJU 未镜像 nvidia channel,CUDA 依赖包走官方,量小可接受)
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

提示:pytorch-cuda 等 CUDA 依赖会从官方 nvidia channel 拉取(几十 MB 级别),比 torch 本体小得多;如果实在连不上,可把 -c nvidia 换成清华源:-c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/

方案三:pip 装 GPU 版(NJU 没有 pytorch-wheels 时的替代)

NJU 镜像站没有镜像 download.pytorch.org 的 GPU wheel(该目录返回 404)。要纯 pip 装 CUDA 版 torch,两条路:

1
2
3
4
5
6
# 1) 用官方索引 + 国内 CDN 加速(部分网络有效)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

# 2) 换有 pytorch-wheels 镜像的站(清华 / 阿里云)
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple \
--extra-index-url https://mirrors.aliyun.com/pytorch-wheels/cu121

验证安装

1
python -c "import torch; print('torch', torch.__version__); print('cuda available:', torch.cuda.is_available()); print('device:', torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'CPU')"
  • 输出 cuda available: True → GPU 版装好了
  • False → 装的是 CPU 版或 CUDA 驱动/版本不匹配(见下文常见问题)

常见问题

1. pip 超时 / SSL 报错

1
2
pip install torch -i https://mirrors.nju.edu.cn/pypi/web/simple --timeout 120 --retries 5
# SSL 证书问题可加 --trusted-host mirrors.nju.edu.cn(仅在不信任证书的环境)

2. torch.cuda.is_available() 返回 False
nvidia-smi 确认驱动存在;再看 torch 版本是否匹配:conda list | grep pytorch-cuda,或重装对应 cu 版本。注意驱动向下兼容:新驱动跑旧 CUDA 没问题,反之不行。

3. conda 解析慢
首次 conda install 要解析依赖,多等 1-2 分钟属正常;可加 --channel-priority strict 减少冲突回溯。

4. 版本选择

场景 推荐
仅 CPU 推理 pip CPU 版(方案一)
深度学习训练(Linux + N 卡) conda GPU 版(方案二)
老机器 / 特定 CUDA nvidia-smi 看驱动版本,再选 cu118/cu121/cu124

国内镜像站横向对比

镜像站 PyPI conda(pytorch channel) pytorch-wheels(GPU)
南大 mirrors.nju.edu.cn
清华 TUNA
中科大 USTC
阿里云

NJU 的优势:conda 的 pytorch channel 全量镜像,教育网内网速可观,且 pypi 同步及时。需要 pip 拉 GPU wheel 的场景请搭配清华/阿里云。


一句话总结:CPU 党 pip install torch -i https://mirrors.nju.edu.cn/pypi/web/simple;GPU 党配好 .condarcconda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch——从此告别下载焦虑。