Entries‎ > ‎

Perl virtual environment

While Python as the extremely useful virtualenv tool, I haven't found anything similar for Perl. What I want is quite simple: being able to test random perl tools. They usually depend on a various set of libraries, some of them either too old on my system or not even present. Most of the time, I just want to test the tool, so installing random perl modules system-wide is not an option. Moreover, I really don't like having to install something system-wide which is not coming as a package for my distribution. The solution is to have a virtual environment where I can install all the libraries I want, somewhere in my user directory. This has the advantage too of allowing to use a different version of a library for different perl tools.

While I haven't found an all-in-one tool for perl, there's the CPAN module called local-libwhich helps a lot. You can find its documentation here. Here is how to use it to create a virtual perl environment:

  • ${PERLENV} represents the directory where you want to have your perl virtual env. E.g.:
export PERLENV="$HOME"/perl5 
  • Create the directory and go in it to make our life easier:
mkdir -p "${PERLENV}" cd "${PERLENV}" 
wget -O local-lib.tar.gz "${TARBALL_URL}" 
tar xvf local-lib.tar.gz
cd local-lib-* 
  • Bootstrap it:
perl Makefile.PL --bootstrap="${PERLENV}" 
  • If that's the first time you use CPAN, you will have a message like, that you probably want to confirm:
Would you like me to configure as much as possible automatically? [yes] 
  • Then install local-lib:
make test && make install 

The environment is now more or less setup. However, to use it, you need to set a few environment variables. Here are 2 helpers for that:

  • A wrapper script for perl. Anytime this particular perl wrapper will be called, it will behave like a normal perl, but using your environment:
cat > $PERLENV/perl << EOF
#!/bin/sh
PERLENV="${PERLENV}"
perl -I\$PERLENV/lib/perl5 -Mlocal::lib=\$PERLENV -MCPAN "\$@"
EOF
chmod 755 $PERLENV/perl 
  • If you need to use other programs without an easy way to override the perl binary to use, you can create an activate script which would setup the environment in your current shell. Once created, you will just need to do source "${PERLENV}"/activate and then anything that needs perl in the current shell will use your custom environment.
cat > "${PERLENV}"/activate << EOF
PERLENV="${PERLENV}"
eval \$(perl -I\$PERLENV/lib/perl5 -Mlocal::lib=\$PERLENV -MCPAN "\$@") 
EOF 

Tadam, you local perl is now usable. You can start installing the CPAN modules you want and they will be installed in the environment:

"${PERLENV}"/perl -MCPAN -e 'CPAN::install(POE::Filter::IRCD)' 

Or the CPAN shell:

"${PERLENV}"/perl -MCPAN -e shell 

And if you want to install your favorite perl tool:

source "${PERLENV}"/activate cd myfavoritetool ./runit 

Various notes:

  • I hardly know anything about perl - I might have missed some things. However, this approach works and seems somehow clean.
  • This is not a fully isolated environment; system libraries will be available too. That's usually more an advantage than an issue though.
  • The main issue is that it still relies on your //~/.cpan// configuration directory, so you cannot have CPAN parameters specific to your environment. AFAIK, there's no easy way around that.
Comments