cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

I have Ubuntu installed under Windows Subsystem for Linux 2 (WSL2). There are Perl5 modules installed under /usr/share/perl/5.30.0 and I can access these with a plain use statement. But when I install new modules with cpanminus, it puts them in /home/*****/perl5/lib/perl5/, where the interpreter can't find them unless I have a use lib declaration at the top. Is there some way to get cpanm to install them in the right spot? Here is the relevant output of perl -V. IDK if the entries in %ENV were there before I used cpanm. Otherwise the installation is what came with the distro.
%ENV: + PERL5LIB="/home/*****/perl5/lib/perl5" + PERL_LOCAL_LIB_ROOT="/home/*****/perl5" + PERL_MB_OPT="--install_base "/home/*****/perl5"" + PERL_MM_OPT="INSTALL_BASE=/home/*****/perl5" + @INC: + /home/*****/perl5/lib/perl5/5.30.0/x86_64-linux-gnu-thread-multi + /home/*****/perl5/lib/perl5/5.30.0 + /home/*****/perl5/lib/perl5/x86_64-linux-gnu-thread-multi + /home/*****/perl5/lib/perl5 + /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

Replies are listed 'Best First'.
Re: Why is cpanm installing Perl5 modules in the wrong spot?
by Arunbear (Prior) on Feb 07, 2021 at 16:43 UTC
    cpanm plays it safe by default to avoid interfering with the system Perl, but it does give hints of how to circumvent this e.g.
    +% cpanm -n Attempt ! ! Can't write to /usr/local/share/perl5 and /usr/local/bin: Installing + modules to /home/arun/perl5 ! To turn off this warning, you have to do one of the following: ! - run me as a root or with --sudo option (to install to /usr/local +/share/perl5 and /usr/local/bin) | - run me with --local-lib option e.g. cpanm --local-lib=~/perl5 ! - Set PERL_CPANM_OPT="--local-lib=~/perl5" environment variable (i +n your shell rc file) ! - Configure local::lib in your shell to set PERL_MM_OPT etc. ! --> Working on Attempt Fetching http://search.cpan.org/CPAN/authors/id/M/MA/MARKF/Attempt-1.0 +1.tar.gz ... OK Configuring Attempt-1.01 ... OK Building Attempt-1.01 ... OK Successfully installed Attempt-1.01 1 distribution installed
    i.e. run it as a root or with the --sudo option, but really this is the less safe option.

    Also you can avoid adding a 'use lib ...' by setting the PERL5LIB env variable (which looks already set going by what you've shown above).

      run it as a root or with the --sudo option

      FWIW, I would always use --sudo instead of running cpanm with elevated privs so that the build and test phases are run as the unprivileged user. It is only the installation into the protected directories which requires root access and that's what --sudo does. You are still trusting the module author for that final installation phase, of course.


      🦛

        I suspect the "less safe option" comment wasn't meant to say that running as root is better than using sudo, but rather was an attempt to scare the user away from wanting to do a system-wide install at all. (i.e., "running as root or using sudo is less safe than a local-lib install") Because the current fad is to treat the system perl as if it's made of plutonium and should never be approached or touched in any way at all.
      What made me post this to begin with is that my Komodo IDE/debugger was saying a module I hasd installed with cpanm couldn't be found. But after reading your comment that the ENV variable should take care of this, I created a little test script to run from the command line, and Perl found one of the cpanm modules just fine. So the issue seems to be that that debugger is not reading the ENV variable somehow.