How to install and use CPAN modules as a non-root user

It is often preferable to install CPAN modules under a (pseudo-)user account. This avoids having to run any CPAN code as root, makes the install of an application along with its required CPAN modules portable across (similar) systems, and keeps the underlying system “clean”. It's done roughly as follows:

mkdir ~/perl
export PERL5OPT="-I${HOME}/perl/lib/perl5 -I${HOME}/perl/lib/perl5/site_perl"

You may have to type perl -MCPAN -e shell instead of cpan on some systems other than Owl.

In response to the appropriate prompt, enter PREFIX=~/perl. Update: with recent versions of the CPAN shell, this prompt no longer appears by default. To make it appear (along with lots of other prompts), answer no to Would you like to configure as much as possible automatically? Alternatively, exit the CPAN shell after initial configuration is done and edit .cpan/CPAN/ to set:

'makepl_arg' => q[PREFIX=~/perl],

Once in the CPAN shell, type install MODULE, where MODULE is the name of the module to install.

In order for Perl scripts to use modules installed in this way, at least _one_ of the following must be true:

1. PERL5OPT be set to imply the appropriate -I… options to Perl. You can add the following line to ~/.bashrc:

export PERL5OPT="-I${HOME}/perl/lib/perl5 -I${HOME}/perl/lib/perl5/site_perl"

This should work great for scripts invoked manually while logged in.

2. The appropriate -I… options be passed to perl explicitly. This may be done by editing the first line of Perl scripts to become:

#!/usr/bin/perl -I/home/.../perl/lib/perl5 -I/home/.../perl/lib/perl5/site_perl

(also include any other options to perl that the script might have already been using).

3. A use lib … directive may be added to the Perl scripts:

use lib qw(/home/.../perl/lib/perl5 /home/.../perl/lib/perl5/site_perl);

Naturally, the last two options are only needed for CGIs where we couldn't have conveniently set PERL5OPT.

How to build CPAN modules as non-root yet install them globally

Grigoriy used the following approach:

First I installed and tested the modules locally to ~grg/installroot/
by setting the following in ~/.cpan/CPAN/ :

  'make_arg' => q[INSTALLDIRS=vendor DESTDIR=/home/grg/installroot],
  'make_install_arg' => q[INSTALLDIRS=vendor DESTDIR=/home/grg/installroot],

   # only for XML::Parser::Expat:
   # 'makepl_arg' => q[EXPATINCPATH=/opt/libexpat/current/include EXPATLIBPATH=/opt/libexpat/current/lib],

and running:

  $ FTP_PASSIVE=1 PERL5LIB=/home/grg/installroot/usr/lib/perl5/vendor_perl/5.8.3 cpan

Some modules are broken and cannot be compiled or installed automatically (or
it's CPAN's approach used to install modules that is broken), so I installed them
manually by unpacking the tarball, patching/tuning and running:

  $ PERL5LIB=/home/grg/installroot/usr/lib/perl5/vendor_perl/5.8.3 \
      perl Makefile.PL INSTALLDIRS=vendor DESTDIR=/home/grg/installroot

  $ PERL5LIB=/home/grg/installroot/usr/lib/perl5/vendor_perl/5.8.3 \
      make INSTALLDIRS=vendor DESTDIR=/home/grg/installroot install

Then I moved /home/grg/installroot/usr/lib/perl5/vendor_perl/
to /usr/lib/perl5/vendor_perl/ as root.
internal/perl/cpan.txt · Last modified: 2013/11/03 03:21 by solar
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate to DokuWiki Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Powered by OpenVZ Powered by Openwall GNU/*/Linux