Last updated on August 29th, 2024 at 11:50 am
It is been a while since I have played around with Perl. But recently got a requirement to install couple of modules for a user management system written in perl (this was in Ubuntu 18.04). I am also discussing about how to do it in RedHat / CentOS so don’t worry. Basically covering major Linux distributions in this tutorial. The binary we are going to use to easily install perl modules is cpanm. I have also covered some of the options available in cpanm binary as a quick reference below.
I know that CPAN can be used to install modules but getting stuck with dependencies was a major pain. A lot of effort and time needs to invested in just figuring out errors thrown by CPAN (may be it is just me)
Anyway using regular CPAN to install modules, I got a lot of errors similar to the one below (just an example below)
Running make for H/HA/HAARG/local-lib-2.000028.tar.gz
HAARG/local-lib-2.000028.tar.gz
make -- NOT OK
No such file or directory
Can't locate local/lib.pm in @INC (you may need to install the local::lib module) (@INC contains: /home/ubuntu/perl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl/5.26/CPAN/FirstTime.pm line 1357.
Now what?
Did some research in the internet and figured out that there is a repo available named cpanminus. This is a script to get, unpack, build and install modules from CPAN and does nothing else. Let us jump right on how to configure cpanminus. Hope this tutorial will alleviate some of your pain in getting the right modules installed for Perl (without breaking much binaries in the system by installing dependencies).
Before we begin make sure you install “make” ,
If you are using Redhat or Centos try the below commands
yum group install “Development Tools” package (this command will install a lot of package which may not be required for your use case)
OR Just
yum install make
Since we are using Ubuntu run this command to install make package.
$ sudo apt install make
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
make-doc
The following NEW packages will be installed:
make
Starting from below it is going to be same steps for Ubuntu / Redhat / CentOS or any cpanminus supported distribution.
Now that we have make package installed. Next step is to install cpanminus , sharing the complete output for you to may be compare with your installation (if that helps). Use “sudo” to install
$ sudo curl -L https://cpanmin.us | perl - --sudo App::cpanminus
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 295k 100 295k 0 0 4689k 0 --:--:-- --:--:-- --:--:-- 4765k
--> Working on App::cpanminus
Fetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7045.tar.gz ... OK
Configuring App-cpanminus-1.7045 ... OK
Building and testing App-cpanminus-1.7045 ... OK
Successfully installed App-cpanminus-1.7045
1 distribution installed
$
The above command has now installed cpanminus binary on your machine.
You are all set !!
Note: If you get the below error in Redhat/Centos while installing cpanminus, Please run this command yum install perl-ExtUtils-MakeMaker.Then proceed with the installation again.
--> Working on ExtUtils::MakeMaker
Fetching http://www.cpan.org/authors/id/B/BI/BINGOS/ExtUtils-MakeMaker-7.64.tar.gz ... OK
Configuring ExtUtils-MakeMaker-7.64 ... OK
Can't locate ExtUtils/Manifest.pm in @INC (you may need to install the ExtUtils::Manifest module) (@INC contains: FatPacked::94408008947600=HASH(0x55dd13d65790) /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5) at - line 119.
Let us try to run a script and see how to install dependency modules using cpanminus.
Below is an example of an error I got while running a script. This script required OpenSSH module as you can see
$ ./user_migration.pl
Can't locate Net/OpenSSH.pm in @INC (you may need to install the Net::OpenSSH module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at ./user_migration.pl line 2.
BEGIN failed--compilation aborted at ./user_migration.pl line 2.
Next step is to install modules that are required. As per the above error it is the OpenSSH that is missing, use this command cpanm <Module_Name_To_Install>
$ cpanm Net::OpenSSH
--> Working on Net::OpenSSH
Fetching http://www.cpan.org/authors/id/S/SA/SALVA/Net-OpenSSH-0.80.tar.gz ... OK
Configuring Net-OpenSSH-0.80 ... OK
Successfully installed Net-OpenSSH-0.80
1 distribution installed
root@production-server:~#
Now since we have the dependency module installed lets rerun the script. (Output Truncated)
$ ./user_migration.pl
>>OpenSSH Module Found<<
----Starting The Migration----
--Connecting To Backend Server Using SSH---
....
..
Some quick reference on the options available in cpanm
# install Test::More
cpanm Test::More
# full distribution path
cpanm MIYAGAWA/Plack-0.99_05.tar.gz
# install from URL
cpanm http://example.org/LDS/CGI.pm-3.20.tar.gz
# install from a local file
cpanm ~/dists/MyCompany-Enterprise-1.00.tar.gz
# use the fast-syncing mirror
cpanm --mirror http://cpan.cpantesters.org/ DBI
# use only this secure mirror and its index
cpanm -M https://cpan.metacpan.org App::perlbrew
You can go to this github repo ( https://github.com/miyagawa/cpanminus/tree/devel/App-cpanminus )for more details on CPANMINUS
Note: Cpanminus repo is publicly maintained. You can see more details of this package in the github url mentioned above.