【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 安装 包更新快、兼容性好
相关推荐
烟锁池塘柳06 天前
Conda 创建环境报错:Unable to read repodata JSON file,清华源 pkgs/free 失效解决方法
json·conda
veminhe6 天前
conda使用
python·conda
方与圆之帆10 天前
conda 管理不同版本的软件包
conda·gdal·osm
数智工坊12 天前
Conda 环境 Docker 标准化迁移完整方案,适配深度学习 GPU 训练
深度学习·docker·conda
软糖姐姐14 天前
Python开篇——简介、pip和conda
python·conda·pip·数据科学·
puamac14 天前
Conda + CUDA 环境安装与使用完全指南
conda
叶 落15 天前
Mac 通过 Miniconda 安装 Python
python·macos·conda·miniconda
冰心孤城16 天前
Conda、Anaconda、Miniconda对比分析
conda
不明觉厉二十年16 天前
uv Windows配置
conda·pip
心念枕惊17 天前
Conda 环境一键搬家:用 conda-pack 打包带走,连网都不用
conda·neo4j