Conda更换国内镜像源
- 引言
- [一、配置 Conda 使用国内镜像源(关键!)](#一、配置 Conda 使用国内镜像源(关键!))
-
-
- [方法:修改 `.condarc` 配置文件(推荐)](#方法:修改
.condarc配置文件(推荐)) -
- [1. 打开或创建配置文件](#1. 打开或创建配置文件)
- [2. 粘贴以下 **优化后的清华源配置**(已实测加速显著):](#2. 粘贴以下 优化后的清华源配置(已实测加速显著):)
- [方法:修改 `.condarc` 配置文件(推荐)](#方法:修改
-
- 二、验证配置是否生效
- 三、测试创建环境
- 四、备选方案:如果仍慢,尝试以下优化
-
-
- [1. **优先使用 `conda-forge` channel**](#1. 优先使用
conda-forgechannel) - [2. **改用 `mamba`(强烈推荐!)**](#2. 改用
mamba(强烈推荐!)) -
- [安装 mamba:](#安装 mamba:)
- 用法(命令几乎一样):
- [1. **优先使用 `conda-forge` channel**](#1. 优先使用
-
- 五、恢复默认源(如需)
- [六、总结:让 `conda create` 变快的终极组合](#六、总结:让
conda create变快的终极组合)
引言
conda 默认使用国外官方源(anaconda.org / repo.anaconda.com) ,在国内访问速度极慢甚至超时。解决方案:更换为国内镜像源(如清华、中科大) + 优化配置。
一、配置 Conda 使用国内镜像源(关键!)
方法:修改 .condarc 配置文件(推荐)
⚠️ 不要只加
channels,还要关闭默认的defaults源 并启用 strict channel priority,否则仍会回退到国外源!
1. 打开或创建配置文件
- Windows 路径 :
C:\Users\<你的用户名>\.condarc - WSL / Linux 路径 :
~/.condarc
2. 粘贴以下 优化后的清华源配置(已实测加速显著):
yaml
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# 关闭默认的 defaults 源(非常重要!)
default_channels: []
# 启用 strict channel priority,避免混源
channel_priority: strict
# 显示 channel 地址(方便调试)
show_channel_urls: true
# 超时时间(秒)
remote_read_timeout_secs: 120
💡 为什么有效?
- 移除了
defaults(即repo.anaconda.com),强制走镜像channel_priority: strict防止 conda 自动回退到慢速源- 清华源同步及时,支持
main、free、conda-forge、pytorch等主流 channel
二、验证配置是否生效
运行:
bash
conda config --show channels
正确输出应只有你配置的镜像地址 ,没有 defaults 或 https://repo.anaconda.com/...
如果看到 defaults,说明配置未生效,请检查 .condarc 文件格式(YAML 对缩进敏感)。
三、测试创建环境
bash
# 示例:创建一个 Python 3.10 环境
conda create -n testenv python=3.10 -y
🚀 实测对比:
- 原始速度:5~10 分钟(甚至失败)
- 配置后:30 秒 ~ 2 分钟(取决于包大小)
四、备选方案:如果仍慢,尝试以下优化
1. 优先使用 conda-forge channel
很多包在 conda-forge 更新更快、镜像更全。创建环境时显式指定:
bash
conda create -n myenv -c conda-forge python=3.10 numpy pandas
2. 改用 mamba(强烈推荐!)
mamba 是 conda 的超快替代品,用 C++ 重写,解析依赖速度提升 10~100 倍!
安装 mamba:
bash
# 先用 conda 装一次(之后都用 mamba)
conda install mamba -c conda-forge
用法(命令几乎一样):
bash
mamba create -n myenv python=3.10 pytorch torchvision -c pytorch
💡 在国内配合清华源,
mamba create通常 10~30 秒完成!
五、恢复默认源(如需)
删除 .condarc 文件,或运行:
bash
conda config --remove-key channels
conda config --remove-key default_channels
conda config --remove-key channel_priority
六、总结:让 conda create 变快的终极组合
| 措施 | 效果 |
|---|---|
| ✅ 配置清华/中科大镜像 + 移除 defaults | 解决网络慢 |
✅ 设置 channel_priority: strict |
防止回退到国外源 |
✅ 使用 mamba 替代 conda |
解决依赖解析慢 |
✅ 优先从 conda-forge 安装 |
包更新快、兼容性好 |
