Chapter 3. Using libraries

3.2. Installing libraries with cpanm

The best way to install modules is with cpanm.

How to install cpanm

Hopefully its already installed, but if not:

curl -L | perl - --sudo App::cpanminus

Basic usage

cpanm --help         # get help
cpanm URI            # install the URI module from CPAN
cpanm DateTime@1.44  # install version 1.44

Installing to a locallib

You will have a different set of dependencies for each project and sometimes requirements conflict between projects. Here is how to install your dependencies to a local directory in your current project:

cpanm --local-lib <path> <module>[@version]
cpanm --local-lib local URI   # install to ./local
cpanm -l local URI            # same but less typing

To use a dependency installed to a locallib, include the path in your $PERL5LIB environment variable:

$ export PERL5LIB=./lib:./local/lib/perl5:$PERL5LIB
$ echo $PERL5LIB

For more information about locallibs see the documentation for the local::lib module.

Best practices

You should almost always install to a locallib rather than the system directory.