Installing Perl Modules
Written by Chad Lake   
Wednesday, 23 January 2008 08:46

The Unix boxes in the SoC Facility, including both local desktop systems and our public interactive servers, are provided without software outside of what is provided by the operating system. While our current Unix builds provide a ton of useful software, one instance where users might find themselves wanting additional software installed is when using perl modules that might not be installed by default. If a perl module is desired on a particular machine, it is easy enough to install it in your home directory, research space, or any other directory for your own use.

There are two ways to go about installing perl modules in your own directory space. The first uses the command line to download, compile and install a module by hand.  The second uses CPAN (the Comprehensive Perl Archive Network; a local mirror is available at cpan.cs.utah.edu). Either method works, although if you are going to be installing many modules, or modules that have many dependencies, you might find using CPAN to be a big time-saver.

 

INSTALLING PERL MODULES VIA THE COMMAND LINE

  1. Find whatever module you are interested in and download it. Note that we maintain a local mirror of CPAN.
  2. Choose a directory where you want the installed module to go.  These instructions assume that you are going to install your perl modules into ~/lib/perl5 but you can use any directory you like.
  3. Unpack, and compile the module in a temporary directory. This is normally done in a couple of easy steps: running perl on the Makefile.PL file, running make, make test and then make install.  However, when you run perl on the Makefile.PL, append "PREFIX=" to the command line, where is the directory you are installing the module. Note that the perl module will be installed into /lib/perl5/site_perl, so keep that in mind when running the perl command.
  4. Set up your environment variable PERLLIB to contain the directory /lib/perl5/site_perl, or add-I/lib/perl5/site_perl to the #!/usr/bin/perl line in your script.

