perlcpanlocallibcpanm

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


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.


Solution

  • 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.