How to install Perl modules using CPAN on Linux

 

CPAN or also known as Comprehensive Perl Archive Network is a repository for modules written in Perl. Currently, there are 177,000+ published modules which can be downloaded and used by the Perl software developers or by the system/network administrators. Today we are going to show you how to install Perl modules manually or by using CPAN on your Linux.

Manual Perl module installation

There are different ways for installing Perl modules. One of the ways is by building the module from source. This way is useful when a single module with no dependencies is required to be installed. First of all, install the Perl language and the dependencies if they are not already installed on your server.

For Ubuntu Cloud VPS:

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install perl build-essential curl

For CentOS Cloud VPS:

yum update
yum install perl perl-devel gcc curl

Once the required software is installed, go to http://search.cpan.org/ and search the module you would like to install. Let’s say you would like to install the DBD::mysql module. Download and unpack the source:

cd /opt/
wget http://search.cpan.org/CPAN/authors/id/M/MI/MICHIELB/DBD-mysql-4.041.tar.gz
tar -xzvf DBD-mysql-4.041.tar.gz
cd DBD-mysql-4.041

Build and install the Perl module using the following commands:

perl Makefile.PL
make
make test 
make install

And that’s it. The module has been successfully installed. Please note that this way of installing Perl modules is not recommended as during the installation no dependencies are being installed. In other words, if the module has dependencies it will not work and you will need to manually install all of them to fix this. Alternatively, you can use your system package manager to install the module and all its dependencies automatically or use the cpan command.

Install Perl modules using CPAN

The CPAN perl module should already be installed on your Linux cloud server by default. In case it is not installed, go ahead and install it. To run cpan you can use the following command:

cpan

If this is the first time you are using cpan you will need to proceed with the basic configuration by answering a few simple questions.

The basic syntax for installing modules is the following:

cpan module_name

To install the same Perl module we installed before you can use the following command:

cpan DBD::mysql

This will also ensure that all module dependencies will be properly installed.

For more installation options as well as basic usage of the module you can check the module’s page at http://search.cpan.org/. For example, the page for the module we installed would be http://search.cpan.org/dist/DBD-mysql/lib/DBD/mysql/INSTALL.pod.

To learn how to interact with CPAN from the command line you can visit the cpan help page using the command below:

cpan -help

Of course, you don’t have to do any of this if you use one of our Linux Cloud VPS Hosting services, in which case you can simply ask our expert Linux admins to install any Perl module for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment