跳至主要內容

换源

大约 7 分钟

换源


pip

源地址

使用方法

方法一:临时使用

直接在 pip 后加-i 后跟这次使用的源即可,例:

pip install web.py -i https://mirrors.aliyun.com/pypi/simple/

指令中的网址为上方的源地址。

如果出现带有trusted-host字样的报错,这是由源不为 https 协议导致的,使用:

pip install web.py -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

添加信任主机即可。

方法二:更改默认源

创建或修改配置文件(一般都是创建)

  • linux 与 mac 的设置的文件在 ~/.pip/pip.conf

  • vim ~/.pip/pip.conf
    
  • windows 在 %HOMEPATH%\pip\pip.ini

  • 如果没有创建即可。

使用 pip config set global.index-url xxxx 来设置 xxxx 源也可

更改内容:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com

文件中的网址为上方的源地址。 刚刚下面的内容是 http 协议源的实例。需要添加信任。 保存退出即可。

方法三:python 代码更改安装源

临时使用其他源安装软件包的 python 脚本如下:

#!/usr/bin/python

import os

package = raw_input("Please input the package which you want to install!\n")
command = "pip install %s -i https://mirrors.aliyun.com/pypi/simple/" % package
# http源的代码实例如下
# command = "pip install %s -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn" % package
os.system(command)

npm

源地址

使用方法

请注意,使用镜像库均不能 publish,如需 publish 需要换回官方库

方法一:使用 cnpm 替代

安装 cnpm:

npm install -g cnpm --registry=https://registry.npm.taobao.org

之后使用 cnpm 替代 npm 命令即可。支持除 publish 外所有命令:

cnpm install [name]

官方网址:http://npm.taobao.org/open in new window

方法二:临时替换

在执行 npm 命令时指定参数 registry 即可:

npm --registry https://registry.npm.taobao.org install express

方法三:持久使用

在命令行中执行:

npm config set registry https://registry.npm.taobao.org

或者~/.npmrc中添加

registry = https://registry.npm.taobao.org

(执行vim ~/.npmrc即可更改.npmrc内容)

查看更改是否生效:

npm config get registry

或者

npm info express

yarn


查询与更换源

yarn config get registry

yarn config set registry https://registry.npm.taobao.org/

默认源:

https://registry.npmjs.org/
yarn config set registry https://registry.npmjs.org/

其他源

# 华为
https://repo.huaweicloud.com/yarn/
yarn config set registry https://repo.huaweicloud.com/

安装依赖

  • 下载所有依赖:
    yarn install
    
  • 添加一个依赖:
    yarn add axios
    

brew

<h2 id="brew">brew</h2>

说明

brew 为 macOS 中的软件包管理工具,相当于 linux 中的 apt、yum。可以用 brew 下载命令行程序,brew cask 下载编译完成的二进制 GUI 程序。
但是国内速度感人,因此我们要替换成国内源。

方法

替换国内源要替换几个地址。

  • homebrew-core.git 库地址。是 brew 的主程序地址
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

其中**https://mirrors.ustc.edu.cn/brew.git**为源地址。我们这里使用的是中科大的源作为演示。

  • brew.git 库地址。记录更新和安装脚本。
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

其中https://mirrors.ustc.edu.cn/homebrew-core.git为源地址。

  • 二进制库 Homebrew Bottles 源
    • bash 用户:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

其中https://mirrors.ustc.edu.cn/homebrew-bottles为源地址。

* zsh用户:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

其中https://mirrors.ustc.edu.cn/homebrew-bottles为源地址。

  • 都替换完成后执行brew update即可。

源地址

官方源

类型地址
brew.githttps://github.com/Homebrew/brew.gitopen in new window
homebrew-core.githttps://github.com/Homebrew/homebrew-core.gitopen in new window
Homebrew Bottles/

中科大源

类型地址
brew.githttps://mirrors.ustc.edu.cn/brew.gitopen in new window
homebrew-core.githttps://mirrors.ustc.edu.cn/homebrew-core.gitopen in new window
Homebrew Bottleshttps://mirrors.ustc.edu.cn/homebrew-bottlesopen in new window

清华源

类型地址
brew.githttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.gitopen in new window
homebrew-core.githttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.gitopen in new window
homebrew-science.githttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-science.gitopen in new window
homebrew-python.githttps://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-python.gitopen in new window
Homebrew Bottleshttps://mirrors.tuna.tsinghua.edu.cn/homebrew-bottlesopen in new window

docker

<h2 id="docker">docker</h2>

