Difference between revisions of "Perl/Local"

From Leaky
Jump to: navigation, search
(Better than perlbrew for a production system)
 
(added package requirements!)
 
Line 1: Line 1:
 
Installing a local copy of Perl and libraries (using local::lib) is the recommended way of setting up an environment for developing a web application. It means you don't need to be root to install more modules, and the modules can be specific versions rather than whatever happens to be available as (often out of date) packages for the operating system.
 
Installing a local copy of Perl and libraries (using local::lib) is the recommended way of setting up an environment for developing a web application. It means you don't need to be root to install more modules, and the modules can be specific versions rather than whatever happens to be available as (often out of date) packages for the operating system.
 +
 +
Ensure you have installed various development packages first!
 +
 +
yum install -y patch gcc-c++ gcc
  
 
The following script can be copied/pasted into a shell script and run. This will build a local version of Perl 5.24.1 (edit PERLVERSION for a different version) and install some of the key modules. After it has finished, you'll need to use ''cpanm ModuleName'' to install any additional modules. Depending on the environment, you may find this takes a while - for example Catalyst::Devel and dependencies can be 20-30 minutes!
 
The following script can be copied/pasted into a shell script and run. This will build a local version of Perl 5.24.1 (edit PERLVERSION for a different version) and install some of the key modules. After it has finished, you'll need to use ''cpanm ModuleName'' to install any additional modules. Depending on the environment, you may find this takes a while - for example Catalyst::Devel and dependencies can be 20-30 minutes!

Latest revision as of 22:46, 22 February 2018

Installing a local copy of Perl and libraries (using local::lib) is the recommended way of setting up an environment for developing a web application. It means you don't need to be root to install more modules, and the modules can be specific versions rather than whatever happens to be available as (often out of date) packages for the operating system.

Ensure you have installed various development packages first!

yum install -y patch gcc-c++ gcc

The following script can be copied/pasted into a shell script and run. This will build a local version of Perl 5.24.1 (edit PERLVERSION for a different version) and install some of the key modules. After it has finished, you'll need to use cpanm ModuleName to install any additional modules. Depending on the environment, you may find this takes a while - for example Catalyst::Devel and dependencies can be 20-30 minutes!

#!/usr/bin/env bash

LOCALDIR=${HOME}/local/
PERLVERSION=5.24.1
PERLINSTALLTARGETDIR=${LOCALDIR}perl-${PERLVERSION}
PERLBUILDURL=https://raw.githubusercontent.com/tokuhirom/Perl-Build/master/perl-build
CPANMURL=http://cpanmin.us/
LOCALPERL=${PERLINSTALLTARGETDIR}/bin/perl${PERLVERSION}
LOCALEXEC=${LOCALDIR}exec
RUNTESTS=--notest
PERLJOBS=9

echo "Installing Perl ${PERLVERSION}"
curl ${PERLBUILDURL} | perl - --jobs ${PERLJOBS} ${RUNTESTS} --noman ${PERLVERSION} ${PERLINSTALLTARGETDIR}

echo "Bootstrapping local::lib"
curl -L ${CPANMURL} | perl - -l ${LOCALDIR} local::lib
eval $(perl -I${LOCALDIR}lib/perl5 -Mlocal::lib=--deactivate-all); \
        curl -L ${CPANMURL} | ${LOCALPERL} - -L ${LOCALDIR} ${RUNTESTS} --reinstall \
        local::lib App::cpanminus App::local::lib::helper

echo "Creating exec program (for cron etc)"
cat > ${LOCALEXEC} <<EOF
#!/usr/bin/env bash
eval \$(perl -I${LOCALDIR}lib/perl5 -Mlocal::lib=--deactivate-all)
source ${LOCALDIR}bin/localenv-bashrc
PATH=${LOCALDIR}bin:${PERLINSTALLTARGETDIR}/bin:\$PATH
export PATH
exec  "\$@"
EOF
chmod 755 ${LOCALEXEC}

# For running commands within the normal shell
cat >> $HOME/.bash_profile <<EOF
eval \$(perl -I${LOCALDIR}lib/perl5 -Mlocal::lib=--deactivate-all)
source ${LOCALDIR}bin/localenv-bashrc
export PATH=${LOCALDIR}bin:${PERLINSTALLTARGETDIR}/bin:\$PATH
EOF

When running a Perl script via cron (scheduled tasks), use the local/exec script to execute it. For example:

10 * * * *      $HOME/local/exec $HOME/application/my_perl_script.pl

If using Apache to serve pages that rely on the local version of Perl, add the following to the relevant Apache config.

SetEnv PATH /home/www/local/bin:/home/www/local/perl-5.24.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
SetEnv PERL5LIB /home/www/local/lib/perl5