Over the last couple of years, Perl development has been transformed by exciting new technologies such as Catalyst, DBIx::Class, and Moose.
However, these and other such tools all have one thing in common - they are part of the CPAN, not part of the core Perl distribution. For users in certain environments, such as shared hosting, it can be difficult to install CPAN modules without root access to the system. Fortunately, there is a simple solution - local::lib.
local::lib is a Perl module that sets everything up so you can install distributions from CPAN into your home directory. This means you won't need root access, and ensures that anything you install will not interfere with the either the system Perl or any other users.
And of course, local::lib itself can be installed without root permissions, so there's no need to bother your sysadmin at all!
I'm going to demonstrate how to use local::lib on a clean install of Ubuntu 9.04.
The first thing to do is go to http://search.cpan.org/dist/local-lib and download the latest version. Save the .tar.gz archive to your Desktop folder.
Next, open a Terminal session and enter the following commands to unpack the archive:
cd Desktop gunzip local-lib-1.004004.tar.gz tar -xf local-lib-1.004004.tar cd local-lib-1.004004
We now need to tell local::lib we want it to 'bootstrap' itself. This creates a 'perl5' folder within your home directory, and instructs the toolchain to install local::lib there.
perl Makefile.PL --bootstrap
You'll be prompted a couple of times to ask if you want CPAN.pm to be configured automatically, just press Enter to accept the default value ('yes').
When the configuration has finished, enter the following commands which will build and test local::lib.
make make test
If the tests pass, you will see the message 'All tests successful', so local::lib is now ready to install.
make install
We're almost there! There's just one step left, to set some environment variables that tell Perl where your installed modules are kept.
Note that the following command is specific to your command shell and operating system. If you're not using bash and Ubuntu, see the Notes section below for common variations.
echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.bashrc
All done! At this point you should close down any Terminal windows you have running, then open a new Terminal session so that the new settings are picked up.
To prove it all works, let's try installing a simple module. Open a new Terminal window and start the CPAN shell:
perl -MCPAN -eshell
At the CPAN prompt, enter
install Acme::Time::Baby
You will see the module get downloaded, built, tested, and installed - all without any manual . Type 'exit' to leave the CPAN shell, then we can test the installed module with a simple one-liner:
perl -MAcme::Time::Baby -E 'say babytime'
Now it's time for a proper test. Let's install Catalyst.
Catalyst is a big module with lots of dependencies, but now we have CPAN.pm set up that won't be a problem. However, by default the CPAN shell will prompt to ask if each dependency should be installed, which can get quite tedious. So before we start, let's tell CPAN.pm to follow dependencies automatically.
perl -MCPAN -eshell o conf prerequisites_policy follow o conf build_requires_install_policy yes o conf commit
Note that occasionally modules will still ask questions releated to their own configuration, so you should still keep an eye on what's going on.
We're now ready to start the install.
install Catalyst::Devel
CPAN.pm will go through and build all of Catalyst's dependencies, then a short while later you will see the following message to indicate the installation was a success:
FLORA/Catalyst-Devel-1.19.tar.gz /usr/bin/make install -- OK
To prove that everything has worked, let's exit from the CPAN shell and try creating a new Catalyst application.
~/perl5/bin/catalyst.pl MyApp cd MyApp perl Makefile.PL
Note that the 'catalyst.pl' script is in the ~/perl5/bin directory - this is local::lib's default location for installing scripts.
Catalyst applications include a stand-alone development server, so we can run the app by simply entering:
script/myapp_server.pl -r -d
You can now browse to http://localhost:3000 and see the Catalyst test page is all its glory!
The command to set local::lib's environment variables depends on which operating system and command shell you are using. Some common variations are shown below:
Linux / bash: echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.bashrc Mac OS X: echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.profile KSH: echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >>~/.profile csh: perl -I$HOME/perl5/lib/perl5 -Mlocal::lib >> ~/.cshrc
Some Perl modules are written in C, typically to provide a speed boost or to interface with C-based library functions. So to get full use out of CPAN, you'll need a C compiler installed on your machine. Linux and other UNIX systems often come with a C compliler already installed (type 'which cc'), but if not then your vendor should provide a GCC package. On a Mac, installing the Xcode developer tools (from your OS X install DVD) will give you everything you need.
The system I've used for testing, Ubuntu 9.04, includes Perl 5 version 10.0. You can check which version of Perl you have with the command
perl -v
If you have an older release of Perl 5, i.e. anything with a version of v5.8.9 or earlier, you should really ask your system administrator or OS vendor for an upgrade. Perl 5 version 10.0 introduced many new features such as smart matching, named regex captures, a switch statement, and state variables - see http://perldoc.perl.org/perl5100delta.html for the complete list.
If you want to remove all of your installed modules and start again, just delete your local ~/.cpan and ~/perl5 directories:
rm -rf ~/.cpan rm -rf ~/perl5
Then edit your ~/.bashrc file and remove the following line:
eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)
You can then go through the local::lib installation procedure as shown above.
Jon Allen - Perl & Catalyst developer, web designer, and technical manager.
Contact jj@jonallen.info.
Follow me on Twitter at twitter.com/JJ_Perl.