31

I find it very convenient to install packages on a new machine through package files like brewfiles, caskfiles, dockerfiles, package.json etc.

Is there an alternative to this for apt-get since I still just use it
through commandline with

apt-get install pkg1 pkg2 pkg3…

?

3
  • 2
    Does it need one? You could use xargs: xargs -P1 apt-get install < list-of-packages.
    – muru
    Jan 19, 2015 at 10:26
  • @muru well I don't really know how xargs works (reading man now) but I don't think that is a solution to having the packages centralised in one file? I would still have to manually list the files after the < since using a .txt file with three packages in it doesn't work with this command (tested on Debian)
    – thibmaek
    Jan 19, 2015 at 12:29
  • 1
    the < is shell redirection. For multiple files, use: cat file1 file2 .. | xargs -P1 apt-get install
    – muru
    Jan 19, 2015 at 12:32

4 Answers 4

43

As specified in the comments of your question, you can write a simple text file, listing the packages to install:

iceweasel
terminator
vim

Assuming this is stored in packages.txt, then run the following command:

xargs sudo apt-get install <packages.txt

xargs is used to pass the package names from the packages.txt file to the command line. From the xargs manual:

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial arguments followed by items read from standard input.

1
  • 3
    You missed the -y to avoid the apt-get asking confirmation from the user. Mar 15, 2021 at 5:57
33

Improving a bit on the answer of @ortomala-lokni, you can give the file directly as an argument to xargs:

xargs -a packages.txt sudo apt-get install

Optionally you can give xargs the -r option to prevent the apt-get call from being run at all if your packages.txt does not contain any non-whitespace characters.

4
  • This may be a peculiarity of Alpine / BusyBox, but the -a arg wasn't documented and didn't seem to work there: xargs: unrecognized option: a. My version output shown is BusyBox v1.27.2 (2017-12-12 10:41:50 GMT) multi-call binary. Apr 29, 2018 at 2:35
  • 1
    Using the accepted answer resulted in an aborted Y/n query of apt. Using this worked. Thanks!
    – Martin
    Dec 13, 2019 at 19:00
  • @OkLetsdothis You just need to add the -y parameter, see the comment below the accepted answer. Jan 4, 2022 at 9:13
  • @TaylorD.Edmiston BusyBox really isn't completely compatible with coreutils... Mar 6 at 21:53
3
sudo apt install `cat somefile`

Just replace somefile with the name of the file with path, if necessary.  Note that this command uses backquotes.  A backquote will execute the contents and then include the results in the line.

1
  • 1
    Please don't use backticks. They're rarely the best solution to a problem. I'd suggest sudo apt install $(cat somefile) instead. Mar 6 at 21:54
1

As well as the more general scripting solutions, you can create a deb package.

you will need

  • equivs-build, to build the deb. The deb can only contain dependencies. This is a simplified deb builder.
  • gdebi to install the deb.

This will work on all debian based systems (systems that use apt/deb-files).

The advantage of doing it with deb files, as opposed to a shell script, is that it is much easier to undo, you can uninstall a deb, or re-configure a deb, and re-install it (this will cause files to be added and removed).

You must log in to answer this question.

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