Ubuntu 15.04: Local Mirror for Offline Package Installation

What and Why

If you often install Ubuntu you will understand the benefits of a local Ubuntu repository. In the following I set up a mirror for Ubuntu Vivid 15.04 packages so that I can install on 64-bit and 32-bit PC's as well as Raspberry Pi. The mirror itself will reside on an Ubuntu 14.04 server and will update the mirror once a day, and I don't have to load the internet connection everytime I install a new OS or want a new package. 

An old 500GB hard disk from a worn out laptop has found a new purpose in life - physically hosting the repo files and offline wiki data (see my offline wiki blog here ). It lives at the end of a USB cable attached to the server and (at time of writing) is mounted manually after reboots. Let's get to it!

Install web server

sudo apt-get install apache2

Install mirroring software:

sudo apt-get install apt-mirror

Create a directory for packages, e.g. REPOVIVID :

sudo mkdir /mnt/REPOVIVID

...and mount the disk partition (let's say it's sdb1) you'll be keeping the files on, e.g.

sudo mount /dev/sdb1 /mnt/REPOVIVID

Also create a directory for other handy stuff, e.g. Scripts and Ubuntu binaries...

sudo mkdir /mnt/REPOVIVID/resources

Edit the mirror.list file

sudo vi /etc/apt/mirror.list

and changes it look like something along these lines:

############# config ##################
#
set base_path    /mnt/REPOVIVID
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu vivid main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu vivid-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu vivid-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu vivid-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu vivid-backports main restricted universe multiverse

deb-i386 http://archive.ubuntu.com/ubuntu vivid main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu vivid-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu vivid-updates main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu vivid-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu vivid-backports main restricted universe multiverse

deb-armhf http://ports.ubuntu.com/ubuntu-ports vivid main restricted universe multiverse
deb-armhf http://ports.ubuntu.com/ubuntu-ports vivid-security main restricted universe multiverse
deb-armhf http://ports.ubuntu.com/ubuntu-ports vivid-updates main restricted universe multiverse
#deb-armhf http://ports.ubuntu.com/ubuntu-ports vivid-proposed main restricted universe multiverse
deb-armhf http://ports.ubuntu.com/ubuntu-ports vivid-backports main restricted universe multiverse

#deb-src http://archive.ubuntu.com/ubuntu vivid main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu vivid-security main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu vivid-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu vivid-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu vivid-backports main restricted universe multiverse

Verifiy you have enough space for the repo. Vivid needed about 230GB (2015-08-04, 32-bit and 64-bit packages and armhf port)

Now get the packages. And remember the old adage attributed to Sphinx:

"He who always waits impatiently while downloading can only download whilst he impatietly waits"

sudo apt-mirror 

Sample output:

Downloading  index files using 20 threads... Begin time: Tue July 28 11:12:13 2015 [20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]...

Repo directories will look something like this after downloading armhf, 32-bit and 64-bit packages (the 'data' directory contains offline wiki files):
Disk Usage - Repository

Schedule cron job to run e.g. 4am:

sudo vi /etc/cron.d/apt-mirror

Make sure you have a line like this:

0 4    * * *   apt-mirror      /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log

Make mirror available on the web server's /REPOVIVID directory:

cd /var/www/
sudo ln -s /mnt/REPOVIVID/mirror/archive.ubuntu.com/ubuntu/ ubuntu

In the unlikely event that youwant to remove a link, use rm thusly to remove above link:

sudo rm ubuntu

Set up a directory for some handy scripts in the /REPOVIVID directory:

sudo mkdir /mnt/REPOVIVID/resources/SCRIPTS

Insert repo path into a file like e.g. client_sources.list

sudo vi /mnt/REPOVIVID/resources/SCRIPTS/client_sources_list

...and add lines like these:

deb http://10.0.1.188/ubuntu vivid main
deb http://10.0.1.188/ubuntu vivid multiverse
​deb http://10.0.1.188/ubuntu vivid restricted
deb http://10.0.1.188/ubuntu vivid universe

deb http://10.0.1.188/ubuntu vivid-updates main
deb http://10.0.1.188/ubuntu vivid-updates multiverse
deb http://10.0.1.188/ubuntu vivid-updates restricted
deb http://10.0.1.188/ubuntu vivid-updates universe

deb http://10.0.1.188/ubuntu vivid-security main
deb http://10.0.1.188/ubuntu vivid-security multiverse
deb http://10.0.1.188/ubuntu vivid-security restricted
deb http://10.0.1.188/ubuntu vivid-security universe

Create a file like e.g. client_install_list.sh to automate installing packages you always install:

sudo vi /mnt/REPOVIVID/resources/SCRIPTS/client_install_list.sh

...and add lines like these:

#!/bin/sh apt-get update
sudo apt-get install gimp -y
sudo apt-get install openssh-server -y
(see my Gots-to-Have list here)

Now might be a good time to download the Ubuntu release binaries, just so's ya gots 'em nice 'n handy...

sudo mkdir /mnt/REPOVIVID/resources/ISO
cd /mnt/REPOVIVID/resources/ISO
wget http://releases.ubuntu.com/15.04/ubuntu-15.04-server-amd64.iso
wget http://releases.ubuntu.com/15.04/ubuntu-15.04-desktop-amd64.iso
wget http://releases.ubuntu.com/15.04/ubuntu-15.04-desktop-i386.iso
wget http://releases.ubuntu.com/15.04/ubuntu-15.04-server-i386.iso
wget http://cdimage.ubuntu.com/ubuntu-mate/releases/15.04/release/ubuntu-mate-15.04-desktop-i386.iso
wget http://cdimage.ubuntu.com/ubuntu-mate/releases/15.04/release/ubuntu-mate-15.04-desktop-amd64.iso
wget https://ubuntu-mate.r.worldssl.net/raspberry-pi/ubuntu-mate-15.04-desktop-armhf-raspberry-pi-2.img.bz2

Insert repo path into a file like e.g. client_sources.list

Configure Client

Edit sources.list on the client.. 

sudo vi /etc/apt/sources.list

...open a browser and point it at your repo server e.g. 10.0.1.188/resources/SCRIPTS and download the client_sources.list file created above. Copy/paste contents into sources.list and save.

Also download, make executable, and run the client_install_list.sh from 10.0.1.188/resources/SCRIPTS (that you also made above) thusly:

chmod +x ~/Downloads/client_install_list.sh
sudo ./client_install_list.sh

*** The End ***