26 September 2011

Alternate Perls

Topics: Perl
Tags: cpanminus, perlbrew
Managing multiple Perls used to be a headache. Perlbrew has made it much easier. My preference is to put current Perl at /usr/bin/perl5. This article details how I use Perlbrew.

It is a running issue of contention between the Perl Community and Linux/BSD Distributions that the system’s Perl tends to be out of date. For Distribution maintainers they have a huge amount of plumbing written in Perl and until recently major releases of perl happened about every 6 years, and now suddenly Perl has a new release every year and a 2 year shelf life. Distribution Plumbing and nearly everything else have different needs, and the solution as I see it is to have two Perls on your system. My preference would be for the distribution maintainers to install an ancient Perl (or possibly several) with an alternate name, and leave a current perl to answer #!/usr/bin/perl. Since I have absolutely no influence with anyone who maintains a major distribution, the other option is to ignore /usr/bin/perl entirely and put a current Perl at /usr/bin/perl5. I prefer this solution to the env perl approach many others have chosen. It does require that for your scripts you change the #! line by appending the 5, but it is much easier to append a 5 to the line than remember where your alternate perl is buried in the file system (for scripts run outside your user environment).

To begin, you’ll need to fire up your system CPAN for its first and only ever run. But even before that you may need to install a few things like gcc. On ubuntu it is as simple as (sudo) apt-get install build-essential. You also need to decide which user is going to maintain perlbrew, it is not unreasonable to choose root, it is also quite reasonable to choose a different user.

(sudo) cpan App:perlbrew Say yes to installing dependincies 89 times. There are two tricks you can use to make this a little easier: install App::cpanminus first, it has fewer dependencies and can install perlbrew quietly, alternately Ubuntu Oneric has a perlbrew package, install it and then use perlbrew’s self update option.

If installation of perlbew fails, open up a cpan session and type

force notest install App::perlbrew.

From this point onwards we consider ourselfs to have 2 perls, our real Perl (perl5) the system perl which we will never touch, but which is a dependency for packages needing perl we might have installed.

export PERLBREW_ROOT=/opt/perlbrew (/usr/perlbrew and /usr/local/perlbrew are also good choices) perlbrew init

/opt/perlbrew/bin/perlbrew available /opt/perlbrew/bin/perlbrew install

Using Perl 5.14.1 as an example, type the following two lines

ln -s /opt/perlbrew/perls/perl-5.14.1/bin/perl /usr/bin/perl5
ln -s /opt/perlbrew/perls/perl-5.14.1/bin/perl5.14.1 /usr/bin/perl5.14.1

This will make Perl5.14.1 your default perl5, and it will give you a specific alias to perl5.14.1 if in the future you need to invoke it specifically you can type perl5.14.1 from anywhere without having to remember the full path.

Install cpanminus

Now that you have your new perl installed, even though perlbrew has an option to do this I recommend using the cpan in your new perlbrew directory. ./cpan App::cpanminus, configure cpan and answer the prompts.

When this is done go to /usr/bin, if you don’t have links there for cpan and cpanm copy them from your new perl’s directory, if you do, they’re just wrappers, edit the #! line to /usr/bin/perl5, so that if in the future you have a newer perl5, you won’t need to touch them.

Special case: Padre.

Padre requires a multi-threaded Perl, Perlbrew does not build a multi-threaded Perl by default. I update Padre about once a month. You have a two choices, you can tell Perlbrew to build a threaded Perl, or you can leave Padre on your system Perl, which means that you need to make sure that you update it in your system Perl, not your real Perl. If you don’t want a threaded perl a third option would be to build a special threaded perl for Padre (but as long as it works with system perl, why bother?).

/opt/perlbrew/bin/perlbrew install perl-5.xx.x \
  -Dusethreads -Duselargefiles -Dcccdlflags=-fPIC \
  -Doptimize=-O2 -Duseshrplib -Dcf_by="Your_name_here" \
  -Dcf_email="Your_email@here"