Linux各种包管理换国内源

在国外的机器上开发倒是没什么麻烦事,也不用操心这么多。但是由于众所周知的原因,在天朝可就麻烦多了,不想点法子的话下载依赖就够摸鱼一下午了。这么多包管理,不碰不要紧,一碰要狗命(单押x1 XD 干脆把手头用的这几个记一下,免得以后查来查去麻烦。

一键换源脚本

写完 yum 和 apt 的部分才想起来,这种常见的东西应该是有脚本可以搞定的吧。找了下果然有大佬写好了的。 如果只是 Linux 软件包的话就用一个脚本搞定:

1
$ wget -qO- git.io/superupdate.sh | bash

其他包管理的换源反正也不复杂,就手动弄一下。


YUM

使用 阿里镜像 作镜像源

  1. 备份原配置文件 $ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  2. 换源(注意 OS Version)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    # CentOS 6
    # wget 方式获取
    $ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    # curl 方式获取
    $ curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo    
       
    # CentOS 7
    # wget 方式获取
    $ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    # curl 方式获取
    $ curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
  3. 刷新 cache 生效 $ yum makecache

APT

使用 阿里镜像 作镜像源

  1. 备份原配置文件 $ cp /etc/apt/sources.list /etc/apt/sources.list_backup
  2. 清空配置 $ echo "" > /etc/apt/sources.list
  3. 换源(注意按 OS Version 进行添加) $ vim /etc/apt/sources.list
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    
    # Ubuntu 14.04.5 LTS
    deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
       
    # Ubuntu 16.04
    deb http://mirrors.aliyun.com/ubuntu/ xenial main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
    deb http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
       
    # Ubuntu 18.04(bionic)
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
       
    # Debian 7.x (wheezy)
    deb http://mirrors.aliyun.com/debian/ wheezy main non-free contrib
    deb http://mirrors.aliyun.com/debian/ wheezy-proposed-updates main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ wheezy main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ wheezy-proposed-updates main non-free contrib
       
    # Debian 8.x (jessie)
    deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
    deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
       
    # Debian 9.x (stretch)
    deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
    deb http://mirrors.aliyun.com/debian-security stretch/updates main
    deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
    deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
    deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
    deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
    

pip/pip3

永久换源

使用 阿里镜像 作镜像源 在配置文件中添加配置项 vim ~/.pip/pip.conf

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

[install]
trusted-host=mirrors.aliyun.com

临时换源

在使用 pip/pip3 命令安装软件时,在命令中添加参数 -i https://mirrors.aliyun.com/pypi/simple/ 例如升级 pip 的命令为:pip install -U pip -i https://mirrors.aliyun.com/pypi/simple/

Docker

使用 Docker 中国 作镜像源 一般情况下修改配置文件$ vim /etc/docker/daemon.json

添加下面配置项:

{
  "registry-mirrors": ["https://registry.docker-cn.com/"]
}

然后重启 docker deamon 即可

1
2
$ systemctl daemon-reload
$ systemctl restart docker

如果是通过 snappy(Ubuntu 16.04+)安装的 docker 的话,配置文件路径和重启命令都不同,配置内容一样:

1
2
$ vim /var/snap/docker/current/config/daemon.json
$ snap restart docker

运行命令$ docker info,查看其中的配置项是否正确:

Registry Mirrors:
 https://registry.docker-cn.com/

Docker 镜像使用帮助 [LUG@USTC]

Composer

使用 Packagist 作镜像源

全局配置

任意目录下执行下例命令即可:

1
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com

单个项目配置

进入项目目录 dir 执行命令:

1
$ composer config repo.packagist composer https://packagist.phpcomposer.com

NPM

使用 淘宝 NPM 镜像 作镜像源 使用淘宝 NPM 定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

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

或者通过添加 npm 参数 alias 一个新命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ alias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/dist \
--userconfig=$HOME/.cnpmrc"

## Or alias it in .bashrc or .zshrc
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \
  --cache=$HOME/.npm/.cache/cnpm \
  --disturl=https://npm.taobao.org/dist \
  --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
updatedupdated2020-04-062020-04-06
加载评论