Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a bash or perl script to install a number of packages on my debian based machine.

I want it to be something like :

aptitude install package1 
aptitude install package2

But, I do not know how how to automatically say "yes" through the script at the prompt to confirm you want to install that package. Can someone give me an example in perl and bash ?


share|improve this question
up vote 26 down vote accepted

aptitude install -y package1 package2 package3

share|improve this answer
what happens when it needs extra libs ? – Lucas Kauffman Jul 31 '11 at 11:58
Dependencies are resolved automatically, and installed/upgraded as necessary. – Fran Garcia Jul 31 '11 at 12:22
this answer is the one that really answers OP's request +1 – hmontoliu Jul 31 '11 at 14:57

I guess this isn't really a script, but you could install puppet and use the package type. Then it will simply be a matter of creating a file with a bunch of package directives.

For example:

package { zsh:
        ensure => latest

package { vim-enhanced:
        ensure => latest,
        name => $operatingsystem ? {
                'centos' => 'vim-enhanced',
                'ubuntu' => 'vim',


You could also use puppet to customize a bunch of other things like your shell, and your .rc files etc if you get in the mood to extend your scope. It might sound like overkill, but in the long run the little bit of extra effort will likely pay off.

share|improve this answer

In addition to using the -y flag for yes answers you will also want to set DEBIAN_FRONTEND=noninteractive to prevent debconf setting questions from popping up.

DEBIAN_FRONTEND=noninteractive apt-get install PACKAGE -y

That being said, it might be a better road in the long run to follow Kyle's suggestion and take a look at a configuration management system such as Puppet.

share|improve this answer

My answer is a slightly wider scope than the original question, but if you want to do this repeatedly, from scratch, across multiple machines you are setting up then I'd recommend spending the time looking into pxeboot. (I'm assuming you are a debian derivative mentioning aptitude). The learning curve is steep, the documentation a bit patchy but worth persisting if you need it.

If you have to do machines like this on a semi regular basis it's a no brainer. We have machines build from blank to fully configured in around 8 minutes with a ton of custom packages, auto installed.

For bonus points, look into some sort of apt caching mechanism as well.

A perfectly good setup is to make a build server / apt cache out of a virtual machine image to avoid polluting an existing machine

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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