How to install Perl Module using CPAN

At times, we need to install Perl modules that are not found in the official repository of a distribution. In that case, we have to revert to using cpan. CPAN stands for Comprehensive Perl Archive Network. It is an archive of over 16,000 modules of software written in Perl, as well as documentation for it. This tutorial will show you how to install a module using CPAN.

First we need to launch cpan. Type cpan in a terminal. If it is not already configured, then it will start to ask a series of questions. Just press Enter all the way until it asks for your location. Specify the location by entering the number for that location in the list shown. Then it will ask about your country, go ahead and type the number appropriate for your country. At the end you will see the cpan prompt like so

cpan>

To install a module, for example, Cache::Static, type the following at cpan prompt

cpan>install Cache::Static

It will ask you some questions, just press Enter to accept the defaults which almost always work. The module with all its dependencies will be downloaded, compiled and installed.

When you get back the cpan prompt, the module is installed.
Type quit to get out of cpan

cpan>quit

To confirm that the module is installed successfully, run this command

perl -e "use Cache::Static"

If you get no output, it means the module is installed successfully.

If you see the error “/bin/sh: cc: command not found”, then gcc is not installed. Install it like this on Debian, Ubuntu

aptitude install gcc

On Red Hat, Centos, Fedore, the following will work

yum install gcc

 

Links:
cpan.org
perl.org

3 Responses to “How to install Perl Module using CPAN”

  1. Warren Melnick Says:

    If you have the cpan rpm installed, as assumed above, installation is as easy as “cpan “. If not you can do “perl -MCPAN -e “install “.

  2. Warren Melnick Says:

    If you have the cpan rpm installed, as assumed above, installation is as easy as “cpan modulename”. If not you can do “perl -MCPAN -e “install modulename”.

  3. Michael Lueck Says:

    In a shared web hosting environment, I encountered that I could not install modules via CPAN as it was trying to access /root/.cpan

    I found the solution to correct this problem here:

    Local CPAN settings
    http://www.perlmonks.org/?node_id=601013

Leave a Reply