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

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 | 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

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

Step 4. Install a Perl

Firstly, see which versions are available:
$ perlbrew available

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

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

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:
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 = [

and if you want to add something to that library just install cpanminus and you're in full swing!
$ \curl -L | perl - App::cpanminus
$ cpanm Data::Dump
--> Working on Data::Dump
Fetching ... OK
Configuring Data-Dump-1.22 ... OK
Building and testing Data-Dump-1.22 ... OK
Successfully installed Data-Dump-1.22
1 distribution installed


  1. Replies
    1. This is a good description of how plenv is used However, follow the documentation and don't install both perlbrew and plenv on the same server!

  2. 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!

  3. @cpan_author confirms that the instructions above still work