Checking for installed packages and if not found install Checking for installed packages and if not found install linux linux

Checking for installed packages and if not found install


Try the following code :

if ! rpm -qa | grep -qw glibc-static; then    yum install glibc-staticfi

or shorter :

rpm -qa | grep -qw glibc-static || yum install glibc-static

For debian likes :

dpkg -l | grep -qw package || apt-get install package

For archlinux :

pacman -Qq | grep -qw package || pacman -S package


Based on @GillesQuenot and @Kidbulra answers, here's an example how to loop over multiple packages, and install if missing:

packageList="git gcc python-devel"for packageName in $packageList; do  rpm --quiet --query $packageName || sudo yum install -y $packageNamedone


if [ $(yum list installed | cut -f1 -d" " | grep --extended '^full name of package being checked$' | wc -l) -eq 1 ]; then  echo "installed";else  echo "missing"fi

I use this because it returns installed / missing without relying on an error state (which can cause problems in scripts taking a "no tolerance" approach to errors via

set -o errexit

for example)