...making Linux just a little more fun!

Installing Perl Modules as a Non-Root User

By Ben Okopnik

Introduction

If you use Perl for anything more complex than the traditional (and boring) generation of "Hello, World", then it's also likely that you're familiar with those wonderful work-saving devices - Perl modules. Furthermore, chances are that you're also familiar with CPAN, the Comprehensive Perl Archive Network, and the easy interface to it that is provided by the CPAN module. That all works just great - assuming that you a) run a sensible modern OS like Linux, and b) have root access to the machine you're using (or have a friendly and cooperative sysadmin). But what if those assumptions don't hold true? What if, for example, you have a shell account on a machine run by some mega-corporation that doesn't feel like installing the Foo::Bar::Zotz::Blagger-0.01 module in their /usr/lib/perl? I, for one, can't blame them; a system-wide installation could open them up to unknown bugs galore. When your interests and theirs conflict, you lose every time - since they own the system.

So, what can we do if we really, really need that module but can't get it installed on a system-wide basis? The answer is to install it elsewhere - in some directory where you have write permissions.

Configuring the Environment

Clearly, if you're not root, you're not going to be able to save the files to their default locations under '/usr/' - which is where they would normally go. Therefore, we need to tell Perl where to find the modules that we'll install. Fortunately, this is the easy part: just decide on the location where you'll install the modules, create that directory if it doesn't already exist, append '/lib' to its name, and set the PERL5LIB variable to that string. E.g., if you're using Bash, then edit your ~/.bash_profile and add the following:

if [ -z "$PERL5LIB" ]
then
	# If PERL5LIB wasn't previously defined, set it...
	PERL5LIB=~/myperl/lib
else
	# ...otherwise, extend it.
	PERL5LIB=$PERL5LIB:~/myperl/lib
fi

MANPATH=$MANPATH:~/myperl/man

export PERL5LIB MANPATH

Now, create the three necessary directories:

mkdir -p ~/myperl/lib
mkdir -p ~/myperl/man/man{1,3}

After you've logged out and back in, Perl will treat that location as a part of @INC (the list of directories to search for libraries and modules.) If you want to confirm that it's actually happened, just execute the following and use your directory name as the argument to 'grep':

perl -wle'print for grep /myperl/, @INC'

Installing the Modules

perl -MCPAN -we 'shell'

First, you'll need to configure the CPAN module. If you've never done this, it's really simple; just execute the above at your command line, and you're on your way. Some people have referred to it as a "pecking chicken" install (that is, if you set a chicken over the 'Enter' key and it then keeps pecking at the key, it'll get through the installation successfully). For our purposes, however, we need to make two small modifications to the standard procedure: when the script asks you for any extra arguments for Makefile.PL, you need to supply it with the following list (assuming that you chose '~/myperl' as your private lib directory):

LIB=~/myperl/lib INSTALLSITEMAN1DIR=~/myperl/man/man1 INSTALLSITEMAN3DIR=~/myperl/man/man3

You also need to make sure that the UNINST parameter is turned off; you can do this by setting 'UNINST=0' when that question comes up during the installation. (This is the default behavior unless you set it, but you might as well make sure.)

If you had already configured the CPAN shell at some point in the past, you simply need to modify the configuration. Again, start the shell as above, and issue the following commands at the "cpan> " prompt:

`` o conf makepl_arg "LIB=~/myperl/lib INSTALLSITEMAN1DIR=~/myperl/man/man1 INSTALLSITEMAN3DIR=~/myperl/man/man3" o conf make_install_arg UNINST=0 o conf commit ''

and you're done. From this point forward, using the CPAN shell (or any of the other CPAN functions and methods) should work just like usual:

# Invoke the shell
perl -MCPAN -we shell

# Install the Net::FTP module
perl -MCPAN -we 'install "Net::FTP"'

# Update all outdated modules on this system
perl -MCPAN -we 'CPAN::Shell->install(CPAN::Shell->r)'

Conclusion

Both the CPAN module and its documentation - which are, incidentally, included as part of the standard Perl installation - have always been good, but they've become even better in the recent years. If you have not taken the time to become familiar with them, you should do so, since the end result will be a great savings in time and effort. The CPAN itself should also be your first stop before you embark on any complex project - I've wasted many an hour trying to accomplish some task only to find out later (or part-way through the project, if I was lucky) that a module to perform that task already exists. Give it a shot - and happy hacking!

Talkback: Discuss this article with The Answer Gang


picture

Ben is the Editor-in-Chief for Linux Gazette and a member of The Answer Gang.

Ben was born in Moscow, Russia in 1962. He became interested in electricity at the tender age of six, promptly demonstrated it by sticking a fork into a socket and starting a fire, and has been falling down technological mineshafts ever since. He has been working with computers since the Elder Days, when they had to be built by soldering parts onto printed circuit boards and programs had to fit into 4k of memory. He would gladly pay good money to any psychologist who can cure him of the recurrent nightmares.

His subsequent experiences include creating software in nearly a dozen languages, network and database maintenance during the approach of a hurricane, and writing articles for publications ranging from sailing magazines to technological journals. After a seven-year Atlantic/Caribbean cruise under sail and passages up and down the East coast of the US, he is currently anchored in St. Augustine, Florida. He works as a technical instructor for Sun Microsystems and a private Open Source consultant/Web developer. His current set of hobbies includes flying, yoga, martial arts, motorcycles, writing, and Roman history; his Palm Pilot is crammed full of alarms, many of which contain exclamation points.

He has been working with Linux since 1997, and credits it with his complete loss of interest in waging nuclear warfare on parts of the Pacific Northwest.


Copyright © 2007, Ben Okopnik. Released under the Open Publication License unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 139 of Linux Gazette, June 2007

Tux