How to install modules from CPAN without sudo

perl

Is there a way to install modules from CPAN without resorting to sudo?

(And without perlbrew, please.)

Best Answer

You can configure cpan to install modules outside of the system default path. Point it to some place under your home directory and you shouldn't need to call it with sudo to install modules.

The o command in the cpan interactive shell lets you change options for cpan and the makepl_arg option changes the options that are passed to the perl Makefile.pl call that CPAN makes to build the Makefile for the module.

If you wanted to change the install path to ~/lib/perl5 you'd do:

mkdir -p ~/lib/perl5
perl -MCPAN -e shell

And from the CPAN shell:

cpan> o conf makepl_arg 'PREFIX=~/lib/perl5'
cpan> install MyModule

To use modules installed in this location you'll need to add this path to your PERL5LIB environment variable. For bash do:

export PER5LIB=~/lib/perl5

Or whatever environment variable setting syntax is required by your shell of choice.

There's a nice discussion of customizing CPAN for a single user here.