Perlbrew for Self-Contained Perl Installations 1

Posted by JD 08/24/2011 at 05:00

For years scripting languages like perl and add-ons like perl modules were supposed to be installed as part of the OS. This worked when the server administrator was the primary programmer on the system who could control all that stuff. In these days of role-based administration, the users and the administrators are hardly ever the same person. Administrators have different needs for systems than users or programmers and often those needs conflict.

How do we solve this problem for Perl scripts? Perlbrew.

Perlbrew

Perlbrew is a tool that compartmentalizes Perl installations. From the perlbrew.pl website:

perlbrew is a tool to manage multiple perl installations in your $HOME directory. They are completely isolated perl universes.

The version of Perl and all the libraries and modules are placed into self contained directory structures. The tool lets you switch between different perl versions pretty easily, managing environment variables for you. Advanced users may have symbolic links changed on the file system. This is fantastic if you are a Perl module creator and want to test your modules with different versions of Perl – enhancing the quality of your code.

Perlbrew handles installations of the different versions of perl too. As of today, there are 9 versions of Perl available to be installed through Perlbrew on my development system.

$ perlbrew available
perl-5.15.2
perl-5.14.1
perl-5.12.4
i perl-5.10.1
perl-5.8.9
perl-5.6.2
perl5.005_04
perl5.004_05
perl5.003_07

I have only 5.10.1 installed on this machine. This is in addition to the version of Perl already installed on the system through a package manager.

Why Is This Great?

Deployment. When it comes time to deploy your fancy new program, you want a self contained installation to drop on the remote server. All packages, all dependencies, controlled by you, not some administrator who is overworked and may patch on a whim. Sometimes those patches will break your beautifully crafted program. That’s bad.

Admins and Package Managers

I don’t really know when it started, but package managers started over stepping their previous bounds when they started including scripting language modules. Package managers don’t point to the native scripting language repository, so now we have 2 different groups trying to maintain modules on a single machine. Some examples:

I’m not anti-package manager – APT and RPM are some of the greatest inventions for computing of all time. However, those package management systems have human limitations. Humans working for a Linux distribution (or volunteers) cannot always get the latest modules for php, python, or perl when it is time to re-publish their latest ISO. The task is simply too big and if you add in testing of all the dependencies, it becomes impossible.

Anyway, when we install a package from the operating system repository, not from the scripting language repository, we’ve just limited our ability to use and maintain the proper bindings for that entire scripting language. We certainly won’t be able to easily install the latest version of a single module inside the package manager. For scripting languages, using the package manager versions is usually bad, unless you are creating trivial programs. This simple little command

$ sudu apt-get install libxml-simple-perl

is the road to hell. This is fine for a causal scripter, but not very nice to the professional.

Package manager maintainers are system admins, not scripting programmers. Unless something in the specific package breaks their code, it won’t be updated. Having 3 yr old versions of some perl modules included with the latest Ubuntu release repositories is common.

Perlbrew solves this issue.

No Administrator Required

Perlbrew doesn’t require admin access to install or use it. Ok, that may not be entirely true – your perl code may needs suid-root to work, but for everything other than that type of issue, you don’t need root/administrator credentials to install or use or maintain Perlbrew or any of the perl environments.

Quick, Sample Uses

Install Perlbrew

Using CPAN is not recommended. I don’t know why.

$ curl -L http://xrl.us/perlbrewinstall | bash 

Follow the instruction on screen to setup your .bashrc or .cshrc to put it in your PATH.

Which Perl Versions Are Available>
$ perlbrew available
Install Perl 5.10.1
$ perlbrew install perl-5.10.1
Install Perl 5.12.4
$ perlbrew install perl-5.12.4
Change Default Perl Version
$ perlbrew switch perl-5.14.1
to make this your default perl going forward.

Or to change the perl version for a single shell instance:

$ perlbrew use 5.10 

Install Common CPAN Modules into Each

The active Perl/cpan version (from the use or switch above) will be used.

$ cpan -i Module::Submodule

Update Perlbrew Itself
$ perlbrew self-upgrade

Links for More Information

Perl invented many of the things that other scripting languages have a core features today, but Perl didn’t invent everything. The really great things from other scripting tools, we stood back and watched, waiting for the best solution to drop out, then stole it.

Perl isn’t the same as it was in the 1990s anymore. It has objects, exceptions, mature modules for testing, logging, dependencies, web design, the greatest ORM available today in any scripting language, and more.

CPAN is simply amazing. The code there is of extreme quality and automatically tested across many, many different platforms to ensure that quality. Error reports at automatically emailed to the module author. Uploaded modules always have built-in tests to ensure the code works. Some tests are just 1 thing – did the module load. Other modules have thousands of test cases. Since the tests are included with the module installation, the module user has lots of example source code in addition to the built-in module documentation. Amazing is an understatement.

New Perl Startup Line

In the new, modern perl, we need to think cross-platform and that includes Windows. Instead of having \#!/usr/bin/perl in our scripts, we need to start using:

  • #!/usr/bin/env perl*

This is really important when you use Perlbrew too. I just spent about an hour troubleshooting a Perl environment issue because my test script used the old-style, not the env-perl style. I’m so embarrassed.

What About Ruby?

I asked for a similar tool on a Ruby IRC channel and got this reply.

Try rvm + bundler.

I’m not enough of a ruby-ist to know if that is equivalent or not.

  1. JD 09/03/2011 at 13:20

    Migrating a Perlbrew setup to a new machine

    If you are like me, you have a few different machines where you work on projects. Sometimes you want to take your latest code with you on a laptop or netbook. I keep 1 development machine updated with perlbrew. It takes over an hour to recreate this manually using CPAN.

    This week I wanted to drop my latest work onto a netbook for a perl monger’s meeting. That netbook only had whatever the distro decided to install, perl-wise.

    Here’s how I was able to clone my dev environment in about 10 minutes (the netbook is wifi connected to the network).

    1. scp -r srv:~/perl5 netbook:
    2. scp -r srv:~/.perbrew netbook:
    3. scp -r srv:~/bin netbook:
    4. run $ perlbrew init
    5. edit your .bashrc, adding the line that perlbrew told you about. (this assumes bash is your shell).
      The last copy was for good measure only, I don’t think it is required, but don’t we all have some handy scripts in our ~/bin dirs?

    At this point, we are missing some dependencies that may matter a bunch. Those compiled libraries that are installed on the source system that aren’t part of the perlbrew setup. I don’t have an answer for those.

    Perl is cross platform, but this method is not. I’ve only tried this with closely related distros. Ubuntu-ish 10.04 and 10.10. I add the “-ish” because none of my installations are really ubuntu, but they do use the ubuntu repositories.

    I’ve made lots of assumptions and tried to stay with the default configurations. If you’ve done something different, like you are sharing the source perlbrew.

    If you use perl, then you probably already tried these things and this is nothing new.
    How can this be improved?