29

I am trying to make a bash script that automates the installation of several packages that I use on any ubuntu machine. I frequently create virtual machines either through Amazon AWS or Digital Ocean and would like to just run one script to install all the packages I use.

Some of the packages I would like to install are Emacs and Node.js

The normal way I install these would be to run apt-get install Emacs, and while doing this I am always prompted with a warning about how much space this app will take up and if I am sure I want to continue.

Is there a way to automate this process, from a script, and always say "yes" to these prompts?

Startec
  • 1,815
  • yes | apt-get ... – Joshua Jul 31 '16 at 03:30
  • 1
    You could also set up an image of a machine after you've set it up the way you want, and then make clones of it when you need new ones. Another option (especially if you have a long list that sometimes changes) is pkgsync. That program takes care of keeping the packages you need installed and up to date, and removing the ones that are not needed or actively unwanted. – ToVine Jul 31 '16 at 13:53
  • 1
    You might want to take a look at Ansible, Puppet, or Chef, which were designed to solve exactly this sort of problem. Ansible in particular is an easy one to get up and running as it operates over SSH. You can either send your own commands to your nodes, or (more likely) use the many modules that have been written to solve common tasks (such as installing packages). Once you have it set up, you only need to run a single command and it ensures that your entire inventory of servers are all in the correct state. – Jon Bentley Jul 31 '16 at 18:37
  • Also read: https://superuser.com/a/164580/320611 – David Refoua Mar 18 '17 at 12:31

3 Answers3

50

From the OPTIONS section of man apt-get

-y, --yes, --assume-yes
    Automatic yes to prompts; assume "yes" as answer to all prompts and
    run non-interactively. If an undesirable situation, such as
    changing a held package, trying to install a unauthenticated
    package or removing an essential package occurs then apt-get will
    abort. Configuration Item: APT::Get::Assume-Yes.
steeldriver
  • 136,215
  • 21
  • 243
  • 336
27

There is a unix command called

yes

Without options it outputs the string "y" repeatedly until killed.

To use it, simply pipe the result to the command where you need the confirmations:

yes | apt-get install ...

Read more in the Unix man pages or in the SO post The “yes” command.

WeSee
  • 371
1

You can add -y To any library install to answer yes

Eg : apt-get install -y nodejs

Eg : apt-get install -y gnupg

vijay
  • 121
  • 5