跳到主要内容

常用包管理换源

众所周知,国内访问国外的服务器速度很慢,所以我们需要将一些常用的包管理器的源换成国内的镜像源,以提高下载速度。

这里记录一些常用包管理器换源的方法,以备随时查阅。

npm

淘宝npm镜像 http://registry.npm.taobao.org/

cnpmjs镜像 http://r.cnpmjs.org/

临时换源

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

永久换源

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

取消换源

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

pip

  • 清华大学开源镜像站 https://pypi.tuna.tsinghua.edu.cn/simple

  • 阿里开源镜像站 https://mirrors.aliyun.com/pypi/simple/

临时换源

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

永久换源

  • 一键换源

    需要 pip 版本 > 10.0.0

    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=pypi.tuna.tsinghua.edu.cn

apt

  • 一键脚本 (By Skyone)
sudo bash -c "$(curl -fsSL https://tc.skyone.host/shell/apt-mirrors)"

通过 MIRROR 环境变量指定镜像

通过 REQUEST_PROTOCOL 指定 http (默认 https )

例如:

MIRROR=ustc REQUEST_PROTOCOL=http sudo bash -c "$(curl -fsSL https://tc.skyone.host/shell/apt-mirrors)"
  • 根据系统版本选择镜像源

    24.04:noble;22.04:jammy;20.04:focal;18.04:bionic;16.04:xenial;14.04:trusty

  • 阿里云 https://mirrors.aliyun.com/ubuntu/

  • 腾讯云 https://mirrors.cloud.tencent.com/ubuntu/

  • 中科大 https://mirrors.ustc.edu.cn/ubuntu/

  • 清华大学 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/

sources.list 格式(位于 /etc/apt/sources.list):

# 默认注释了源码仓库,如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ noble main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ noble-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.ustc.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse

DEB822 格式(位于 /etc/apt/sources.list.d/ustc.list):

Types: deb
URIs: https://mirrors.ustc.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: https://mirrors.ustc.edu.cn/ubuntu
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg