Sunday, December 12, 2010

Day 12: installing CPAN modules locally

Today's entry (and the next three days) is around installing CPAN modules. If you have the sufficent rights ('root' or 'Administrator') you can install directly into the system directories of your Perl installation. I prefer to avoid that. So, a system upgrade does not interfer with my installed modules.

There are a few ways to tell your system Perl where your locally installed modules are:
  • environment variable PERL5LIB
  • 'use lib ...' in your script
  • 'perl -I...' in the command line or shebang line
To install a CPAN module in a custom directory, you can use INSTALL_BASE for ExtUtils::MakeMaker (Makefile.PL) or install_base for Module::Build (Build.PL). I don't want to go into detail here, because I want to show you a much simpler solution: local::lib. It configures your CPAN.pm config and helps you setting the right environment variables for your shell.

You need to follow the bootstrapping technique:
  • download and unpack the tarball from CPAN
  • perl Makefile.PL --bootstrap
  • make test && make install
  • perl -I~/perl5 -Mlocal::lib
  • copy the above output into ~/.bashrc (depending on your shell)
  • open a new shell, so that the new environment variables are present
Now you can use cpan and perldoc like usual, but all installs go into ~/perl5. If you want to run perl scripts outside your shell environment (e. g. in Apache or crontab), set PERL5LIB to the value from your ~/.bashrc.

Links:

1 comment:

  1. You can also do:

    $ eval $(perl -I~/perl5 -Mlocal::lib)

    at your shell prompt, so you don't have to open a new shell.

    In fact I have the above line in .bashrc, so it looks that much less cluttered :)

    ReplyDelete

Note: Only a member of this blog may post a comment.