How to set-up your own CPAN mirror

Are you a sysadmin and you're running servers with a lot of Perl code and Perl modules installed? You are a Perl hacker, who wish to access the complete CPAN repository on his notebook even when not connected to the internet?

Why not set-up your own CPAN-Mirror then? It's really easy.

All you need is to install the Perl module called CPAN::Mini.

cpan CPAN::Mini

will do that for you.

Here is a little Perl Script that Mirrors the CPAN Mirror of your choice to your local hard-drive:

#!/usr/bin/env perl

$> = 33; # switch effective user id to www-data

use CPAN::Mini;

CPAN::Mini->update_mirror(

remote => "ftp://ftp.cpan.org/pub/CPAN",
local => "/var/www/localhost/mirrors/cpan",
trace => ($ARGV[0] ? 1 : 0)
);

The trace option will active debug output. If you call your little cpan-mirror script with whatever command line argument, it will tell CPAN::Mini to print out some activity log. If you call it without any argument, it will be silent - which is nice when running as a cronjob.

Alternatively you could go and use the "minicpan" command line tool that comes with the CPAN::Mini bundle (installs to /usr/local/bin by default). Type in "perldoc minicpan" for details how to use it.

Next, you should make sure that you can reach your CPAN-Mirror via HTTP or FTP. This means you should make sure to mirror into a directory that can be accessed by your local web- or ftp-server.

Then, go and tell your CPAN shell to use your mirror. Type in "cpan" to start the CPAN shell.

cpan[1]> o conf urllist 'http://localhost/mirrors/cpan/' 'ftp://ftp.cpan.org/pub/CPAN' 'ftp://ftp.funet.fi/pub/languages/perl/CPAN'
cpan[2]> o conf commit

You should specify your local web- or ftp-server as first argument in the list of CPAN mirrors. Add as many mirrors as you like. A full list of CPAN mirrors in your country can be obtained here.

Leave a comment

About Marc

user-pic I am a Perl developer and CPAN author. I develop software with Perl since 1996 and Perl is still one of my favorite programing languages.