【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 自动回退到慢速源
  • 清华源同步及时,支持 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 安装 包更新快、兼容性好
相关推荐
饭来_4 天前
Ubuntu 中出现 CondaToSNonInteractiveError 的解决方法
ubuntu·conda·debug
emo_2b6 天前
Pycharm配置conda解释器时遇到的问题及解决方法
linux·pycharm·conda
Είναι η κοπέλα8 天前
llama.cpp 与 GGUF 格式:本地大模型的“裸引擎“
开发语言·人工智能·pytorch·python·conda
Είναι η κοπέλα10 天前
显存计算与模型选择:你的显卡能跑多大的模型
人工智能·pytorch·python·开源·conda
天蓝蓝的本我10 天前
conda日常用到的命令积累1-登录linux,安装pycharm
linux·pycharm·conda
Είναι η κοπέλα12 天前
Python 虚拟环境:venv、conda 与 Python 版本选择全解
开发语言·python·conda
夏天拐跑了西瓜14 天前
一文入门LangChain:从框架认知到构建你的第一个AI Agent
python·langchain·conda·agent
weixin_4617694014 天前
机械臂SDK运行日志
conda·sdk
三点水-here16 天前
从 Miniconda 迁移到 Miniforge3 全记录:安装、配置、环境迁移与排错实录
python·conda
MI_farmer17 天前
Anaconda 安装在 D 盘但虚拟环境跑到 C 盘,以及 Conda PermissionError: WinError 5 的解决方法
c语言·开发语言·conda