DTeam 技术日志

Doer、Delivery、Dream

国内开发资源镜像一览

Posted at — Apr 29, 2020 阅读

本页面记录我们开发环境中的使用国内镜像站加速的配置参考,便于快速配置开发环境使用的镜像站,一些相关的详细参考内容会以参考资料形式给出。

本文以全局镜像配置为主,即配置在全局的路径下,所有的项目均受影响。像 Maven, Gradle 之类的工具支持项目单独定义镜像仓库,并不在本文涉及的内容中。

镜像配置列表:

Gradle

创建目录~/.gradle/init.d(Unix/Linux 环境)或%USERPROFILE%\.gradle\init.d(Windows 环境),写入以下两个文件:

: gradle 使用镜像站下载依赖的时候可能会出现镜像站还未缓存完毕依赖,从而下载依赖的时候出错,导致 task 失败退出的情况。如果遇到这种情况,等待镜像站缓存完毕,加上--refresh-dependencies参数重新运行一次 task 即可,如:

./gradlew --refresh-dependencies build

参考资料:

Maven

创建~/.m2/settings.xml(Linux/Unix 环境)或%USERPROFILE%\.m2\settings.xml(Windows 环境)文件,写入以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <mirrors>
    <mirror>
      <id>huaweicloud</id>
      <mirrorOf>*</mirrorOf>
      <url>https://repo.huaweicloud.com/repository/maven</url>
    </mirror>
  </mirrors>
</settings>

参考资料:

Groovy Grape

创建~/.groovy/grapeConfig.xml(Linux/Unix 环境)或%USERPROFILE%\.groovy\grapeConfig.xml(Windows 环境)文件,写入以下内容:

<ivysettings>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
    <chain name="downloadGrapes" returnFirst="true">
      <filesystem name="cachedGrapes">
        <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
        <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
      </filesystem>
      <ibiblio name="localm2" root="${user.home.url}/.m2/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true"/>
      <ibiblio name="huawei" root="https://repo.huaweicloud.com/repository/maven" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>

参考资料:

nvm

配置环境变量:

参考资料:

npm

NOTE: 从 npm v9 版本开始,npm 命令限制了 npm config set 只能设置 npm 自己的 config (所有可用的设置参考官方文档: Config Settings),因此无法像旧版本一样设置镜像站了,不过你依旧可以通过编辑 ~/.npmrc 文件的方式实现旧版本 npm config set 的效果,但这不是官方推荐的方案。因此本文改用了环境变量的方式设置镜像站。环境变量对 yarn 同样受益。

配置以下环境变量(Linux/Unix 下需要 export):

# 注意如果想发布到 npmjs, 需要暂时去掉这个环境变量,或者在`package.json`中配置`publishConfig`强行指定npmjs 仓库:
# "publishConfig": {
#   "registry": "https://registry.npmjs.org/"
# },
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com
# 从npm v8 版本开始默认隐藏 install script 的输出,要像之前版本一样显示这些安装过程的输出,请设置
NPM_CONFIG_FOREGROUND_SCRIPTS=true

NODEJS_ORG_MIRROR=https://cdn.npmmirror.com/binaries/node
SASS_BINARY_SITE=https://cdn.npmmirror.com/binaries/node-sass
PHANTOMJS_CDNURL=https://cdn.npmmirror.com/binaries/phantomjs
CHROMEDRIVER_CDNURL=https://cdn.npmmirror.com/binaries/chromedriver
OPERADRIVER_CDNURL=https://cdn.npmmirror.com/binaries/operadriver
ELECTRON_MIRROR=https://cdn.npmmirror.com/binaries/electron/
PYTHON_MIRROR=https://cdn.npmmirror.com/binaries/python
ELECTRON_BUILDER_BINARIES_MIRROR=https://cdn.npmmirror.com/binaries/electron-builder-binaries/
npm_config_node_sqlite3_binary_host_mirror=https://cdn.npmmirror.com/binaries/sqlite3
PUPPETEER_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries
npm_config_sharp_binary_host=https://cdn.npmmirror.com/binaries/sharp
npm_config_sharp_libvips_binary_host=https://cdn.npmmirror.com/binaries/sharp-libvips
npm_config_canvas_binary_host_mirror=https://cdn.npmmirror.com/binaries/canvas
npm_config_argon2_binary_host_mirror=https://cdn.npmmirror.com/binaries/argon2
npm_config_better_sqlite3_binary_host_mirror=https://cdn.npmmirror.com/binaries/better-sqlite3
CYPRESS_DOWNLOAD_PATH_TEMPLATE='https://cdn.npmmirror.com/binaries/cypress/${version}/${platform}-${arch}/cypress.zip'
PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright

如果依旧使用 npm < 9 版本,您依旧可以使用以下命令设置镜像:

npm config set registry https://registry.npmmirror.com
npm config set disturl https://cdn.npmmirror.com/binaries/node
npm config set sass_binary_site https://cdn.npmmirror.com/binaries/node-sass
npm config set phantomjs_cdnurl https://cdn.npmmirror.com/binaries/phantomjs
npm config set chromedriver_cdnurl https://cdn.npmmirror.com/binaries/chromedriver
npm config set operadriver_cdnurl https://cdn.npmmirror.com/binaries/operadriver
npm config set electron_mirror https://cdn.npmmirror.com/binaries/electron/
npm config set python_mirror https://cdn.npmmirror.com/binaries/python
npm config set electron_builder_binaries_mirror https://cdn.npmmirror.com/binaries/electron-builder-binaries
npm config set node_sqlite3_binary_host_mirror https://cdn.npmmirror.com/binaries/sqlite3
npm config set puppeteer_download_host https://cdn.npmmirror.com/binaries
npm config set sharp_binary_host https://cdn.npmmirror.com/binaries/sharp
npm config set sharp_libvips_binary_host https://cdn.npmmirror.com/binaries/sharp-libvips
npm config set canvas_binary_host_mirror https://cdn.npmmirror.com/binaries/canvas
npm config set argon2_binary_host_mirror https://cdn.npmmirror.com/binaries/argon2
npm config set better_sqlite3_binary_host_mirror https://cdn.npmmirror.com/binaries/better-sqlite3
npm config set CYPRESS_DOWNLOAD_PATH_TEMPLATE 'https://cdn.npmmirror.com/binaries/cypress/${version}/${platform}-${arch}/cypress.zip'
npm config set PLAYWRIGHT_DOWNLOAD_HOST https://cdn.npmmirror.com/binaries/playwright

