Search code examples
perlperl-modulecpan

Installing Perl modules to a specific location


I have several Linux machines that run Perl programs and other programs and tools.

I want to keep all tools between machines synchronized, so I have shared the /usr/local directory between one machine (Main) and the others.

Now I would like to keep all my Perl modules and their dependencies synchronized as well in /usr/local/<path to modules>.

I have found the local::lib module, but that is intended to install modules to your home directory.

How can I set up CPAN (or CPAN alternatives) to install all modules and dependencies to one location? And how can I set up Perl on other machines to use that location to find modules?


Solution

  • For our convenience, let's assign the base location to a variable: (This var isn't used by anything but the following commands. There's actually no need to export it.)

    export PERL_BASE="/usr/local/perl"   # Or "$HOME" or whatever
    

    Instruct ExtUtils::MakeMaker where to install: (This assumes $PERL_BASE doesn't include any shell metacharacters)

    export PERL_MM_OPT="INSTALL_BASE=$PERL_BASE"
    

    Instruct Module::Build where to install: (This assumes $PERL_BASE doesn't include any shell metacharacters)

    export PERL_MB_OPT="--install_base $PERL_BASE"
    

    Instruct Perl where to look for modules: (This assumes $PERL_BASE doesn't include :)

    export PERL5LIB="$PERL_BASE/lib/perl5"
    

    Instruct the system where to look for scripts: (This assumes $PERL_BASE doesn't include :)

    export PATH="$PERL_BASE/bin${PATH:+:$PATH}"
    

    Instruct the system where to look for man pages: (This assumes $PERL_BASE doesn't include :)

    export MANPATH="$PERL_BASE/man${MANPATH:+:$MANPATH}"
    

    All together:

    export PERL_BASE="/usr/local/perl"
    export PERL_MM_OPT="INSTALL_BASE=$PERL_BASE"
    export PERL_MB_OPT="--install_base $PERL_BASE"
    export PERL5LIB="$PERL_BASE/lib/perl5"
    export PATH="$PERL_BASE/bin${PATH:+:$PATH}"
    export MANPATH="$PERL_BASE/man${MANPATH:+:$MANPATH}"