123

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

1
  • 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. May 26, 2009 at 13:19

10 Answers 10

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.

3
  • 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, 2012 at 16:27
  • 3
    If you're on linux, add the line export PERL_MM_USE_DEFAULT=1 to your ~/.bashrc
    – andersand
    May 9, 2012 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, 2014 at 16:12
61

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
5
  • 2
    I have this problem when I use cpanm, so telling me to use it is not the solution here. Mar 17, 2015 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 Mar 19, 2015 at 18:59
  • On an Ubuntu 18.04 I installed with sudo apt install cpanminus make. Feb 9, 2019 at 6:31
  • Down-voting. Don't recommend that people execute the output of curl. Mar 19, 2020 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. Jan 8, 2021 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.

1
  • 1
    isn't it the same as in @sdf anwer?
    – filimonov
    Feb 23, 2016 at 13:48
31

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.

2
  • Thanks! I need these two options so that it did not ask me again and again!
    – Dat TT
    May 20, 2017 at 2:27
  • Note that these are the default values for these settings.
    – ikegami
    Jun 19, 2017 at 8:27
28

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

cpan> o conf prerequisites_policy follow
cpan> o conf commit
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.)

0
6

Set

prerequisites_policy

in the configuration.

See Config Variables.

4
  • 2
    Are the prompts related to downloading module source? If so, see connect_to_internet_ok option. Otherwise, tell us what other prompts you are trying to avoid. May 22, 2009 at 19:02
  • Well all basically, these last ones was "optional modules".
    – Nifle
    May 22, 2009 at 19:19
  • The problem is things that auto-install on their own. CPAN.pm doesn't get a chance to handle that stuff. See tsee's answer. May 26, 2009 at 13:18
  • 1
    Link now appears to be dead
    – OrangeDog
    Mar 4, 2013 at 12:39
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.

1
  • 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, 2017 at 8:23
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!)"
3
  • This does not answer the question - the OP wants a way to automatically install dependencies / automatically respond to prompts. Jul 29, 2015 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!)" Jul 29, 2015 at 14:16
  • What is "yes" supposed to do? Jan 22, 2017 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.