Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for installed packages and if not found install

Tags:

I need to check for installed packages and if not installed install them.

Example for RHEL, CentOS, Fedora:

rpm -qa | grep glibc-static glibc-static-2.12-1.80.el6_3.5.i686 

How do I do a check in BASH?

Do I do something like?

if [ "$(rpm -qa | grep glibc-static)" != "" ] ; then 

And what do I need to use for other distributions? apt-get?

like image 984
amanada.williams Avatar asked Oct 09 '12 18:10

amanada.williams


People also ask

How do you check whether a package is installed or not?

The dpkg-query command can be used to show if a specific package is installed in your system. To do it, run dpkg-query followed by the -l flag and the name of the package you want information about.

How do I check if a package is installed in Python?

Start the Anaconda Navigator application. Select Environments in the left column. A dropdown box at the center-top of the GUI should list installed packages. If not, then select Installed in the dropdown menu to list all packages.

How do I check packages in R?

Start R within your package directory, load the devtools library with library(devtools) and then type check() , just as you had used build() and install() to build and install the package.


2 Answers

Try the following code :

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

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 
like image 103
Gilles Quenot Avatar answered Sep 22 '22 10:09

Gilles Quenot


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 $packageName done 
like image 32
Noam Manos Avatar answered Sep 19 '22 10:09

Noam Manos