How to apt-get install multiple packages without stopping (if not found)?

37,925

Solution 1

I suggest an apt-get option

sudo apt-get --ignore-missing install $list_of_packages

but be aware that missing dependences cannot be ignored, and even if you use --force you can get a broken system.

Solution 2

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

Solution 3

Install each package as a separate command rather than in a single command, this way if one fails to install either due to not found or some other error then it won't stop the others from installing. For which you can use 'for' loop as below. Also, remember to use the -y flag if installing a lot of packages, to avoid the mayhem of typing yes for each one.

INSTALL_PKGS="pk1 pk2 pk3 pk4 pk5 ... and so ... on ..pk_gogol"
for i in $INSTALL_PKGS; do
  sudo apt-get install -y $i
done

Solution 4

Here's a solution that actually works (unlike the accepted answer) and which addresses the performance issue of the correct answer:

Before invoking apt install, filter non-existent packages out of the list. The list of installable packages can be obtained by running apt-cache --generate pkgnames, which we then grep for the packages we want to install, and xargs the result into apt install. The full command looks like this:

apt-cache --generate pkgnames \
| grep --line-regexp --fixed-strings \
  -e package1 \
  -e package2 \
  -e package3 \
  … \
| xargs apt install -y
Share:
37,925

Related videos on Youtube

Hypercube
Author by

Hypercube

Updated on September 18, 2022

Comments

  • Hypercube
    Hypercube almost 2 years

    I am making a bash script that installs various packages using apt-get install. However, I notice that if there is a line that says:

    sudo apt-get install package1 package2 package3
    

    and either of the packages is not found, none of the others will be installed. How do I make it keep going even if it can't find one of the packages?

    Thanks in advance.

  • Serge Broslavsky
    Serge Broslavsky about 13 years
    A bit brutal performance wise (package dependency tree is being built from the scratch for every iteration), but should work fine.
  • leoheck
    leoheck about 8 years
    This actually does not work.
  • Chenna V
    Chenna V over 7 years
    @Fabby I've edited the text to make it explicit for your satisfaction
  • Fabby
    Fabby over 7 years
    That looks more like an edit to the existing answer here than a new answer. ¯\_(ツ)_/¯ Sorry! -1...
  • pd12
    pd12 about 7 years
    As @blueskin alluded to, using the -y option would be good if you didn't want to interact with it, especially when installing a massive list of packages.
  • RichieHH
    RichieHH about 3 years
    The main requirement is to duplicate on another machine so a solution utilising a catalogue from the machine you're duplicating would be nice. Why are you hard coding package names here?? But agreed, I never got any of these solutions to work in the real world.
  • Tomáš Janoušek
    Tomáš Janoušek about 3 years
    @RichieHH The question is literally "how to make sudo apt-get install package1 package2 package3 not fail when one of packageN does not exist?" and so that's what my answer answers.
  • Akito
    Akito almost 3 years
    Why is this selected? It does not work.