118

How do I tell CPAN to install all dependencies?

I tried setting these in cpan:

cpan> o conf prerequisites_policy follow
cpan> o conf commit

I still had to answer "y" a couple of times (but fewer than before it feels like).

Is there a way to get it to always go ahead and install? I want to make it unattended.

It would seem that I want a flag to always trust CPAN to do the right thing, and if it suggests an answer I would like to follow it (always hit Enter when it asks something).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Nifle
  • 11,285
  • 9
  • 70
  • 93
  • 1
    Perhaps I should add a feature to my cpan script to set the auto install environment variables based on the CPAN.pm setting. See tsee's answer, which is the other half of the problem. – brian d foy May 26 '09 at 13:19

10 Answers10

83

Try setting PERL_MM_USE_DEFAULT like so:

PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install My::Module'

It should make CPAN answer the default to all prompts.

kbosak
  • 2,187
  • 1
  • 13
  • 16
  • Any way to make this persistent, so you can just type "install My:Module" into cpan each time and it will remember this? Edit Config.pm? – Geoff Feb 16 '12 at 16:27
  • 3
    If you're on linux, add the line `export PERL_MM_USE_DEFAULT=1` to your ~/.bashrc – andersand May 09 '12 at 13:01
  • 1
    If you get an error `Can't locate object method "install" via package "xxx" at -e line 1.` add a "+" right before the module name. – Yann Sagon Mar 28 '14 at 16:12
60

The latest and greatest answer to this question is to use cpanm instead (also referred to as App::cpanminus or cpanminus)!

DESCRIPTION

cpanminus is a script to get, unpack, build and install modules from CPAN and does nothing else.

It's dependency free (can bootstrap itself), requires zero configuration, and stands alone. When running, it requires only 10MB of RAM.

To bootstrap install it:

curl -L http://cpanmin.us | perl - --sudo App::cpanminus

or if you are using perlbrew simply

perlbrew install-cpanm

or from cpan itself:

cpan install App::cpanminus

From then on install modules by executing (as root if necessary)

cpanm Foo::Bar
Joel G Mathew
  • 5,528
  • 12
  • 41
  • 74
Joel Berger
  • 19,816
  • 5
  • 47
  • 101
  • 2
    I have this problem when I use `cpanm`, so telling me to use it is not the solution here. – reinierpost Mar 17 '15 at 17:40
  • 3
    rather than a blind comment, please ask as a new question and link here. I suspect a permissions issue might be your problem – Joel Berger Mar 19 '15 at 18:59
  • On an Ubuntu 18.04 I installed with `sudo apt install cpanminus make`. – Pablo Bianchi Feb 09 '19 at 06:31
  • Down-voting. Don't recommend that people execute the output of curl. – Richard Smith Mar 19 '20 at 22:23
  • Up-voting. "The output of curl" is how to install a great MANY things, from k8s to (on some systems) Docker. The curl is used in context of a legitimate project, not random advice to run curl output, but besides that it is out of scope to impose your personal policy here. Be aware you can always save the curl to disk, inspect it then run it. – Scott Prive Jan 08 '21 at 20:27
34

Here is the one-liner making these changes permanent including automatic first-time CPAN configuration:

perl -MCPAN -e 'my $c = "CPAN::HandleConfig"; $c->load(doit => 1, autoconfig => 1); $c->edit(prerequisites_policy => "follow"); $c->edit(build_requires_install_policy => "yes"); $c->commit'

Or combine it with local::lib module for non-privileged users:

perl -MCPAN -Mlocal::lib=~/perl5 -e 'my $c = "CPAN::HandleConfig"; $c->load(doit => 1, autoconfig => 1); $c->edit(prerequisites_policy => "follow"); $c->edit(build_requires_install_policy => "yes"); $c->commit'

Run it before using the CPAN shell or whatever.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Atento
  • 736
  • 6
  • 7
30

Changing the following parameter on top of prerequisites_policy follows.

cpan> o conf prerequisites_policy 'follow'
cpan> o conf build_requires_install_policy yes
cpan> o conf commit

This will change it from "ask/yes" to "yes" and stop it asking you.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
sdf
  • 301
  • 3
  • 2
28

Here's what I'm pretty sure you're looking for:

cpan> o conf prerequisites_policy follow
cpan> o conf commit
Mark C
  • 697
  • 1
  • 6
  • 15
6

Set

prerequisites_policy

in the configuration.

See Config Variables.

Jose Faeti
  • 11,379
  • 5
  • 37
  • 52
Sinan Ünür
  • 113,391
  • 15
  • 187
  • 326
6

Maybe it's related to ExtUtils::AutoInstall or Module::AutoInstall being used. Try setting the PERL_AUTOINSTALL environment variable. (Cf. the documentation of those modules.)

tsee
  • 4,960
  • 1
  • 17
  • 27
3

I found this to be, by far, the quickest and most reliable way to install CPAN modules:

yes | perl -MCPAN -e "CPAN::Shell->notest(qw!install Your::Module!)"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • This does not answer the question - the OP wants a way to automatically install dependencies / automatically respond to prompts. – William Turrell Jul 29 '15 at 14:14
  • In fact, it does install dependencies and automatically responds to prompts. I use it all the time, including this very moment. But there is a need for a small modification: `yes|/root/bin/perl -MCPAN -e "CPAN::Shell->notest(qw!install Your::Module!)"` – Vladimir Marchenko Jul 29 '15 at 14:16
  • What is "`yes`" supposed to do? – Peter Mortensen Jan 22 '17 at 11:35
3

I'm writing this for benefit of people who may have come to this page searching for a way to install all module dependencies needed by a particular perl script. I wrote a script for that:

It should be run as ./installdep.pl yourscript.pl

#!/usr/bin/perl
`sudo apt install cpanminus`;
while (<>) {
    if (/USE /i)
    {
        my $line=$_;
        $line=~ s/\s//g;
        $line=~ /^(.*)\./;
        $line=~ s/\;//;
        $line=~s/^USE//i;
        $line=~s/lib.*//i;
        $line=~s/feature.*//i;
        $line=~s/strict//i;
        $line=~s/warnings//i;
        $line =~ s/^(.*)\(.*/$1/;
        unless ($line eq '') {
        my $cmd='sudo cpanm '.$line;
        print "Installing $line \n";
        open my $cmd_fh, "$cmd |";
        while (<$cmd_fh>) {
          print "$_";
        }
        close $cmd_fh;
        print "\n";
    }
    }
}

This will use cpanminus to install all module dependencies required by your script. If cpanm isnt installed, it will install it.

Joel G Mathew
  • 5,528
  • 12
  • 41
  • 74
3

Personally I have only tried a couple of times to modify the settings in that way and have actually found it easier to drop into the CPAN.pm shell by giving cpan no arguments, then configuring CPAN.pm from the interactive prompt:

$ cpan
cpan> o conf init

This will initiate the CPAN.pm interfaces configuration utility. Simply run through it and specify "follow" where you need to (I forget the question offhand), and you should be good to go.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
numberwhun
  • 946
  • 9
  • 6
  • You can pass specific settings to `init`. For example, `o conf init prerequisites_policy`. That way, you don't have to go through the long initialization process, but still get the friendly prompt for the setting in question – ikegami Jun 19 '17 at 08:23