↑ Best viewed this side up ↑

Installing .deb Packages from a Local Repository

May 11th, 2011

Want to jump straight to the meat of this post? Click here.

So you’ve built or downloaded some .deb packages and want to install them on your Ubuntu/Debian server. If these packages do not have (m)any external dependencies, you can use dpkg -i, otherwise it would make sense to set up a repository and use conventional APT tools.

For instance, if you have followed my previous post and backported PHP 5.3.3 to Lucid, you may want to install the php5-gd package to enable graphics handling in your PHP scripts. Let’s see:

$ aptitude show php5-gd
   .  .  .
Depends: libc6 (>= 2.4), libfreetype6 (>= 2.2.1), libgd2-xpm (>=
         2.0.36~rc1~dfsg), libjpeg62, libpng12-0 (>= 1.2.13-4), libt1-5 (>=
         5.1.0), libx11-6, libxpm4, zlib1g (>= 1:1.1.4), phpapi-20090626+lfs,
         php5-common (= 5.3.2-1ubuntu4.7)
   .  .  .

Naturally, php5-gd depends on a handful of graphics libraries that are not installed by default on Ubuntu Server, and these libraries in turn have dependencies, so better leave it to APT to deal with all that.

If you agree, here is

How To Set Up a Local Repository On Your Ubuntu Server

I will only consider the setup of a local repository right on a production server. If you need to deploy to many physical and/or virtual servers, you likely already have a network-accessible repository on a dedicated system and so on (if you do not, that is unfortunately beyond the scope of this post.)

I however suggest that you create and maintain such a repository on a reference system, and then simply copy it to production servers, so as to minimize interference with their daily operation.

  1. If you came here from my PHP backport post, you may create such a reference repository right on the build system and skip this step. Otherwise, you may need to install the dpkg-dev package:
    sudo apt-get install dpkg-dev
  2. Create a directory that will hold the repository, such as /usr/local/deb, and in it, a subdirectory for each package set:
    sudo mkdir -p /usr/local/deb/php5-5.3.3
  3. In the root of the repository, create an update script, owned by root:
    sudoedit /usr/local/deb/update

    Paste the following:

    ! cd "$(dirname "$0")" \
       && echo "Cannot change directory to $(basename "$0") script location" 1>&2 \
       && exit 1
    echo "Updating the local repository in" `pwd`
    find . -mindepth 1 -maxdepth 1 -type d \
       | cut -c3- \
       | xargs -n 1 -I DIRNAME dpkg-scanpackages DIRNAME DIRNAME/override \
       | gzip -c9 >Packages.gz
    [ $? -ne 0 ] \
       && echo "Update failed" 1>&2 \
       && [ `id -u` -ne 0 ] && echo "Maybe you've forgotten to sudo?" 1>&2

    and give the owner execute permission:

    sudo chmod u+x /usr/local/deb/update

    Basically, this script runs dpkg-scanpackages against all immediate subdirectories of its own containing directory and collects the output in a repository index file called Packages.gz.

  4. Copy the packages that you need to deploy to the repository.
    For instance, the only PHP packages needed for this WordPress installation to work are php5-fpm and php5-mysql, and the former in turn requires php5-common:

    sudo cp php5-fpm*.deb php5-mysql*.deb php5-common*.deb /usr/local/deb/php5-5.3.3/
  5. (optional) Create an extra override file in each subdirectory, e.g.:
    sudoedit /usr/local/deb/php5-5.3.3/override

    For the above three packages, paste:

    php5-common     optional        web
    php5-mysql      optional        web
    php5-fpm        optional        universe/php

    (I have extracted these lines from the original Ubuntu 10.10 extra override files, found in the indices directory of your favorite mirror, e.g. http://us.archive.ubuntu.com/ubuntu/indices/)

  6. Run the repository update script:
    sudo /usr/local/deb/update

    Expected output:

    Updating the local repository in /usr/local/deb
    dpkg-scanpackages: info: Wrote 3 entries to output Packages file.
  7. Create a new .list file in /etc/apt/sources.list.d/:

    sudoedit /etc/apt/sources.list.d/local.list

    with the following contents:

    deb file:/usr/local/deb ./

    and run:

    sudo apt-get update
  8. Test the repository:

    sudo apt-get -s install package-name

    (“-s” means simulated install, omit it if you want to do a real one)

    For instance:

    sudo apt-get -s install php5-fpm php5-mysql

    Expected output (watch for version number and “localhost“:

       .  .  .
    The following NEW packages will be installed:
      php5-common php5-fpm php5-mysql
    0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
    Inst php5-common (5.3.3-1ubuntu9.5 localhost)
    Inst php5-fpm (5.3.3-1ubuntu9.5 localhost)
    Inst php5-mysql (5.3.3-1ubuntu9.5 localhost)
       .  .  .
  9. Finally, package the repository for easy deployment to production servers. Include .deb files, Packages.gz, and the .list file:

    tar cvfPz php5_5.3.3-lucid-backport.tgz \
           /usr/local/deb/Packages.gz \
           /usr/local/deb/php5-5.3.3/*.deb \

    (the “P” option is a shorthand to --absolute-names – do not strip the leading “/” from file names.)

Now, to install the backport on another system, simply unpack the archive from wherever you have copied it and run apt-get as usual:

sudo tar xvf php5_5.3.3-lucid-backport.tgz
sudo apt-get update
sudo apt-get install php5-fpm php5-mysql

Tags: , , ,


* Copy This Password *

* Type Or Paste Password Here *