Easy installation of Perl modules (as a non-root user)

Installing Perl modules traditionally with the operating systems package system as root has a lot of disadvantages. Often you will be forced to stick around with outdated versions, and when you want to do an upgrade you need to check that every application and user have their requirements or dependencies met. Or the opposite way but less frequent - the new version of the operating system wont let you run your legacy application that requires an old module. Installing smaller Perl modules via cpan and mixing them with already installed packages in /usr/local/lib can work for short while, but in the end you will most likely end up with a mess with a complete reinstallation of all modules as the result.
Modern languages like Ruby, Java and Python all have different methods to let the user install libraries in the users home folder (or everywhere where the user has write access). Java have jar-files which is very easy to put whereever the users wants as long as its location is added to the classpath. Ruby gems also installs nicely under the running user's home directory when gem is run as a non-privileged user. Out of the box. Its is nice to have different library directories for different users. It makes them independent in the case of an upgrade or a new deployment. Every application should for security reasons be run as seperate user anyway, and of course never as root. 

Unfortunately the newer ways of installing Perl modules is, as far as I could find, not documented in the official documentation of the Perl packages in the different distributions. The tools required is not installed by default either. Lets just cut to the chase: by installing the Perl modules local::lib combined with the more easy to use cpan-minus (App::cpanminus) installing og keeping your Perl modules updated will be as easy (or at least very closely) as using apt on Ubuntu. The method is also very useful if you want to install Perl modules on a system where you don't have root privileges. Lets walk through the setup.

The installation is quite similar for every Unix-like operatin system. including FreeBSD, OS X, Solaris and Linux. You will, of course, need Perl installed, but apart from that keep as few as possible Perl modules installed in the base site_perl library.

As your user (here user test) logged in to the system (development), fetch (or wget if you use Linux) and install local::lib:

test@development ~> fetch \
test@development ~> tar zxf local-lib-1.006007.tar.gz
test@development ~> cd local-lib-1.006007
test@development ~/local-lib-1.006007> perl Makefile.PL --bootstrap 

CPAN will ask you some question but hopefully answer most of them itself. Just press enter on most of them, or correct the settings that seems obviously wrong.

test@development ~/local-lib-1.006007> make

If everything is ok, install it.

test@development ~/local-lib-1.006007> make install

Then you need to set some environment variables to be loaded when your user log in. Simply execute this if you are using tcsh:

perl -I$HOME/perl5/lib/perl5 -Mlocal::lib >> ~/.cshrc

If you are using bash instead of tcsh, execute this:

echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.bashrc

Now, to set them vars, simply log out and in, and check that they are set correctly (these three below need to be present):

test@development ~> env
Next, configure cpan to install dependencies automatically and to download the modules from a nearby mirror:

test@development ~> perl -MCPAN -e shell
cpan[1]> o conf prerequisites_policy follow
cpan[2]> o conf build_requires_install_policy yes
cpan[3]> o conf urllist ftp://ftp.uninett.no/pub/CPAN
cpan[4]> o conf commit
cpan[5]> quit

Install App::cpanminus:

test@development ~> cpan -i App::cpanminus

After some minutes with compiling and installing, the cpanminus will hopefully be installed. It is shipped with a executable 'cpanm' that is placed in your path. Type rehash to reload the path if you're using csh/tcsh.

Lets try to install a small module, take YAML for example (CPAN will complain if its not there, so its good to get it in anyway):

test@development ~> cpanm YAML
--> Working on YAML
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/YAML-0.72.tar.gz ... OK
Configuring YAML-0.72 ... OK
Building and testing YAML-0.72 ... OK
Successfully installed YAML-0.72
test@development ~> 

Now thats better! If you want do to something more complicated with a lot of module dependencies, try installing the nice Catalyst framework:

test@development ~> cpanm Catalyst::Devel

If you want to upgrade a perl module, just run cpanm <module>, and cpanm will upgrade it if there is a new version available in CPAN. 

Last tweets

Follow tagestad on Twitter