How to install Perl modules from CPAN

Last updated on November 21, 2020 by Dan Nanni

Perl has a mechanism to import Perl modules which are external software libraries that can be used inside Perl scripts. 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 in @INC", you first need to install CPAN using a standard Linux package manager as follows.

Install CPAN on CentOS, Fedora or RHEL

$ sudo yum -y install perl-CPAN

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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean