Martin Atkins (mart) wrote in apparentlymart,
Martin Atkins

Integrating CPAN with APT

I don't really care too much about my scratch servers, but on my production servers I'm pretty anal about making sure everything is accounted for via apt/dpkg. Debian currently has a pretty pitiful selection of modules from CPAN in the main distribution, so it's inevitable that anyone running Perl apps will end up installing modules directly from CPAN eventually. This should at least be done by building a Debian package (.deb) file of the CPAN package, and should ideally be done via apt to get the dependencies right.

Debian Sarge ships with a tool called dh-make-perl which can build and optionally install .deb packages from CPAN modules. Unfortunately it doesn't properly follow dependencies nor does it record the dependencies in the .deb file, so it's pretty useless. The Perl module CPANPLUS comes with support for building CPAN modules into various packaging formats including .deb archives, but of course CPANPLUS isn't in the Sarge distribution.

The following is a short recipe for getting and using CPANPLUS's deb package builder, using dh-make-perl as much as necessary to get CPANPLUS working and then using CPANPLUS for everything else. The end result of this is a local apt repository for CPAN modules and a script to build and insert modules into that repository.

First, let's get as much as possible from the core Sarge distribution:

$ apt-get install dh-make-perl
$ apt-get install \
        libarchive-tar-perl \
        libio-zlib-perl \
        liblocale-maketext-simple-perl \
        libmodule-load-perl \

This gets the dh-make-perl utility and a small subset of the forest of prerequisites for CPANPLUS. With that out of the way, we can now use dh-make-perl to get the rest of the dependencies. dh-make-perl dumps the CPAN archive and Debian package into the current directory, so make sure you cd to a suitable location first. The command line to use is:

dh-make-perl --install --notest --cpan module_name

I disable tests here because dh-make-perl doesn't follow and build dependencies and so the tests will fail unless you do these in the right order, which I considered to be too much of a pain in the ass. The list of modules is as follows:

  • Archive::Extract
  • File::Fetch
  • IPC::Cmd
  • Log::Message
  • Module::Build
  • Module::Signature
  • Module::CoreList
  • Module::Load::Conditional
  • ExtUtils::CBuilder
  • Module::Loaded
  • Object::Accessor
  • Package::Constants
  • Params::Check
  • Term::UI

CPANPLUS also depends on a newer version of Test::Harness than the one in the Debian perl-modules package, but dh-make-perl refuses to build it because it's in the core distribution and CPANPLUS doesn't actually seem to mind in practice. Finally, after all this, we're ready to install CPANPLUS itself and the Debian package tool. The following commands should do the trick:

dh-make-perl --install --cpan CPANPLUS
dh-make-perl --install --cpan CPANPLUS::Dist::Build
dh-make-perl --install --cpan CPANPLUS::Dist::Deb

If all that works out (which it should!) you will have a working installation of CPANPLUS, and in particular a new command available called cpan2dist, which is the main tool to use when adding new packages:

cpan2dist --format CPANPLUS::Dist::Deb --buildprereq --timeout 0 --verbose module_name

cpan2dist dumps its files in the directory /root/.cpanplus/5.8.4/dist/debian (assuming you're running it as root). If you have the apt-utils Debian package installed you can use the apt-ftparchive tool to turn that directory into an apt repository:

cd /root/.cpanplus/5.8.4/dist/debian
apt-ftparchive packages main/pool/cpan-lib/ >dists/stable/main/binary-i386/Packages
apt-ftparchive sources main/pool/cpan-lib/ >dists/stable/main/binary-i386/Sources
apt-ftparchive release main/pool/cpan-lib/ >dists/stable/main/binary-i386/Release

The first time you do this you'll need to make the directory into which those files end up, but obviously on subsequent runs this is unnecessary as the files are simply replaced. I find it convenient to bundle the cpan2dist command along with the apt-ftparchive commands up into a simple shell script so that the installation and archive indexing can be done in a single step. Now we can add the repository to our /etc/apt/sources.list file:

deb file:/root/.cpanplus/5.8.4/dist/debian stable main

One apt-get update later and you should find your modules available. Note that the CPANPLUS dist tool generates packages prefixed with cpan- to avoid collisions with packages from the main distribution, but also generates a conflicts entry for the unprefixed version so that both cannot be installed at the same time. While your list of packages is small you can do apt-cache search ^cpan- to see the list of all packages installed through this mechanism.

If you wish, you can skip the apt repository step altogether and install the generated .deb files manually, but of course you will have to manually install all of the dependencies in the correct order, which I consider to be too much like hard work.

No doubt this will all shortly be obsoleted by the imminent release of the new Etch distribution. Unfortunately it appears that Etch doesn't come with CPANPLUS packages either, so no doubt a revised version of this recipe will be necessary in shortly.

Tags: debian, linux, perl

  • Moved to TypePad is now hosted on TypePad rather than LiveJournal. All of the old content remains over here in LiveJournal land, but those who are…

  • Moving the Goalposts

    In the few weeks since I published the first drafts of AtomActivity, ActivitySchema and friends several things have come about: FriendFeed is…

  • Activity Streams and Comment Aggregation

    One pain point that exists for activity streams right now is the dispersal of responses over various networks. When I post a blog entry like this…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.