Perl has a mechanism to import Perl modules which are external software libraries that can be used inside Perl scripts. The CPAN is a public repository of a large number of useful Perl modules contributed by Perl developers worldwide. Since the number of core Perl modules that come with Perl is small, you often need to install external Perl modules from CPAN.
You can search for any Perl module at CPAN. Once you identify the name of the Perl module to install (e.g., HTML::Template), you can install the Perl module as follows.
First, make sure that you have C compiler (e.g., gcc) installed. While most Perl modules are written in Perl, some modules are written in C using XS interface. So you need to set up Linux C development environment.
Next, launch an interactive CPAN shell, and build/install your Perl module as follows.
cpan> install HTML::Template
Alternatively, you can install a Perl module using a simple Perl command line:
If the above commands give you the error saying "Can't locate CPAN.pm in @INC", you first need to install CPAN using a standard Linux package manager as follows.
To install CPAN on CentOS, Fedora or RHEL:
To install CPAN on Ubuntu or Debian:
You should now be able to use "perl -MPCAN" command to manage other Perl modules.
In case you are behind a proxy, and so would like to use CPAN behind proxy, you can use the CPAN console to configure proxy for CPAN as follows.
cpan> o conf init /proxy/
The above command will ask you for proxy server settings, and commit the information on your system.
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!