Monday 4 May 2015

How to install different versions of Perl


Taking control with Perlbrew on Ubuntu

Perlbrew is the quick and easy way to get multiple isolated installations of Perl onto a single host. I'll walk through how this is done on a box with Ubuntu installed.

One reason I like Ubuntu is because it comes with Perl and a library of several thousand modules already installed so you can just write

$ perl -e 'print "Hello Perl ${^V}!\n"'
Hello Perl v5.18.2!

and you're on your way.

However, only using the Perl which comes with Ubuntu can be problematic for reasons such as:
  • You're writing code for a client who will run it against a different version of Perl;
  • You're maintaining a module on CPAN and you've been informed that it breaks on a different version of Perl;
  • For better performance or new features, you want to run your code under a newer version of Perl; or
  • You want to use a more recent version of a module than the one which comes with Ubuntu.
You could just login as root and install the newer version of Perl or the module - but don't! Running this shows you that your Ubuntu box has over a thousand dependencies on its particular version of Perl.

$ apt-cache rdepends perl | grep -v lib.*perl | wc -l
1021

Although Perl is famous for being backward compatible, upgrading your operating system's Perl would be playing Russian roulette. Perlbrew enables you easily host multiple isolated installations of Perl with their own libraries under your home directory at no risk to the host operating system.


Step 1. Ensure your host is able to compile Perl from source


$ sudo apt-get install gcc cpp make


Step 2. Install Perlbrew

As yourself, not root:
$ \curl -L http://install.perlbrew.pl | bash

Step 3. Update your terminal settings

Following the instructions in the output of the previous command
$ echo source ~/perl5/perlbrew/etc/bashrc >> ~/.bash_profile

To see whether it worked, open a new Bash terminal and run
$ which perlbrew
/home/andrew/perl5/perlbrew/bin/perlbrew

If this command fails to find perlbrew
$ echo source ~/perl5/perlbrew/etc/bashrc >> ~/.bashrc
or
$ echo source ~/perl5/perlbrew/etc/bashrc >> ~/.profile

Step 4. Install a Perl

Firstly, see which versions are available:
$ perlbrew available
  perl-5.21.11
  perl-5.20.2
  perl-5.18.4
  perl-5.16.3
  perl-5.14.4
  perl-5.12.5
  perl-5.10.1
  perl-5.8.9
  perl-5.6.2
  perl5.005_04
  perl5.004_05
  perl5.003_07

Once you've decided the Perl you'd like to run against

$ perlbrew install perl-5.20.2
Go make a coffee.


Step 5. Install another Perl just for fun

$ perlbrew install perl-5.21.11
Make another coffee.


Step 6. See what's on offer

$ perlbrew list
  perl-5.20.2
  perl-5.21.11

Noting that $ which perl still refers to /usr/bin/perl


Step 7. To change Perl for the current shell

$ perlbrew use perl-5.20.2
$ which perl
/home/andrew/perl5/perlbrew/perls/perl-5.20.2/bin/perl

You could make this your default Perl every time you log-in with
$ perlbrew switch perl-5.20.2

You can also revert to the system's Perl with
$ perlbrew off        # for this terminal only
$ perlbrew switch-off # every time you log-in

Step 8. Using your new Perl

This shebang at the top of a script will use your operating system's Perl regardless of whether perlbrew is switched on or off:
#!/usr/bin/perl
In order to use the Perl of your current environment
#!/usr/bin/env perl

print "Hello Perl ${^V}!\n";

Step 9. Check out the library

You'll see that when you're switched to perl-5.20.2 the CPAN modules you have access to are all within 5.20.2's own library:
$ perl -e 'use Data::Dumper; print Dumper(\@INC)'
$VAR1 = [
          '/home/andrew/perl5/perlbrew/perls/perl-5.20.2/lib/site_perl/5.20.2/x86_64-linux',
          '/home/andrew/perl5/perlbrew/perls/perl-5.20.2/lib/site_perl/5.20.2',
          '/home/andrew/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2/x86_64-linux',
          '/home/andrew/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2',
          '.'
        ];

and if you want to add something to that library just install cpanminus and you're in full swing!
$ \curl -L https://cpanmin.us | perl - App::cpanminus
...
$ cpanm Data::Dump
--> Working on Data::Dump
Fetching http://www.cpan.org/authors/id/G/GA/GAAS/Data-Dump-1.22.tar.gz ... OK
Configuring Data-Dump-1.22 ... OK
Building and testing Data-Dump-1.22 ... OK
Successfully installed Data-Dump-1.22
1 distribution installed



4 comments:

  1. Thanks for this post - I tried installing & using perlbrew with "sudo apt-get install perlbrew" and it didn't work very well. I removed that and followed your directions and it seems to be working well so far.

    Thanks again!

    ReplyDelete
  2. @cpan_author confirms that the instructions above still work https://twitter.com/cpan_author/status/1523944131621437440

    ReplyDelete