28

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?

4
  • yes | apt-get ...
    – Joshua
    Jul 31, 2016 at 3: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, 2016 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. Jul 31, 2016 at 18:37
  • Also read: superuser.com/a/164580/320611 Mar 18, 2017 at 12:31

3 Answers 3

51

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.
4
  • 4
    I once ran into a package that was immune to --yes. APT didn't ask any questions, but I think a preinst or postinst script was always waiting for some trivial input
    – Xen2050
    Jul 31, 2016 at 9:03
  • 3
    sudo DEBIAN_FRONTEND=noninteractive apt-get -y install foo Jul 31, 2016 at 19:23
  • 1
    @RogerLipscombe I am sorry, can you explain your comment a bit more?
    – Startec
    Jul 31, 2016 at 21:47
  • 3
    The DEBIAN_FRONTEND=noninteractive stops preinst and postinst scripts from asking questions. Aug 1, 2016 at 5:37
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.

1
  • 1
    thank you, I needed this command Apr 6, 2020 at 9:47
1

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

Eg : apt-get install -y nodejs

Eg : apt-get install -y gnupg

Not the answer you're looking for? Browse other questions tagged .