It seems that if a package is already installed, "yum install" will try to update it to the latest version. This will cause "yum install" to fail if the latest version is missing dependencies. I know that I can suppress this with --skip-broken, but I don't want to "skip broken" in case of packages which are not already installed; in that case I want it to fail.

I have seen in this answer that I can do this using an "if" statement if I am installing just one package, but I have a "yum install" statement containing some 15 packages, and don't want to run yum multiple times (once for each package) as this adds a lot of overhead.

How can I get yum to only install packages if they are not already present?

1 Answer 1


In the end I wrote a simple bash script yum-install-if-missing.sh based on other answers. Not sure if there is an easier way.


yumcmd="yum install -y"
for var in "$@"
    if ! rpm --quiet --query $var; then
        yumcmd="$yumcmd $var"

echo "ABOUT TO EXECUTE: $yumcmd"

eval $yumcmd

It can then be executed as: yum-install-if-missing.sh packageone packagetwo and so on.

  • 4
    Use rpm -q --quiet packagename rather than piping to grep.
    – Wildcard
    Nov 30, 2017 at 2:02
  • @Wildcard thanks! this really helped me. because I was running yum-install-if-missing.sh ... file ... and while file was not installed, file-libs was installed, so file got skipped. your suggestion fixed this problem
    – Kidburla
    Dec 6, 2017 at 16:59

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.