概述

docker 的国外源的速度感人,为了避免影响生产效率,建议更换为国内源。
一般国内源比较常用的是阿里源和 DAO 源。
由于使用镜像需要注册相应的账号,每个人获得的镜像链接也不相同。 因此在这里我们不提供具体的方法和链接,但是给出镜像源官方的指导链接,大家可以按照官方指导的步骤操作。
值得注意的是, mac 上有两种 Docker,依赖的技术不一样,一个可以使用命令改更改(老版 Docker Toolbox 依赖 VirtualBox) ,另一个建议使用 GUI 修改(新版 Docker for mac 依赖 HyperKit) 。
Linux 的修改方式与 Docker Toolbox 相同。

阿里源

DaoCloud

<h2 id="yum">yum</h2>

源地址

使用方法


CentOS

首先备份 CentOS-Base.repo

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

之后启用 TUNA 软件仓库, 将以下内容写入 /etc/yum.repos.d/CentOS-Base.repo

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

ubuntu

打开 /etc/docker/daemon.json 并输入

{
  "registry-mirrors": [
    "https://mirrors.tuna.tsinghua.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://ustc-edu-cn.mirror.aliyuncs.com",
    "https://ghcr.io",
    "https://mirror.baidubce.com"
  ]
}

然后重启 docker

service docker restart

<h2 id="thanks-to">参考 / 感谢</h2>
<h2 id="contributor">贡献者</h2>

dockerHub

eryajf/Thanks-Mirror: 整理记录各个包管理器,系统镜像,以及常用软件的好用镜像,Thanks Mirror。 走过路过,如觉不错,麻烦点个赞 👆🌟 (github.com)open in new window

Docker Hub 源使用帮助 — USTC Mirror Help 文档open in new window

解决 dockerhub 下载镜像慢问题_七个披萨的博客-CSDN 博客_dockerhub 太慢open in new window
Docker 国内镜像 - 知乎 (zhihu.com)open in new window
超详细,带你玩转 Docker 镜像、Docker Hub 仓库、镜像的查看、标签、拉取、查找_董哥的黑板报的博客-CSDN 博客_docker 指定仓库搜索open in new window
突破 DockerHub 限制,全镜像加速服务 - 掘金 (juejin.cn)open in new window

PS: 鉴于 Docker Hub 的特殊性国内没有一家将之全站镜像的, 所以如果镜像拉不下来仓库就换回官方源拉下吧

不过关于 Docker-hub 以及 GitHub 的使用,又的确会经常遇到网络方面的问题,因此也一直在留心这方面的解决方案,目前大多是提供加速的方案,算是镜像方案之下的一个折中策略。

PS: 建议直接阅读 eryajf/Thanks-Mirror#Docker-hubopen in new window

方案一

替换原来镜像的前缀域名即可实现加速效果,比如:

原来地址: eryajf/centos:7.4  # 这个是官方镜像,省略了前边的域名
替换地址: docker.mirrors.sjtug.sjtu.edu.cn/eryajf/centos:7.4

另外,加速通常只是针对某个源站进行的加速,国外对公开放的 docker 仓库并非官方一家,因此这里就以源站的维度进行区分,整理出经过测试可用的加速站。

  • Docker-hub
    • 上海交通大学: docker.mirrors.sjtug.sjtu.edu.cn
    • 中科大: docker.mirrors.ustc.edu.cn
    • docker proxy: dockerproxy.com
  • gcr.io
    • docker proxy: gcr.dockerproxy.com
    • lank8s: 后期可能会转成付费: gcr.lank8s.cn
  • k8s.gcr.io
    • 上海交通大学: k8s-gcr-io.mirrors.sjtug.sjtu.edu.cn
    • docker proxy: k8s.dockerproxy.com
    • lank8s: lank8s.cn
  • ghcr.io
    • docker proxy: ghcr.dockerproxy.com
  • quay.io
    • 中科大: quay.mirrors.ustc.edu.cn

方案二

将加速地址写入到 docker 配置文件当中实现加速

/etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件) :

{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}

需要注意的是一定要符合 json 文件规范, 否则 docker 会启动失败

然后重启服务

sudo systemctl daemon-reload
sudo systemctl restart docker

目前可用的加速地址有:

  • 网易: http://hub-mirror.c.163.com
  • ustc: https://docker.mirrors.ustc.edu.cn
  • 百度云: https://mirror.baidubce.com/

换源拉不下来建议用官方源再试试, 毕竟镜像里仓库不一定齐