Install from file containing output from apt list --installed
You can extract the part before the first /
and use that with xargs
to install via apt
:
xargs -a <(awk -F/ '{print $1}' some-file) apt install
Or:
awk -F/ '{print $1}' some-file | xargs apt install -y
APT clearly states that is not designed to use with scripts:
SCRIPT USAGE AND DIFFERENCES FROM OTHER APT TOOLS
The apt(8) commandline is designed as an end-user tool and it may change behavior between versions. While it tries not to break backward compatibility this is not guaranteed either if a change seems beneficial for interactive use.All features of apt(8) are available in dedicated APT tools like apt-get(8) and apt-cache(8) as well. apt(8) just changes the default value of some options (see apt.conf(5) and specifically the Binary scope). So you should prefer using these commands (potentially with some additional options enabled) in your scripts as they keep backward compatibility as much as possible.
You are using wrong tool. Better way is to use dpkg
itself:
dpkg -l | awk '$1 == "ii" {print $2}' > installed
sudo apt-get install $(cat installed)
Details:
-
man apt
locally or online; -
man dpkg
locally or online.
By the way this method looks narrow-minded (like in this great guide on Community). You are loosing big amount of information - package origins and their GPG keys.
Moreover this method will fail on the first occurrence of locally installed package or the package installed from some PPA which does not exist on second machine.
If you want all-inclusive service - try my python script named srslsud
(Save/Restore Software List Script for Ubuntu and Debian Topics Resources).
It will save all APT repositories, their GPG keys; lists of Snaps, Flatpaks and Ubuntu Make apps to JSON file from the first machine.
Then you can restore this list using same JSON file on the second machine.
I actually recently wrote a script on my own machine that, at its core, does exactly that. I does it by first piping to sed 's/\/.*//g'
to remove everything after the slash. Then the output is piped to xargs /usr/bin/sudo apt install
.
As @N0rbert mentioned of his/her solution, the main weakness here is that it takes only one error case (such as a missing repo in sources.list
) to halt the entire operation. Therefore, if you are using a different sources file than the system that generated the input, the solution that you go with will need to first check that the pkg is available to your system. In general, your choice will depend on your the intended use-case(s).
For example, you could probably get away without piping through sed
, but in my case, the script uses tee to save the list to a file, and then does further processing using the file as input. In fact, the list itself is the central theme of that script and a few others that work in tandem with it.
A couple notes: You do not need to worry about checking whether or not the package is already installed on your machine. If it is, apt will simply skip it over. Also, since you are most likely about to install quite a few packages when running, It is a good idea to start out with the sudo apt update && sudo apt upgrade
command. This will save you from downloading a bunch of packages just to end up downloading a slightly newer version for many of them a bit later.