# 从npm v8 版本开始默认隐藏 install script 的输出,要像之前版本一样显示这些安装过程的输出,请设置
npm config set foreground-scripts true

手改~/.npmrc或设置等效的环境变量均有效。

特别注意,如果想要发布 npm 包到中央仓库,必须先取消设置registry,发布完毕之后再改回来:

# 发布中央仓库之前必须取消registry镜像设置
npm config delete registry

# 发布完毕再把registry镜像加回来
npm config set registry https://registry.npmmirror.com

参考资料:

PyPI

执行命令:

pip config set global.index-url https://mirror.sjtu.edu.cn/pypi/web/simple

参考资料:

Poetry

在项目的 pyproject.toml 中增加以下配置:

[[tool.poetry.source]]
name = "sjtug"
url = "https://mirror.sjtu.edu.cn/pypi/web/simple"
default = true

Anaconda

执行命令:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --set show_channel_urls yes

修改.condarc配置文件可以达成同样的效果,参考下方参考资料。

参考资料:

Vagrant

通过镜像站安装插件:

vagrant plugin install --plugin-clean-sources --plugin-source https://mirrors.aliyun.com/rubygems/ <plugin>...

vagrant-vbguest下载 VBoxGuestAdditions 镜像(修改Vagrantfile):

config.vbguest.iso_path = "https://mirrors.tuna.tsinghua.edu.cn/virtualbox/%{version}/VBoxGuestAdditions_%{version}.iso"

通过镜像站下载 box:

# Ubuntu镜像
vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

# Centos镜像
vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

也可以修改Vagrantfileconfig.vm.box_url = "box文件的url"

参考资料:

Rust

rustup

设置环境变量:

crates.io 索引镜像

写入配置文件~/.cargo/config:

# === cargo 版本高于 1.68 支持稀疏克隆,可以加快速度,推荐下面的配置 ===
[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
# === cargo 版本小于 1.68 则改为 replace-with = "rsproxy" ===

参考资料:

注意:如果在 cargo build 时报下面的错误:

warning: spurious network error (2 tries remaining): [6] Couldn’t resolve host name (Could not resolve host: crates)

那么,添加环境变量:CARGO_HTTP_MULTIPLEXING=false(要 export),或者在~/.cargo/config添加顶级配置http.multiplexing = false

Docker Hub

中科大和网易云等提供了公开的 Docker Hub 仓库镜像,以下配置以中科大和网易云为例。其他国内的云厂商如阿里云华为云等等都提供了 Docker Hub 镜像,需要用户自行注册账号,并且手工开通,使用是免费的。如果期望使用云厂商免费的 Docker Hub 镜像,替换下面的镜像地址为云厂商分配给用户的加速地址即可。

Linux

/etc/docker/daemon.json中加入:

{
  "registry-mirrors": [
    "https://docker.mirrors.sjtug.sjtu.edu.cn",
    "https://docker.mirrors.ustc.edu.cn/"
  ]
}

重启服务即可。

MacOS

  1. 打开 “Docker.app”
  2. 进入偏好设置页面(快捷键 ⌘, )
  3. 打开 “Daemon” 选项卡
  4. 在 “Registry mirrors” 中添加 https://docker.mirrors.sjtug.sjtu.edu.cnhttps://docker.mirrors.ustc.edu.cn/
  5. 点击下方的 “Apply & Restart” 按钮

Windows

在系统右下角托盘 Docker 图标内右键菜单选择 Settings ,打开配置窗口后左侧导航菜单选择 Daemon 。在 Registry mirrors 一栏中填写地址 https://docker.mirrors.sjtug.sjtu.edu.cnhttps://docker.mirrors.ustc.edu.cn/ ,之后点击 Apply 保存后 Docker 就会重启并应用配置的镜像地址了。

参考资料:

Podman

编辑 /etc/containers/registries.conf.d/00-unqualified-search-registries.conf 文件,写入以下内容:

unqualified-search-registries = ["docker.io"]

编辑 /etc/containers/registries.conf.d/01-registeries.conf 文件,写入以下内容:

[[registry]]
prefix = "docker.io"
location = "docker.io"

[[registry.mirror]]
prefix = "docker.io"
location = "docker.mirrors.sjtug.sjtu.edu.cn"

Julia

以下方案二选一:

参考资料:

Julia 下载镜像: Julia 二进制安装程序

github

使用Fast Git镜像,可安装此油猴脚本获取最新地址。

以 spaCy 的 en_core_web_lg model (模型没有 pip 镜像)安装为例,使用镜像下载的例子如下:

pip install https://download.fastgit.org/explosion/spacy-models/releases/download/en_core_web_lg-2.2.5/en_core_web_lg-2.2.5.tar.gz

作为对比,试试官方文档中的安装命令:

python -m spacy download en_core_web_lg

觉得有帮助的话,不妨考虑购买付费文章来支持我们 🙂 :

付费文章

友情链接