How to install Perl modules from CPAN

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.

$ sudo perl -MCPAN -e shell
cpan> install HTML::Template

Alternatively, you can install a Perl module using a simple Perl command line:

$ sudo perl -MCPAN -e 'install HTML::Template'

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:

$ sudo yum -y install perl-CPAN

To install CPAN on Ubuntu or Debian:

$ sudo apt-get install perl-modules

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.

$ sudo perl -MCPAN -e shell
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.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *