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
-
On departmental machines where I am not
root
or any other privileged users. -
Even if I can obtain
root
privilege, installing Perl modules to my home directory makes it easy to move among machines: NFS-ed$HOME
or packing with a tarball.
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 initinside 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
-
mbuildpl_arg
is the argument passing toperl Build.PL
for Module::Build. -
makepl_arg
is the argument passing toperl Makefile.PL
for ExtUtils::MakeMaker. -
prefer_installer
can be eitherMB
(forModule::Build
) orMM
(forMakeMaker
) andMB
is preferred. -
o conf prerequisites_policy follow
makes the CPAN module automatically install ("follow") build/runtime dependencies, which saves us from the chore of confirming installation for each dependency. See CPAN for an explanation. -
o conf commit
stores the configuration in a hash$CPAN::Config
(e.g., in a module$HOME/.cpan/CPAN/MyConfig.pm
, which you can edit by hand). See CPAN for an explanation.
Install Perl Modules
With proper configuration, installation is as simple as either
cpan -i MODULEat system shell or
perl -MCPAN -e 'install PKG'
install MODULEat CPAN shell.
To make CPAN
module easier to use, follow the suggestions here, we install the following modules
cpan -i Term::ReadLine::Perl Term::ReadKeyto 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:$PERL5LIBWe 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/.bashrcWe can check
perl
is aware of PERL5LIB
by perl -le 'print qq(@INC)'