Installing modules with cpanminus

[ Perl tips index ]
[ Subscribe to Perl tips ]

One of Perl's greatest advantages is the CPAN — tens of thousands of freely available modules to help you solve almost any problem imaginable. However installing CPAN modules hasn't always been as straightforward as it should be... until cpanminus.

App::Cpanminus, or just cpanm, is a fast, lightweight, pure-perl CPAN installer. Its features are numerous, including:

Installing cpanm is easy. You can go to http://cpanmin.us/ and save page returned to you, or—if you have curl or wget installed—run:

    curl -L http://cpanmin.us | perl - --self-upgrade

or:

    wget -O - http://cpanmin.us | perl - --self-upgrade

Using cpanm with local::lib

The best way to use cpanm is with local::lib, which provides a simple and straightforward way to have a local repository of modules, without need for root access. The easiest way to use local::lib is simply to load the module by itself, and then copy the results into your relevant shell .rc file. For example:

    $ perl -Mlocal::lib
    export PERL_LOCAL_LIB_ROOT="/home/pjf/perl5";
    export PERL_MB_OPT="--install_base /home/pjf/perl5";
    export PERL_MM_OPT="INSTALL_BASE=/home/pjf/perl5";
    export PERL5LIB="/home/pjf/perl5/lib/perl5/i686-linux-gnu-thread-multi:/home/pjf/perl5/lib/perl5:$PERL5LIB";
    export PATH="/home/pjf/perl5/bin:$PATH";

With these commands in your .bashrc (or equivalent) file, there's no need to load local::lib at all; Perl (and cpanm) will be able to find your modules all by themselves.

Installing modules with cpanm

Once you have cpanm, installing a module and its dependencies are easy. For example, to install the most excellent Moose object oriented framework, just run:

    cpanm Moose

Rather than the pages of output you'd expect from other installers, you can expect a very concise report. My system already has Moose, so it gets upgraded:

    Fetching http://search.cpan.org/CPAN/authors/id/D/DO/DOY/Moose-2.0402.tar.gz ... OK
    Configuring Moose-2.0402 ... OK
    Building and testing Moose-2.0402 for Moose ... OK
    Successfully installed Moose-2.0402 (upgraded from 2.0401)

On a fresh system, cpanm would also install Moose and all its dependencies, with absolutely no action required by you whatsoever.

If you wish to install Moose for the whole system, and not just yourself, you can use:

    cpanm --sudo Moose

which will then use sudo to elevate your privileges when required to install modules to the system perl include path.

When things go wrong

In the unlikely case that something goes wrong, there's always a full log of cpanm's actions that you can find in ~/.cpanm/build.log. This can be invaluable in debugging what's gone wrong (often a module with undeclared dependencies), or if you just want to see the full output of the build operations.

Learn more

You can learn more about cpanminus by visiting its metacpan page at https://metacpan.org/module/App::cpanminus.

[ Perl tips index ]
[ Subscribe to Perl tips ]


This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

If you have any questions please don't hesitate to contact us:

Email: contact@perltraining.com.au
Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS