18 November 2010

Install & Use Perl Modules as Non-Root

Perl is an indispensable tool in my arsenal.

My reasons to install Perl into $HOME are

Configure CPAN Module

Create $HOME/.perl for your local Perl modules.

mkdir $HOME/.perl

Start CPAN shell with either cpan or perl -MCPAN -e shell. Follow the prompt to initialize it. We can come back to the initialization procedure by issuing

o conf init
inside the shell.

Once in the shell, we can poke around by

h  # help for the shell
o conf /mbuild/   # Search for regex /build/ -> Module::Build
o conf /make/  # Search for regex /make/ -> Ext::UtilMakeMaker

Now comes the key configurations

o conf mbuildpl_arg "installdirs=site install_base=$HOME/.perl"
o conf makepl_arg "INSTALLDIRS=site INSTALL_BASE=$HOME/.perl"
o conf prefer_installer MB
o conf prerequisites_policy follow
o conf commit

Install Perl Modules

With proper configuration, installation is as simple as either

cpan -i MODULE
perl -MCPAN -e 'install PKG'
at system shell or
install MODULE
at CPAN shell.

To make CPAN module easier to use, follow the suggestions here, we install the following modules

cpan -i Term::ReadLine::Perl Term::ReadKey
to enable history and command completion functionalities.

Use Local Modules

perlrun explains how perl locates libraries.

With colon-separated (semicolon-separated under Windows) list of search path in PERL5LIB environment variable, we make perl and perdoc aware of our locally installed modules.

We can export PERL5LIB in $HOME/.bashrc, $HOME/.xinitrc, or $HOME/.xsession with

export PERL5LIB=$HOME/.perl:$HOME/.perl/lib/perl5:$PERL5LIB
We do not need to add the architecture-specific directory here because, according to here, "... any architecture-specific directories under the specified locations are automatically included if they exist (this lookup being done at interpreter startup time) ..."

Do not forget to source the script, e.g.,

source $HOME/.bashrc
We can check perl is aware of PERL5LIB by
perl -le 'print qq(@INC)'

Reference

CPAN, Module::Build, ExtUtils::MakeMaker.

Sear Perl documentation.