CPAN modules installed locally by cpan/cpanm cannot be used without manual intervention

1k Views Asked by At

I have packaged my app as a Perl module, which fails to install using the cpan or cpanm commands. The problem is that these commands - when not run as root - install the prerequisite modules into the ~/perl5 directory. However, my ~/perl5 directory is not included in the @INC

This leads to the following confusing output from cpanm:

$ cpanm --installdeps .
--> Working on .
Configuring my-module-0.001 ... OK
==> Found dependencies: Image::Size
--> Working on Image::Size
Fetching http://www.cpan.org/authors/id/R/RJ/RJRAY/Image-Size-3.232.tar.gz ... OK
Configuring Image-Size-3.232 ... OK
Building and testing Image-Size-3.232 ... OK
Successfully installed Image-Size-3.232
! Installing the dependencies failed: Module 'Image::Size' is not installed
! Bailing out the installation for my-module-0.001.
1 distribution installed

As you can see, it successfully downloads, tests and installs the Image::Size module, but then tries to use it and fails.

I know I can fix this problem by setting the environment variable $PERL5LIB to "~/perl5/lib/perl5" and adding "~/perl5/bin" to my $PATH, but I'd really like to know how this situation arose in the first place. I would like to keep the installation instructions for my app's users as simple as possible, and manually modifying environment variables is not something I plan to instruct them.

2

There are 2 best solutions below

6
On BEST ANSWER

If you're using bash, you could install local::lib add something like this to your .bashrc file

# adds $HOME/perl5/bin to PATH
[ $SHLVL -eq 1 ] && eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"

That should solve your problem. The issue is the way your local system is configured, so tweak that rather than bringing Dist::Zilla into the mix where it's not required.

3
On

Here is my own solution to the problem. I switched to Dist::Zilla and came up with the following installation instructions for my users:

Installation instructions
=========================

To install this program you need Dist::Zilla, which can be installed
by running the following command as root:

    cpan Dist::Zilla

After that, make sure you're in directory where this INSTALL file is,
and run the following command as root:

    dzil install