2

This question already has an answer here:

This looks good:

for i in package1 package2 package3; do
  sudo apt-get install -y $i
done

but with the packages listed in a file:

package1
package2
..

each on its own line. Looking for simplest script to read, performance not really an issue. Of course, the odd package will require some human intervention during install to agree or for configuration.

As an aside, what's the "real" way of dealing with large lists of packages to be installed? I'm just looking for monkey-see-monkey-do.

marked as duplicate by dessert, Community Jan 2 at 18:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I don't really understand your question, you could read from the file and run the apt command on each read line! – George Udosen Jan 2 at 13:33
  • LOL, yes, exactly. How? (I'll google that, also, of course.) – Thufir Jan 2 at 13:35
6

There is the xargs program which transforms a file to command-line arguments. Simply prepend xargs to the command (with all arguments) for which you’d like to supply additional arguments from the file (let’s call it list.txt) and let xargs to read your file using standard input redirection.

< list.txt xargs sudo apt-get install -y

You can test it by putting echo before (or instead of) sudo or removing the -y option.

  • 1
    I am sorry, I don’t think there is any way to make this command simpler. Every character in this command has its own purpose. The only thing I can simplify is to write apt instead of apt-get. – Melebius Jan 2 at 14:06
4

Something like this?

# check that the filename was supplied (keeping it simple for the example)
set -o nounset

packagefile=$1

# initialize the package variable
packages=''

# read the lines of the package file
while IFS= read -r line; do
    packs+=" $line"
done < $packagefile

# apt install all of the packages
apt install -y $packs
  • yes, perfect thanks. As an aside, is it possible or advisable to pipe apt search output through a script or utility? Assuming you know ahead of time that the result set is limited. You already answered the question I asked, it's just an aside. – Thufir Jan 2 at 13:38
  • 1
    You can pipe the output but IMO that's really asking for it since you could get many matches that you didn't intend. I'd recommend outputing to a file first (like: apt search some-pack_name > packages) and then editing that file before doing the install. – Eric Mintz Jan 2 at 13:57
  • It's the editing which is annoying. – Thufir Jan 2 at 14:02
  • With bash, it would probably make more sense to slurp all the lines into an array and use that mapfile -t packs < "$packagefile" ; apt-get install -y "${packs[@]}" – steeldriver Jan 2 at 15:28

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