8

Normally in linux Debian we do sth like this to install a package non-interactively e.g

sudo apt-get install -y Package_x_z

#[-y --assume-yes]

How we can do the same while installing a perl module e.g

sudo perl -MCPAN -e 'install DBI'
Mort
  • 3,379
  • 1
  • 25
  • 40
sakhunzai
  • 13,900
  • 23
  • 98
  • 159

4 Answers4

34

That prompt is (typically) coming from ExtUtils::MakeMaker's prompt() function. Stick export PERL_MM_USE_DEFAULT=1 in your .bashrc (or equivalent for your preferred shell) to stop the prompts. The ExUtils::MakeMaker man page documents it thus:

PERL_MM_USE_DEFAULT

If set to a true value then MakeMaker's prompt function will always return the default without waiting for user input.

Note that this can come to bite you if you run cpan(1) on a box that's not yet had CPAN repositories configured. It will rattle on and get stuck in a prompt loop at a point where there is no default and you need to make a choice, but have no ability to do so. export PERL_MM_USE_DEFAULT=0 in the shell before running cpan(1) will of course temporarily re-enable input.

pndc
  • 3,710
  • 2
  • 23
  • 34
12

To prevent the CPAN client from asking whether to install prerequisites, start it in interactive mode

perl -MCPAN -e shell

and enter the commands:

o conf build_requires_install_policy yes
o conf prerequisites_policy follow
o conf commit

The commit command is optional, but it will update the default configuration, which I suspect is what you want. Without it, you may or may not (depending on whether autocommit is enabled in your CPAN config) need to make this change every time you want to do a prompt-less installation.

These changes will deal with all of the CPAN client's routine questions about whether to install dependencies. For distributions which have questions embedded in their install scripts, you may also want to add

o conf inactivity_timeout 60

to set how long it will wait for a response before automatically going with the default answer to the question. (Set it to 0 to change it back to "wait forever".)

Bret Weinraub
  • 1,943
  • 15
  • 21
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • 1
    Is there any way to do this non-interactively? I have a use case where I need to automate this for several serves, so I'm hoping it's possible to execute this in a single command... – Jay Apr 06 '17 at 14:41
5

What about just :

$ yes | sudo perl -MCPAN -e 'install DBI'

Ban ! your problem is solved :-)

frogatto
  • 28,539
  • 11
  • 83
  • 129
user2144282
  • 59
  • 1
  • 1
0

Appending to an answer here, you can also make these changes in config file located at /usr/share/perl5/CPAN/Config.pm.

'build_requires_install_policy' => q[yes],
'prerequisites_policy' => q[follow],

This helped me to automate installation, since CPAN doesn't have these configuration by default.

stongo
  • 1
  • 2