CPAN and Installing Perl Modules

From Dev411: The Code Wiki

There are a number of way to install Perl modules. This page provides information about some of these ways. If you have system rights as a sysadmin or through sudo, you can install Perl modules using CPANPLUS or CPAN. Otherwise check out the manual installation methods. Manual methods are also useful when your system isn't connected to the Internet or the modules you are installing haven't reached CPAN yet (i.e., at http://pause.perl.org/incoming).

Table of contents

Automatic Installation

CPANPLUS.pm (http://search.cpan.org/~kane/CPANPLUS-0.0562/lib/CPANPLUS.pm) and CPAN.pm (http://search.cpan.org/~andk/CPAN-1.80/lib/CPAN.pm) are modules that can be used as an interface to the world wide CPAN mirror servers. CPAN.pm comes bundled with Perl. Some prefer CPANPLUS, however it needs to be installed like any other module. You will typically need system rights to install Perl modules into the system directories this way.

A common issue with CPAN is needing to set FTP to passive mode. To do this see Using CPAN.

CPANPLUS

CPANPLUS is an interface to the CPAN mirror servers. To install CPANPLUS, use CPAN or a manual method.

Command Line Interface:

$ perl -MCPANPLUS -e'install DateTime';

Interactive Shell:

$ perl -MCPANPLUS -eshell
[...]
CPAN Terminal> install DateTime

CPAN

CPAN is the Comprehensive Perl Archive Network. It is also the CPAN.pm module. To install a module with CPAN you can use the command line interface (CLI) or the interactive shell:

Command Line Interface:

$ perl -MCPAN -e 'install DateTime';

Interactive Shell:

$ perl -MCPAN -e shell
[...]
cpan> install DateTime

Manual Installation

Sometimes you may not be able to install automatically using CPANPLUS or CPAN. This is typically the case when you don't have permissions to write to the system Perl directories or don't have an Internet connection. In these cases you can install download the modules and then install them manually.

Given that MakeMaker and Module::Build use different directory structures, it may be easier to keep their directories separate and include both in your path. If you also wish to use the copy files method, you can either copy them into the directory structure used by MakeMaker, Module::Build, or create another directory to place in your include path.

To add these additional directories to your path, you can use the "use lib" pragma in your script or add them to the PERL5LIB environment variable (which is needed for installation under MakeMaker and Module::Build).

MakeMaker

MakeMaker is a common way to build and install Perl modules. First download the tarball from cpan.org, uncompress and extract it. Once you have changed your current directory to the top level directory in the package you will see the file Makefile.PL.

To install a module in a custom location such as in your user directory, you will need to set the PREFIX option shown below. Create ~/lib directory if necessary. MakeMaker will create a set of subdirectories that mimic the Perl directory structure.

PREFIX works on *NIX systems, however, it does not work with some other systems. In those cases, a module may be built with Module::Build which will generate a "Sorry, PREFIX is not supported...." error when using Makefile.PL compatibility mode.

$ PERL5LIB=$HOME/lib/perl5/5.8.7:$HOME/lib/perl5/site_perl/5.8.7

$ perl Makefile.PL PREFIX=~/lib

$ make
$ make install

Module::Build

When a module installs with Module::Build (http://search.cpan.org/~kwilliams/Module-Build-0.2611/lib/Module/Build.pm), you will have to use it's directory structures instead of PREFIX as with MakeMaker. The following will work.

$ PERL5LIB=$PERL5LIB:$HOME/lib/perl5
$ PATH=$PATH:$HOME/bin
$ MANPATH=$MANPATH:$HOME/man

$ perl Build.PL install_base=/home/<user>

$ make
$ make install

Module::Build was specifically designed NOT to use PREFIX, however, it seems that there is enough demand that compatbility is being worked on.

This link provides additional information: Module::Build's Alternatives to PREFIX (http://search.cpan.org/dist/Module-Build/lib/Module/Build.pm#Alternatives_to_PREFIX)

Copy Files

Since many Perl modules are pure Perl and simply files, a direct way to install many Perl modules is to simply copy the file into a location your Perl interpreter can find. If the module is just a single file you download it easily by viewing the source on CPAN.org. If the module is multiple files it will be easier to download and extract the files from the tarball. Simply create a directory structure and add the path to your @INC.

Here's an example for copying in the HTML::Template file.

$ mkdir ~/perl_modules
$ mkdir ~/perl_modules/HTML
$ cp Template.pm ~/perl_modules/HTML
#!perl
use lib '/home/<user>/perl_modules';
use HTML::Template;

See Also

Further Reading / External Links