在使用 Ubuntu 及 Python 等的过程中,会用到各种包管理软件,比如 apt (Advanced Packaging Tool),pip (Package Installer for Python),conda等。但是这些包管理软件的服务器大部分都在国外,下载速度极慢,并且经常出现下载中断的情况。要知道使用这些包管理工具下载时,如果下载中断了,那可是要从头再来的,所以国内有些机构或公司为这些开源服务建立了国内镜像源,最广为人知的就是清华大学的开源软件镜像站了。
更换了国内镜像源后,软件下载速度得到了极大地提升,使用体验度飙升。下面就总结一些 Ubuntu 中常用的开源镜像及其更换方法。
conda
conda 是一个为 Python 创建的 开源软件包管理系统 和 环境管理系统 ,用于安装多个版本的软件包及其依赖关系,并可以实现各个环境之间的轻松切换。
清华源
批量添加
各个操作系统(Ubuntu、Windows 等)都可以通过更改用户目录下的 .condarc
文件来配置国内源,此处只介绍 Ubuntu 系统下的更换方法。在 Terminal 中执行如下命令来编辑 .condarc
文件:
sudo vim ~/.condarc
在文件中输入如下内容:
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
执行 :wq
保存并退出。然后运行 conda clean -i
清除索引缓存,保证用的是镜像站提供的索引。最后,运行 conda config --show-sources
测试一下是否安装成功,如果正确输出了上面的 .condarc
文件内容,则安装成功。上述过程执行完毕后,即可使用 Anaconda Python 免费仓库国内源。
单独添加
如果想单独添加 conda 某个镜像源,可以使用如下命令:
conda config --add channels url
# url为要添加的镜像源地址,例如:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
通过该方式添加的源可以通过下面的命令移除来恢复默认源(默认源即为 .condarc
中直接编辑的源):
conda config --remove-key channels
Reference
pip
pip (package installer for python) 是一个通用的 Python 包安装与管理工具,提供了对 Python 包的查找、下载、安装、卸载的功能。
清华源
临时设置
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package-name
设为默认
-
直接在 Terminal 中操作修改默认源:
# 需要先升级 pip 到最新的版本 (>=10.0.0) 后进行配置: pip install pip -U # 如果到 pip 默认源的网络连接较差,临时使用清华镜像站来升级 pip : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U # 设置清华源为默认源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
-
此外,还可以通过修改
~/.pip/pip.conf
文件来设置默认源:[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn
其他国内源
阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/