如何加快 Debian 或 Ubuntu 上缓慢的 apt-get 安装速度
如果您觉得在 Debian 或 Ubuntu 系统上通过 apt-get (apt
) 或 aptitude
安装软件包通常太慢,有几种方法可以改善这种情况。您是否考虑过切换正在使用的默认镜像站点?您是否检查过互联网连接的上行带宽,看看这是否是瓶颈?
没有别的,你可以尝试第三个选项:使用 apt-fast 工具。 apt-fast
实际上是围绕 apt-get
和 aptitude
编写的 shell 脚本包装器,可以加快包下载速度。在内部,apt-fast
使用 aria2
下载实用程序,该实用程序可以同时从多个镜像下载分块形式的文件(如 BitTorrent 下载)。
在 Ubuntu 或 Debian 上安装 apt-fast
以下是在基于 Debian 的 Linux 上安装 apt-fast 的步骤。
在 Debian 上:
$ sudo apt-get install aria2
$ wget https://github.com/ilikenwf/apt-fast/archive/master.zip
$ unzip master.zip
$ cd apt-fast-master
$ sudo cp apt-fast /usr/bin
$ sudo cp apt-fast.conf /etc
$ sudo cp ./man/apt-fast.8 /usr/share/man/man8
$ sudo gzip /usr/share/man/man8/apt-fast.8
$ sudo cp ./man/apt-fast.conf.5 /usr/share/man/man5
$ sudo gzip /usr/share/man/man5/apt-fast.conf.5
在 Ubuntu 14.04 及更高版本上:
$ sudo add-apt-repository ppa:saiarcot895/myppa
$ sudo apt-get update
$ sudo apt-get install apt-fast
在 Ubuntu 11.04 至 13.10 上:
$ sudo add-apt-repository ppa:apt-fast/stable
$ sudo apt-get update
$ sudo apt-get install apt-fast
在 Ubuntu 上安装期间,系统会要求您选择默认包管理器(例如,apt-get
、aptitude
)和其他设置。您稍后可以随时通过编辑配置文件 /etc/apt-fast.conf
来更改设置。



配置 apt-fast
安装完成后,您需要在/etc/apt-fast.conf
中配置apt-fast
使用的镜像列表。
您可以在以下 URL 找到可供选择的 Debian/Ubuntu 镜像列表。
Debian: http://www.debian.org/mirror/list
Ubuntu: https://launchpad.net/ubuntu/+archivemirrors
选择地理位置靠近您所在位置的镜像后,按以下格式将这些选定的镜像添加到 /etc/apt-fast.conf
中。
$ sudo vi /etc/apt-fast.conf
德班:
MIRRORS=('http://ftp.us.debian.org/debian/,http://carroll.aset.psu.edu/pub/linux/distributions/debian/,http://debian.gtisc.gatech.edu/debian/,http://debian.lcs.mit.edu/debian/,http://mirror.cc.columbia.edu/debian/')
Ubuntu/薄荷:
MIRRORS=('http://us.archive.ubuntu.com/ubuntu,http://mirror.cc.columbia.edu/pub/linux/ubuntu/archive/,http://mirror.cc.vt.edu/pub2/ubuntu/,http://mirror.umd.edu/ubuntu/,http://mirrors.mit.edu/ubuntu/')
如上所示,特定存档的各个镜像应以逗号分隔。建议您在 MIRRORS 字符串中包含 /etc/apt/sources.list
中指定的默认镜像站点。
使用 apt-fast 安装软件包
现在您已准备好测试 apt-fast
的强大功能。以下是 apt-fast 的命令行用法:
apt-fast [apt-get options and arguments]
apt-fast [aptitude options and arguments]
apt-fast { { install | upgrade | dist-upgrade | build-dep | download | source } [ -y | --yes | --assume-yes | --assume-no ] ... | clean }
要使用 apt-fast 安装软件包:
$ sudo apt-fast install texlive-full
要下载当前目录中的包而不安装它:
$ sudo apt-fast download texlive-full

如前所述,apt-fast
的并行下载是由 aria2
完成的。您可以按如下方式验证来自多个镜像的并行下载。
$ sudo netstat -nap | grep aria2c

请注意,apt-fast
并不会使 apt-get update
更快。仅当 install
、upgrade
、dist-upgrade
和 build-dep
操作时才会触发并行下载。对于其他操作,apt-fast
只需回退到默认包管理器 apt-get
或 aptitude
。
apt-fast
有多快?
为了比较 apt-fast 和 apt-get,我尝试在两个相同的 Ubuntu 实例上使用两种方法安装多个软件包。下图显示了包安装总时间(以秒为单位)。

正如您所看到的,apt-fast
比 apt-get
快得多(例如,快 3--4 倍),尤其是在安装了庞大的软件包时。
请注意,性能改进当然会有所不同,具体取决于您的上游互联网连接。就我而言,我有足够的空闲带宽可用于上游连接,这就是为什么我通过使用并行下载看到了显着的改进。