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.


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.

  • 1
    Note that --ignore-missing only workarounds for an issue in download. See superuser.com/a/844627/106974
    – IsaacS
    Apr 5 '16 at 20:22
  • 9
    This actually does not work.
    – leoheck
    Jun 3 '16 at 17:58
  • Why is this selected? It does not work.
    – Akito
    Sep 15 '21 at 11:22
for i in package1 package2 package3; do
  sudo apt-get install $i
  • 6
    A bit brutal performance wise (package dependency tree is being built from the scratch for every iteration), but should work fine. May 3 '11 at 5:53
  • 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.
    – pd12
    May 8 '17 at 12:36

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
  • @Fabby I've edited the text to make it explicit for your satisfaction
    – blueskin
    Jan 23 '17 at 0:43
  • 2
    That looks more like an edit to the existing answer here than a new answer. ¯\_(ツ)_/¯ Sorry! -1...
    – Fabby
    Jan 23 '17 at 14:30

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
  • 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.
    – RichieHH
    May 5 '21 at 7:03
  • @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. May 5 '21 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.