Installing Perl modules to a specific location
Asked Answered
K

4

6

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?

Kress answered 7/1, 2017 at 22:1 Comment(4)
You can tell local::lib where to install things.Ulric
Are you hoping to install modules to be accessed from a different machine? I don't like the sound of that.Inanity
See also INSTALL_BASE seems to be ignored by Makefile.PLBlond
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Also see Where do I post questions about Dev Ops?Cosetta
A
8

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}"
Antiphonary answered 9/1, 2017 at 4:59 Comment(0)
Y
4

local::lib can be set up at any location, the location ~/perl5 is just a default:

# set up PERL_MM_OPT, PERL_MB_OPT, PERL5LIB, and other shell variables
eval "$(perl -I/opt/perllib/lib/perl5 -Mlocal::lib=/opt/perllib)"

The cpanm CPAN installer can install to any location as if it were a local::lib, regardless of how your environment is configured (whether by local::lib or other means):

cpanm -l /opt/perllib Some::Module

You can make use of any local::lib installation without having enabled it by adding the lib dir using PERL5LIB or lib or the -I switch, and using the path to executables in the local::lib bin dir if needed:

PERL5LIB=/opt/perllib/lib/perl5 /path/to/script.pl
perl -Mlib=/opt/perllib/lib/perl5 ./script.pl
perl -I/opt/perllib/lib/perl5 /opt/perllib/bin/mojo version
Yarmouth answered 16/12, 2019 at 17:27 Comment(0)
E
0

Based on my own previous misadventures with local::lib, you should be able to set the location where modules are installed by setting INSTALL_BASE in PERL_MM_OPT, most likely by running a command similar to:

export PERL_MM_OPT='INSTALL_BASE=/usr/local/<path to modules>'

Alternately, if you're doing the Makefile/make/make test/make install process manually instead of using the CPAN toolchain, you can pass --install_base=/usr/local/<path to modules> as an argument to Makefile.PL.

Evildoer answered 8/1, 2017 at 9:23 Comment(1)
'--INSTALL_BASE' is not a known MakeMaker parameter name.?Sleepless
A
-1

you could use the following bash func ...

 do_check_install_perl_modules(){

      
     local_perl5dir=~/perl5
     bash_opts_file=~/.bashrc

     wget -O- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib
     eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
     echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.bashrc
     echo 'export MANPATH=$HOME/perl5/man:$MANPATH' >> ~/.bashrc

     modules="$(cat ${BASH_SOURCE/.func.sh/.lst})"
     while read -r module ; do use_modules="${use_modules:-} use $module ; "; done < <(echo "$modules");

     perl -e "$use_modules" || {
        echo "deploying modules. This WILL at least 4 min, but ONLY ONCE !!!"
        test "$(grep -c 'Mlocal::lib' $bash_opts_file|xargs)" -eq 0 && \
           echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >> $bash_opts_file
                  while read -r module ; do cpanm_modules="${cpanm_modules:-} $module " ; done < <(echo "$modules")
       
       cmd="cpanm --local-lib=$local_perl5dir $modules"
        $cmd
        set +e
                  }
  }
Auxin answered 14/6, 2021 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.