Feb 18, 2012

Install your own Perl with your own CPAN

Yes I know, it exists a featured tool called perlbrew but can happen that
  • your home dir has not enough space
  • you don't have an internet connection
  • you want to tweak your perl distro
  • you just want to DIY

In this article I talk about how to build your own Perl, and use your own CPAN mirror. 

Suppose your login is "user" on host "darkstar" that is a Linux box with Internet connection.
You can start there to build your Perl and mirror your own CPAN.
After that you can hop to hosts in your Intranet, I mean with no connection outside your Firewall.

You can draw your topology

DEV --> TEST --> PROD 

which are three hosts with custom Perl installation (latest). This is just an example topology that matches my job place policy. Adapt it to your needs, mutatis mutandis.
  • DEV is the first host, connected to Internet, with a freezed CPAN mirror serving to the TEST host.
  • The TEST host, mirrors CPAN from DEV which is test safe, specially for make installdeps.
  • The PROD host, runs Perl software tested in the TEST host.

Let's start now!

You can copy and paste to get your latest Perl installed, except for bold command lines where you have to choose you Perl root dir and your Perl version.

Create your Perl root folder, for instance /opt/Perl and set permissions properly,
probably you will need to use sudo.
[user@darkstar]$ export PERL_ROOT=/opt/Perl
[user@darkstar]$ sudo mkdir $PERL_ROOT
[user@darkstar]$ sudo chown user:group $PERL_ROOT

Create folder structure.
[user@darkstar]$ mkdir $PERL_ROOT/src
[user@darkstar]$ mkdir $PERL_ROOT/CPAN

Download latest Perl sources, for instance version 5.14.2,
and put them under $PERL_ROOT/src folder.
[user@darkstar]$ export PERL_VERSION=5.14.2
[user@darkstar]$ cd $PERL_ROOT/src
[user@darkstar]$ tar xzvf perl-$PERL_VERSION.tar.gz
[user@darkstar]$ cd perl-$PERL_VERSION

Now build your Perl !

Just use defaults and point to your folders.
[user@darkstar]$ sh Configure -des -Dprefix=$PERL_ROOT/$PERL_VERSION -Dscriptdir=$PERL_ROOT/$PERL_VERSION/bin

And pray the mantra, make ... make test ( DON'T MISS IT ! ) ... make install.
[user@darkstar]$ make
[user@darkstar]$ make test
[user@darkstar]$ make install

Clean up.
[user@darkstar]$ cd ..
[user@darkstar]$ rm -rf perl-$PERL_VERSION

Now add this symbolic link, it will be useful in the future to switch to latest Perl version.
[user@darkstar]$ ln -s $PERL_ROOT/$PERL_VERSION $PERL_ROOT/current

Set up your env, for instance add these lines to your bash profile ...
export PERL_ROOT=/opt/Perl
export CPAN_MINI_CONFIG=$PERL_ROOT/.minicpanrc
export PATH=$PERL_ROOT/current/bin:$PATH

and reload it.
[user@darkstar]$ source $HOME/.bash_profile

You can check if you are pointing to the right Perl.
[user@darkstar]$ perl -v

You can upgrade Perl simply repeating installation and using the sympolic link to point to latest version.

Habemus Perl, now it's time to update CPAN !

Since this is the first time you use the cpan shell, it will prompt you for configuration: just use defaults, i.e. type <ENTER> as in an '80s shoot em all :)
[user@darkstar]$ cpan CPAN

Install CPAN::Mini
[user@darkstar]$ cpan CPAN::Mini

Now you can set up your local mirror using your favourite editor (of course vim :).
[user@darkstar]$ vim $CPAN_MINI_CONFIG

local: $ENV{CPAN_ROOT}
exact_mirror: 1

Put exactly $ENV{CPAN_ROOT}, it works !

REMOTE_CPAN_MIRROR  is a working CPAN mirror,
type the following to know your current list ...
[user@darkstar]$ cpan
cpan> o conf urllist

the last entry should work (type q to exit the cpan shell).

It's time to update your mirror, just type ...
[user@darkstar]$ minicpan

since it takes a lot of time to mirror all the stuff :), so go out and have a beer.

Ok, ok ... maybe you should type instead
[user@darkstar]$ nohup minicpan > /dev/null &

so you can go holiday :)

After a while evaluate this pseudo code

if( pickedUpGirl || pickedUpBoy ) 
    raise Exception;
} else { 
    return toHaveFun; 

I assume pickedUpGirl is false :-).

Now you can configure cpan shell to point to your local CPAN mirror
[user@darkstar]$ cpan
cpan> o conf urllist
cpan> o conf urllist file:///path/to/local/cpan
cpan> o conf commit

where path/to/local/cpan is the absolute path to your local CPAN mirror, i.e. what you set for CPAN_ROOT in your .bash_profile.

Your DEV host is ready, now repeat Perl installation on TEST and PROD hosts.

Develop your software on DEV host.

Pack your software as a compliant CPAN distribution.
This helps organize your code, add documentation, write tests, include scripts.

Then use CPAN::Mini::Inject to add your distro, to CPAN mirror on DEV and make it installable via cpan client.
[user@darkstar]$ cpan CPAN::Mini::Inject
[user@darkstar]$ mcpani -H

And read the mcpani man ... I'm sorry but right now I don't remember the options :-P

On DEV and TEST host install CPAN::Mirror::Server::HTTP, it works great!

[user@darkstar]$ cpan CPAN::Mirror::Server::HTTP
[user@ darkstar]$ cpanmirrorhttpd --root $CPAN_ROOT

Yep, there is a running CPAN listening to 8080 port.
Install and configure minicpan on TEST.

Mirror CPAN on TEST host from DEV host if your distro tests on DEV are ok.
Launch cpanmirrorhttpd on TEST host
Configure cpan client on PROD host to point to TEST host on 8080 port.


On DEV, TEST and PROD you have latest Perl release.

  • you develop your software packaged as a CPAN distro
  • there is a CPAN mirror, where you inject your distro
  • cpan client points to local CPAN
  • there is CPAN server on port 8080
  • there is a CPAN mirror, that points to DEV
  • cpan client points to local CPAN
  • install your distro using cpan client and run your software
  • cpan client points to CPAN mirror on TEST
  • run your software installed via cpan client

Come on guys, Perl is awesome !!!