Score:4

Install from file containing output from apt list --installed

co flag

I have a file that contains the output of apt list --installed from another machine.

i.e.

accountsservice-ubuntu-schemas/focal,focal,now 0.0.7+17.10.20170922-0ubuntu1 all [installed,automatic]
accountsservice/focal-updates,focal-security,now 0.6.55-0ubuntu12~20.04.5 amd64 [installed,automatic]
acl/focal,now 2.2.53-6 amd64 [installed,automatic]

...and so on.

Is there an easy way to install these packages using the aforementioned file and the command line?

muru avatar
us flag
Are you looking to install those specific versions?
Gigi Bayte 2 avatar
co flag
@muru That is a good question. As long as everything in the list of packages has its dependencies taken care of, then I am fine if the versions are the same or newer. (As-is, it can be assumed that dependencies are taken care of.)
Score:5
zw flag

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:

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.

marcelm avatar
cn flag
I love how you say using `apt` isn't intended for direct machine parsing, and then you use a `dpkg` invocation that requires two filter steps before it can be parsed :P - Have you considered `dpkg --get-selections` / `dpkg --set-selections`?
Nate T avatar
it flag
I went with apt to get around errors in the case of already installed packages. I couldn't remember if running `dpkg` directly in these instances will error or not, but I knew for sure that apt would just ignore them. Honestly, the Apt warning has never made sense to me. They complain that updates may break current implementations, but that is true of any software. It is the whole reason for our `X.X.XX` versioning system, distinguishing between breaking and non-breaking changes.
bac0n avatar
cn flag
@NateT output from `apt` is locale-sensitive.
Nate T avatar
it flag
@bac0n I see. That makes sense then. Good to know. Is the locale-based functionality baked into the apt-get logic, or just the interface?
Score:5
us flag

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
Score:1
it flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.