使用国内镜像服务器,解决apt-get、pip等安装工具下载依赖包速度慢的问题

问题

使用apt-getpip等安装工具下载依赖包时,很多源都部署在国外服务器,由于众周知的墙的原因,下载速度异常缓慢,经常出现下载超时的错误,无法正常安装软件。例如,例如pip安装软件时,经常出现下述错误:

...
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

解决办法:

首先想到的是使用代理服务器,但经无数次的验证,网上这些常见的代理工具,在命令行终端中基本没有任何效果
下面是其中一种比较简单的代理工具:
首先,安装并运行polipo,通过该工具创建一个端口为8123的HTTP代理服务器,在其内部将HTTP请求转换为Shadowsocks请求:

apt-get install polipo
service polipo stop
polipo socksParentProxy=localhost:1080123

然后,在另一个命令行终端中使用此HTTP代理服务器。注意:如果网站使用https协议,则需要使用https_proxy=http://localhost:8123。另外,一定要先开启shadowshocks服务
我发现如果polipo服务开启时间较长,则下载速度同样会很慢

http_proxy=http://localhost:8123 sudo apt-get update
https_proxy=http://localhost:8123 sudo pip install -e '.[all]'

也可以使用如下命令,可确保在当前命令行终端中使用代理(经实际检测,似乎没什么效果):

export http_proxy=http://localhost:8123
export https_proxy=http://localhost:8123
sudo pip install -e '.[all]'

确实管用的方法:

于是只能另辟蹊径,在国内找镜像服务器,具体操作如下:

1. 将pip服务器更换为国内的镜像服务器。

修改 ~/.pip/pip.conf (没有就创建一个):

# 如果不存在此文件夹,则创建之
mkdir ~/.pip
vi ~/.pip/pip.conf

内容如下:

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

2. 将apt服务器更换为国内的镜像服务器。

cd /etc/apt
# 将原配置文件备份
sudo cp sources.list sources.list.bak
# 修改配置文件 
sudo vi sources.list 

内容如下(可通过清华大学官网获取配置文件内容):

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

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

使用如下命令更新源:

sudo apt-get update

猜你喜欢

转载自blog.csdn.net/davidhopper/article/details/81188906