Monday, August 06, 2007

Using a download accelerator with apt-get

If dist-upgrading your Ubuntu or Debian GNU/Linux distro is taking a considerable amount of time (or says it will), then this might be a useful tip to speed things up a bit.

This is nothing new really; something I used to frequently back in the day when Debian was my primary desktop disto. You see I got sick of waiting for debs to complete downloading, not to mention the apt database being locked, so I wrote a small perl script which used prozilla to quickly download the debs into /var/cache/apt/archives.

Recently, my g/f told me to dist-upgrade her Ubuntu box, since she didn't have broadband @ home and thats when I remembered that I have forgotten where I'd put that script. So I wrote new one that was an even smaller script.

But before that, I wrote a tiny bash "one liner" which can also be used download debs but it may not work with some debs that have to be renamed by escaping some characters. But nevertheless it too can be useful for downloading one or two packages with dependencies.

I'm assuming your running this as root

# cd /var/cache/apt/archives
# apt-get -y --print-uris install package_name > debs.list
# egrep -o -e "(ht|f)tp://[^\']+" debs.list | xargs -l1 axel -a

Instead of using prozilla, I now use axel which is also another download accelerator. You should be able to easily apt-get install axel. Replace package_name with either a package name you are installing or dist-upgrade if you want to upgrade the system altogether. Be warned that you may have to rename some files before apt sees the file as being downloaded completely.

Now on to the "slightly longer, works better", perl script:
# cd /var/cache/apt/archives
# apt-get -y --print-uris install package_name > debs.list
# vi download-debs.pl


#!/usr/bin/perl

while(<>){

if(/([^ ]+)[ ]+([^ ]+)[ ]+/){
($url,$rendeb)=($1,$2);
$url=~s/[' ]//g;
$deb=$url; $deb=~s/.*\///;
$rendeb=~s/[ ]//g;
print "Downloading $deb \n";
system("axel -a $url");
if($deb ne $rendeb){
print "Renaming $deb to $rendeb \n";
system("mv $deb $rendeb");
}
}
}



# ./download-debs.pl debs.list

While your waiting for the download you might even be able to squeeze in a few new smaller packages, as the apt database isn't locked. Once the download is complete, just run the same command without the -print-uris option and your good to go.

Be warned that using a download accelerator might bring your sys admin running to your desk screaming before you can say "apt-get are we done yet!".