Installing Perl modules required by various open source software is a routine tasks for sysadmins. Installing Perl modules manually by resolving all the dependencies is tedious and annoying process.
Installing Perl modules using CPAN is a better solution, as it resolves all the dependencies automatically. In this article, let us review how to install Perl modules on Linux using both manual and CPAN method.
When a Perl module is not installed, application will display the following error message. In this example, XML::Parser Perl module is missing.
Install Perl Modules Manually
Download Perl module
Go to CPAN Search website and search for the module that you wish to download. In this example, let us search, download and install XML::Parser Perl module. I have downloaded the XML-Parser-2.36.tar.gz to /home/download.
# cd /home/download # gzip -d XML-Parser-2.36.tar.gz # tar xvf XML-Parser-2.36.tar # cd XML-Parser-2.36
Build the perl module and install the perl module
# perl Makefile.PL Checking if your kit is complete... Looks good Writing Makefile for XML::Parser::Expat Writing Makefile for XML::Parser # make # make test # make install
This is very simple for one module with no dependencies. Typically, Perl modules will be dependent on several other modules. Chasing all these dependencies one-by-one can be very painful and annoying task. I recommend the CPAN method of installation as shown below. Use the manual method only if the server is not connected to the Internet.
Install Perl Modules using CPAN automatically
Verify whether CPAN is already installed
To install Perl modules using CPAN, make sure the cpan command is working. You should have the CPAN perl module installed before you can install any other Perl modules using CPAN. In this example, CPAN module is not installed.
# cpan -bash: cpan: command not found # perl -MCPAN -e shell Can't locate CPAN.pm in @INC (@INC contains: /usr/lib/perl5/5.10.0/i386-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi /usr/local/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/site_perl/5.10.0 .). BEGIN failed--compilation aborted.
Install the CPAN module using yum
# yum install perl-CPAN
Configure cpan the first time
The first time when you execute cpan, you should set some configuration parameters as shown below. I have shown only the important configuration parameters below. Accept all the default values by pressing enter.
Note: Make sure to execute “o conf commit” in the cpan prompt after the configuration to save the settings.
# cpan Sorry, we have to rerun the configuration dialog for CPAN.pm due to some missing parameters... CPAN build and cache directory? [/root/.cpan] Download target directory? [/root/.cpan/sources] Directory where the build process takes place? [/root/.cpan/build] Always commit changes to config variables to disk? [no] Cache size for build directory (in MB)?  Let the index expire after how many days?  Perform cache scanning (atstart or never)? [atstart] Cache metadata (yes/no)? [yes] Policy on building prerequisites (follow, ask or ignore)? [ask] Parameters for the 'perl Makefile.PL' command?  Parameters for the 'perl Build.PL' command?  Your ftp_proxy?  Your http_proxy?  Your no_proxy?  Is it OK to try to connect to the Internet? [yes] First, pick a nearby continent and country by typing in the number(s) (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 (1) Bahamas (2) Canada (3) Mexico (4) United States Select your country (or several nearby countries)  4 (2) ftp://carroll.cac.psu.edu/pub/CPAN/ (3) ftp://cpan-du.viaverio.com/pub/CPAN/ (4) ftp://cpan-sj.viaverio.com/pub/CPAN/ (5) ftp://cpan.calvin.edu/pub/CPAN (6) ftp://cpan.cs.utah.edu/pub/CPAN/ e.g. '1 4 5' or '7 1-4 8'  2-16 cpan> o conf commit commit: wrote '/usr/lib/perl5/5.10.0/CPAN/Config.pm' cpan> quit No history written (no histfile specified). Lockfile removed.
Install Perl Modules using CPAN
You can use one of the following method to install a Perl module using cpan.
# /usr/bin/perl -MCPAN -e 'install Email::Reply' (or) # cpan cpan shell -- CPAN exploration and modules installation (v1.9205) ReadLine support available (maybe install Bundle::CPAN or Bundle::CPANxxl?) cpan> install "Email::Reply";
In the example above, Email::Reply is dependent on the several other modules. CPAN automatically resolves the dependencies and installs Email::Reply and all the dependent Perl modules.