For example, here is a short and simple perl script that uses the Crypt::IDEA module that is not installed (you don't need to understand what this perl script does, outside of the fact that it is using a module that is not installed):

[514] trust:~:> cat testcrypt.pl
#!/usr/bin/perl -w

use Crypt::CBC;
use Crypt::IDEA;

# This is the key that encrypts/decrypts
my $key = "Eewooqu4otucicuquashausoghazaeth";

# Set up a cipher; we'll need this for getting data for both
# the LDAP password
my $cipher = Crypt::CBC->new(-key => "$key",
-cipher => 'IDEA');

my $pw = $ARGV[0];
chomp $pw;
print $cipher->encrypt("$pw"), "\n";
exit 0;

[515] trust:~:> ./testcrypt.pl
Can't locate Crypt/IDEA.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i586-linux-thread-multi/usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i586-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl .) at ./testcrypt.pl line 4.
BEGIN failed--compilation aborted at ./testcrypt.pl line 4.
[516] trust:~:>

As you can see, I cannot run my perl script because the Crypt::IDEA module is missing. Now I will download, compile and install it. NOTE: I use /home/clake as the directory name instead of just using the ~ character, because as a shell meta-character, the ~ is not always expanded so better to be safe and use a fully-qualified path for the directory:

[574] trust:~:> cd src
[575] trust:~/src:> wget http://cpan.cs.utah.edu/modules/by-module/Crypt/Crypt-IDEA-1.08.tar.gz
--17:57:45--  http://cpan.cs.utah.edu/modules/by-module/Crypt/Crypt-IDEA-1.08.tar.gz
=> `Crypt-IDEA-1.08.tar.gz'
Resolving cpan.cs.utah.edu... 155.98.64.86
Connecting to cpan.cs.utah.edu|155.98.64.86|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7,045 (6.9K) [application/x-tar]

100%[===================================>] 7,045         --.--K/s

17:57:45 (94.63 MB/s) - `Crypt-IDEA-1.08.tar.gz' saved [7045/7045]

[576] trust:~/src:> tar zxf Crypt-IDEA-1.08.tar.gz
[577] trust:~/src:> cd Crypt-IDEA-1.08/
[578] trust:~/src/Crypt-IDEA-1.08:> perl Makefile.PL PREFIX=/home/clake
Checking if your kit is complete...
Looks good
Writing Makefile for Crypt::IDEA
[579] trust:~/src/Crypt-IDEA-1.08:> make
cp IDEA.pod blib/lib/Crypt/IDEA.pod
cp ._test.pl blib/lib/Crypt/._test.pl
cp IDEA.pm blib/lib/Crypt/IDEA.pm
/usr/bin/perl /usr/lib/perl5/5.8.5/ExtUtils/xsubpp  -typemap /usr/lib/perl5/5.8.5/ExtUtils/typemap -typemap typemap  IDEA.xs > IDEA.xsc && mv IDEA.xsc IDEA.c
Please specify prototyping behavior for IDEA.xs (see perlxs manual)
cc -c   -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -pipe   -DVERSION=\"1.08\" -DXS_VERSION=\"1.08\" -fPIC "-I/usr/lib/perl5/5.8.5/i586-linux-thread-multi/CORE"   IDEA.c
IDEA.c: In function `XS_Crypt__IDEA_expand_key':
IDEA.c:35: warning: unused variable `RETVAL'
IDEA.c:36: warning: unused variable `targ'
IDEA.c: In function `XS_Crypt__IDEA_invert_key':
IDEA.c:63: warning: unused variable `RETVAL'
IDEA.c:64: warning: unused variable `targ'
cc -c   -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -pipe   -DVERSION=\"1.08\" -DXS_VERSION=\"1.08\" -fPIC "-I/usr/lib/perl5/5.8.5/i586-linux-thread-multi/CORE"   _idea.c
Running Mkbootstrap for Crypt::IDEA ()
chmod 644 IDEA.bs
rm -f blib/arch/auto/Crypt/IDEA/IDEA.so
LD_RUN_PATH="" cc  -shared IDEA.o _idea.o  -o blib/arch/auto/Crypt/IDEA/IDEA.so
chmod 755 blib/arch/auto/Crypt/IDEA/IDEA.so
cp IDEA.bs blib/arch/auto/Crypt/IDEA/IDEA.bs
chmod 644 blib/arch/auto/Crypt/IDEA/IDEA.bs
Manifying blib/man3/Crypt::IDEA.3pm
[580] trust:~/src/Crypt-IDEA-1.08:> make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl
1..55
ok 1 - (c0) Test object construction isa IDEA
ok 2 - (c0) Test cipher block size
....
ok 54 - (c10) Test Encryption
ok 55 - (c10) Test Decryption
[581] trust:~/src/Crypt-IDEA-1.08:> make install
Installing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/auto/Crypt/IDEA/IDEA.so
Installing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/auto/Crypt/IDEA/IDEA.bs
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/Crypt/IDEA.pod
Installing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/Crypt/._test.pl
Installing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/Crypt/IDEA.pm
Writing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/auto/Crypt/IDEA/.packlist
Appending installation info to /home/clake/lib/perl5/5.8.5/i586-linux-thread-multi/perllocal.pod
[582] trust:~/src/Crypt-IDEA-1.08:>

Now, all I have to do now to use my module is make sure that the environment variable PERL5LIB contains the directory I just installed my module in.  Please read the man page for your shell to find out how to do that on the command line and/or permanently in your dot files. Since I am a bash user, I can easily just temporarily do so like this:

[586] trust:~:> PERL5LIB=/home/clake/lib/perl5/site_perl ./testcrypt.pl foobar
Salted__�z�����(�"�h
[587] trust:~:>

In any case, if you set your PERL5LIB environment variable correctly, you should be able to use all the modules you have installed there.

 

INSTALLING PERL MODULES USING CPAN

CPAN is a tool that makes the installation of perl modules very easy. The CPAN utility will perform the downloading, configuring, compiling and installation of perl modules for you, as well as automatically finding and installing any module dependencies.  The first time you use CPAN, you have to set it up so that it knows how to use your local environment.  These steps are:

  1. Run perl -MCPAN -e shell. This will fire up the perl CPAN shell for you. If this is your first time running this you will be asked a series of questions. It is OK just to hit for every question, which accepts the defaults.  However, two things to take note of: you will want to make sure that /usr/bin is in your path (so that it picks up the location of various helper programs). Also, at the end of the questions you are prompted to choose a CPAN mirror (there is no default for it). You will find our local mirror under North America -> United States -> ftp://cpan.cs.utah.edu. Selecting our local mirror will greatly speed up downloading modules.
  2. Once you have answered all the questions, you will be sitting in a CPAN shell. Type quit to exit the CPAN shell and save the current config. Then fire up a CPAN shell again by running perl -MCPAN -e shell once again. Now you will type in 2 commands that will configure CPAN for your personal use. The first is o conf makepl_arg PREFIX= where you've replacedwith whatever directory you want to install your modules into.
  3. The second configuration command is o conf commit which saves all of these configuration changes so that they are permanent and used as the default every time to start a CPAN shell.
  4. Set up your environment variable PERLLIB to contain the directory /lib/perl5/site_perl, or add-I/lib/perl5/site_perl to the #!/usr/bin/perl line in your script.

Once you have performed these steps you can install/remove perl modules via the CPAN shell (runperl -MCPAN -e shell) or via the cpan command-line program.  For more information, check out Perl.com's page on using the CPAN shell , or read the man page for cpan.

For an example, here is a short and simple perl script that uses the Crypt::IDEA module that is not installed (you don't need to understand what this perl script does, outside of the fact that it is using a module that is not installed):

[514] trust:~:> cat testcrypt.pl
#!/usr/bin/perl -w

use Crypt::CBC;
use Crypt::IDEA;

# This is the key that encrypts/decrypts
my $key = "Eewooqu4otucicuquashausoghazaeth";

# Set up a cipher; we'll need this for getting data for both
# the LDAP password
my $cipher = Crypt::CBC->new(-key => "$key",
-cipher => 'IDEA');

my $pw = $ARGV[0];
chomp $pw;
print $cipher->encrypt("$pw"), "\n";
exit 0;

[515] trust:~:> ./testcrypt.pl
Can't locate Crypt/IDEA.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i586-linux-thread-multi/usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i586-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl .) at ./testcrypt.pl line 4.
BEGIN failed--compilation aborted at ./testcrypt.pl line 4.
[516] trust:~:>

Now I will setup CPAN and install the Crypt::IDEA module using it. Note that I have removed the section where I answer all the questions with the suggested defaults:

[609] trust:~:> /usr/bin/perl -MCPAN -e shell

/home/clake/.cpan/CPAN/MyConfig.pm initialized.

CPAN is the world-wide archive of perl resources....

Now we need to know where your favorite CPAN sites are located. Push
a few sites onto the array (just in case the first on the array won't
work). If you are mirroring CPAN to your local workstation, specify a
file: URL.

First, pick a nearby continent and country (you can pick several of
each, separated by spaces, or none if you just want to keep your
existing selections). Then, you will be presented with a list of URLs
of CPAN mirrors in the countries you selected, along with previously
selected URLs. Select some of those URLs, or just keep the old list.
Finally, you will be prompted for any extra URLs -- file:, ftp:, or
http: -- that host a CPAN mirror.

(1) Africa
(2) Asia
(3) Central America
(4) Europe
(5) North America
(6) Oceania
(7) South America
Select your continent (or several nearby continents) [] 5
Sorry! since you don't have any existing picks, you must make a
geographic selection.

(1) Bahamas
(2) Canada
(3) Mexico
(4) United States
Select your country (or several nearby countries) [] 4
Sorry! since you don't have any existing picks, you must make a
geographic selection.

(1)
(2) ftp://bc1.hpc.lsu.edu/pub/mirrors/CPAN/
(3) ftp://carroll.cac.psu.edu/pub/CPAN/
(4) ftp://cpan-du.viaverio.com/pub/CPAN/
(5) ftp://cpan-sj.viaverio.com/pub/CPAN/
(6) ftp://cpan.calvin.edu/pub/CPAN
(7) ftp://cpan.cs.utah.edu/CPAN/
(8) ftp://cpan.erlbaum.net/CPAN/
(9) ftp://cpan.hexten.net/
(10) ftp://cpan.hostrack.net/pub/CPAN
(11) ftp://cpan.llarian.net/pub/CPAN/
(12) ftp://cpan.mirrors.redwire.net/pub/CPAN/
(13) ftp://cpan.mirrors.tds.net/pub/CPAN
(14) ftp://cpan.netnitco.net/pub/mirrors/CPAN/
(15) ftp://cpan.pair.com/pub/CPAN/
(16) ftp://csociety-ftp.ecn.purdue.edu/pub/CPAN
37 more items, hit SPACE RETURN to show them
Select as many URLs as you like (by number),
put them on one line, separated by blanks, e.g. '1 4 5' [] 7

Enter another URL or RETURN to quit: []
New set of picks:
ftp://cpan.cs.utah.edu/CPAN/


commit: wrote /home/clake/.cpan/CPAN/MyConfig.pm

cpan shell -- CPAN exploration and modules installation (v1.7601)
ReadLine support enabled

cpan> quit
Lockfile removed.
[616] trust:~:>

At this point, I just need to run the few configuration steps to tell CPAN where to install the modules (don't forget to replace my username with your own):

[616] trust:~:> /usr/bin/perl -MCPAN -e shell

cpan shell -- CPAN exploration and modules installation (v1.7601)
ReadLine support enabled

cpan> o conf makepl_arg PREFIX=/home/clake
makepl_arg         PREFIX=/home/clake

cpan> o conf commit
commit: wrote /home/clake/.cpan/CPAN/MyConfig.pm

cpan> install Crypt::IDEA
CPAN: Storable loaded ok
CPAN: LWP::UserAgent loaded ok
Fetching with LWP:
ftp://cpan.cs.utah.edu/CPAN/authors/01mailrc.txt.gz
Going to read /home/clake/.cpan/sources/authors/01mailrc.txt.gz
CPAN: Compress::Zlib loaded ok
Fetching with LWP:
ftp://cpan.cs.utah.edu/CPAN/modules/02packages.details.txt.gz
Going to read /home/clake/.cpan/sources/modules/02packages.details.txt.gz
Database was generated on Wed, 23 Jan 2008 17:30:59 GMT

Fetching with LWP:
ftp://cpan.cs.utah.edu/CPAN/modules/03modlist.data.gz
Going to read /home/clake/.cpan/sources/modules/03modlist.data.gz
Going to write /home/clake/.cpan/Metadata
Running install for module Crypt::IDEA
Running make for D/DP/DPARIS/Crypt-IDEA-1.08.tar.gz
Fetching with LWP:
ftp://cpan.cs.utah.edu/CPAN/authors/id/D/DP/DPARIS/Crypt-IDEA-1.08.tar.gz
CPAN: Digest::MD5 loaded ok
Fetching with LWP:
ftp://cpan.cs.utah.edu/CPAN/authors/id/D/DP/DPARIS/CHECKSUMS
Checksum for /home/clake/.cpan/sources/authors/id/D/DP/DPARIS/Crypt-IDEA-1.08.tar.gz ok
Scanning cache /home/clake/.cpan/build for sizes
Crypt-IDEA-1.08/
Crypt-IDEA-1.08/_idea.c
Crypt-IDEA-1.08/changes
Crypt-IDEA-1.08/COPYRIGHT
Crypt-IDEA-1.08/idea.h
Crypt-IDEA-1.08/IDEA.pm
Crypt-IDEA-1.08/IDEA.pod
Crypt-IDEA-1.08/IDEA.xs
Crypt-IDEA-1.08/Makefile.PL
Crypt-IDEA-1.08/MANIFEST
Crypt-IDEA-1.08/META.yml
Crypt-IDEA-1.08/README
Crypt-IDEA-1.08/._test.pl
Crypt-IDEA-1.08/test.pl
Crypt-IDEA-1.08/typemap

CPAN.pm: Going to build D/DP/DPARIS/Crypt-IDEA-1.08.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for Crypt::IDEA
cp IDEA.pod blib/lib/Crypt/IDEA.pod
cp ._test.pl blib/lib/Crypt/._test.pl
cp IDEA.pm blib/lib/Crypt/IDEA.pm
/usr/bin/perl /usr/lib/perl5/5.8.5/ExtUtils/xsubpp  -typemap /usr/lib/perl5/5.8.5/ExtUtils/typemap -typemap typemap  IDEA.xs > IDEA.xsc && mv IDEA.xsc IDEA.c
Please specify prototyping behavior for IDEA.xs (see perlxs manual)
cc -c   -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -pipe   -DVERSION=\"1.08\" -DXS_VERSION=\"1.08\" -fPIC "-I/usr/lib/perl5/5.8.5/i586-linux-thread-multi/CORE"   IDEA.c
IDEA.c: In function `XS_Crypt__IDEA_expand_key':
IDEA.c:35: warning: unused variable `RETVAL'
IDEA.c:36: warning: unused variable `targ'
IDEA.c: In function `XS_Crypt__IDEA_invert_key':
IDEA.c:63: warning: unused variable `RETVAL'
IDEA.c:64: warning: unused variable `targ'
cc -c   -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -pipe   -DVERSION=\"1.08\" -DXS_VERSION=\"1.08\" -fPIC "-I/usr/lib/perl5/5.8.5/i586-linux-thread-multi/CORE"   _idea.c
Running Mkbootstrap for Crypt::IDEA ()
chmod 644 IDEA.bs
rm -f blib/arch/auto/Crypt/IDEA/IDEA.so
LD_RUN_PATH="" cc  -shared IDEA.o _idea.o  -o blib/arch/auto/Crypt/IDEA/IDEA.so
chmod 755 blib/arch/auto/Crypt/IDEA/IDEA.so
cp IDEA.bs blib/arch/auto/Crypt/IDEA/IDEA.bs
chmod 644 blib/arch/auto/Crypt/IDEA/IDEA.bs
Manifying blib/man3/Crypt::IDEA.3pm
/usr/bin/make  -- OK
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl
1..55
ok 1 - (c0) Test object construction isa IDEA
ok 2 - (c0) Test cipher block size
....
ok 54 - (c10) Test Encryption
ok 55 - (c10) Test Decryption
/usr/bin/make test -- OK
Running make install
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Writing /home/clake/lib/perl5/site_perl/5.8.5/i586-linux-thread-multi/auto/Crypt/IDEA/.packlist
Appending installation info to /home/clake/lib/perl5/5.8.5/i586-linux-thread-multi/perllocal.pod
/usr/bin/make install  -- OK

cpan> quit
Lockfile removed.
[617] trust:~:> PERL5LIB=/home/clake/lib/perl5/site_perl ./testcrypt.pl foobar
Salted__X�N�D���,*�� *�
[618] trust:~:>

 

Remeber, you don't necessarily have to set the PERL5LIB environment variable if you add the -Ioption to the #! line in your perl script.

There are plenty of other resources on the net that explain how to set stuff like this up.  However, if you run into problems or have any questions, feel free to drop us a line at This email address is being protected from spambots. You need JavaScript enabled to view it.anytime.