33

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
    Commented 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
    Commented Jan 19, 2015 at 12:29
  • 1
    the < is shell redirection. For multiple files, use: cat file1 file2 .. | xargs -P1 apt-get install
    – muru
    Commented Jan 19, 2015 at 12:32

5 Answers 5

47

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 -y 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.

0
34

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.

2
  • 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. Commented Apr 29, 2018 at 2:35
  • @TaylorD.Edmiston BusyBox really isn't completely compatible with coreutils... Commented Mar 6, 2023 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
  • 2
    Please don't use backticks. They're rarely the best solution to a problem. I'd suggest sudo apt install $(cat somefile) instead. Commented Mar 6, 2023 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).

0

I know this thread has been quite awhile, but recently I just wrote a tool, sysdep, just for this exact purpose. It is a simple tool that reads from a toml file instead of a plain text file. Suggestion and feedback are welcome!

  1. Download with

    curl -L https://github.com/Benjamin-Tan/sysdep/releases/latest/download/sysdep-$(arch)-unknown-linux-gnu.tar.gz | tar -xz -C ~/.local/bin
    
  2. Create a system_dependencies.toml

    [dependencies]
    apt = ["libpackage", "libpackage2"]
    
  3. sysdep list to list the dependencies, should display 'libpackage', 'libpackage2'.

  4. sysdep install to install them

  5. sysdep to display the help page for more options.

0

You must log in to answer this question.

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