【Conda】Conda更换国内镜像源

Conda更换国内镜像源

  • 引言
  • [一、配置 Conda 使用国内镜像源(关键!)](#一、配置 Conda 使用国内镜像源(关键!))
      • [方法:修改 `.condarc` 配置文件(推荐)](#方法:修改 .condarc 配置文件(推荐))
        • [1. 打开或创建配置文件](#1. 打开或创建配置文件)
        • [2. 粘贴以下 **优化后的清华源配置**(已实测加速显著):](#2. 粘贴以下 优化后的清华源配置(已实测加速显著):)
  • 二、验证配置是否生效
  • 三、测试创建环境
  • 四、备选方案:如果仍慢,尝试以下优化
      • [1. **优先使用 `conda-forge` channel**](#1. 优先使用 conda-forge channel)
      • [2. **改用 `mamba`(强烈推荐!)**](#2. 改用 mamba(强烈推荐!))
  • 五、恢复默认源(如需)
  • [六、总结:让 `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 自动回退到慢速源
  • 清华源同步及时,支持 mainfreeconda-forgepytorch 等主流 channel

二、验证配置是否生效

运行:

bash 复制代码
conda config --show channels

正确输出应只有你配置的镜像地址没有 defaultshttps://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 安装 包更新快、兼容性好
相关推荐
autho2 小时前
conda
linux·python·conda
weixin_421585015 小时前
系统说明用conda新建虚拟环境,以及后续管理,删除,并在pycharm中引用
ide·pycharm·conda
乘风对月歌2 天前
conda 或 vscode 中没有的虚拟环境
ide·vscode·conda
比奇堡的猪猪2 天前
修改默认conda环境(在win中)
python·conda
视图猿人3 天前
Conda的安装和使用-jupyter notebook
conda
itwangyang5204 天前
Windows + Conda + OpenMM GPU(CUDA)完整安装教程-50显卡系列
人工智能·windows·python·conda
小杜今天学AI了吗4 天前
如何配置 linux 系统的conda 环境
linux·运维·conda
gravity_w5 天前
Conda常用命令总结
经验分享·笔记·conda
雪风飞舞6 天前
conda 常用命令
linux·windows·conda