Installing Program Dependencies Automatically


When you have a habit of compiling software yourself, you may know that quite a few programs out there require quite a number of dependency packages at times. A script such as the following, makes searching for and installing the missing dependencies a piece of cake. All you actually need, is a list of the dependencies themselves as well as the sources they are pulled from.

As an example we take a look at a script that checks for the dependencies of the wine emulator and downloads the missing packages:

#!/bin/sh

#
# Process dependency packages for wine.
#

DEPS=
DEPS_NEEDED_ARRAY=
NUMBER_OF_DEPS_TOTAL=0
NUMBER_OF_DEPS_INSTALLED=0
NUMBER_OF_DEPS_NEEDED=0
decision=

DEPS=(libX11-devel \
freetype-devel \
alsa-lib-devel.i686 \
libsndfile-devel.i686 \
readline-devel.i686 \
glib2.i686 \
glibc-devel.i686 \
libgcc.i686 \
libstdc++-devel.i686 \
pulseaudio-libs-devel.i686 \
cmake \
portaudio-devel.i686 \
audiofile-devel.i686 \
freeglut-devel.i686 \
libieee1284-devel.i686 \
openldap-devel.i686 \
unixODBC-devel.i686 \
sane-backends-devel.i686 \
fontforge \
libgphoto2-devel.i686 \
isdn4k-utils-devel.i686 \
mesa-libGL-devel.i686 \
mesa-libGLU-devel.i686 \
libXxf86dga-devel.i686 \
libXxf86vm-devel.i686 \
giflib-devel.i686 \
cups-devel.i686 \
gsm-devel.i686 \
libv4l-devel.i686 \
fontpackages-devel \
ImageMagick-devel.i686 \
libX11-devel.i686 \
docbook-utils-pdf \
libexttextcat \
texlive-cm-lgc \
alsa-lib-devel \
audiofile-devel.i686 \
audiofile-devel \
cups-devel.i686 \
cups-devel \
dbus-devel.i686 \
dbus-devel \
fontconfig-devel.i686 \
fontconfig-devel \
freetype.i686 \
freetype-devel.i686 \
freetype-devel \
giflib-devel.i686 \
giflib-devel \
lcms2-devel.i686 \
lcms2-devel \
libICE-devel.i686 \
libICE-devel \
libjpeg-turbo-devel.i686 \
libjpeg-turbo-devel \
libpng-devel.i686 \
libpng-devel \
libSM-devel.i686 \
libSM-devel \
libusb-devel.i686 \
libusb-devel \
libX11-devel.i686 \
libX11-devel \
libXau-devel.i686 \
libXau-devel \
libXcomposite-devel.i686 \
libXcomposite-devel \
libXcursor-devel.i686 \
libXcursor-devel \
libXext-devel.i686 \
libXext-devel \
libXi-devel.i686 \
libXi-devel \
libXinerama-devel.i686 \
libXinerama-devel \
libxml2-devel.i686 \
libxml2-devel \
libXrandr-devel.i686 \
libXrandr-devel \
libXrender-devel.i686 \
libXrender-devel \
libxslt-devel.i686 \
libxslt-devel \
libXt-devel.i686 \
libXt-devel \
libXv-devel.i686 \
libXv-devel \
libXxf86vm-devel.i686 \
libXxf86vm-devel \
mesa-libGL-devel.i686 \
mesa-libGL-devel \
mesa-libGLU-devel.i686 \
mesa-libGLU-devel \
ncurses-devel.i686 \
ncurses-devel \
openldap-devel.i686 \
openldap-devel \
openssl-devel.i686 \
openssl-devel \
zlib-devel.i686 \
pkgconfig \
sane-backends-devel.i686 \
sane-backends-devel \
xorg-x11-proto-devel \
glibc-devel \
prelink \
fontforge \
flex \
bison \
libstdc++-devel.i686 \
pulseaudio-libs-devel.i686 \
gnutls-devel.i686 \
libgphoto2-devel.i686 \
openal-soft-devel \
openal-soft-devel.i686 \
isdn4k-utils-devel.i686 \
gsm-devel.i686 \
samba-winbind \
libv4l-devel.i686 \
cups-devel.i686 \
libtiff-devel.i686 \
gstreamer-devel.i686 \
gstreamer-plugins-base-devel.i686 \
gettext-devel.i686 \
libmpg123-devel.i686)

echo "Total of: ${#DEPS[*]} dependencies."
i=0
while [ $i -lt ${#DEPS[*]} ]
do
	yum list installed | grep -i ${DEPS[$i]} > /dev/null 2>&1
	if [ "$?" -ne 0 ] ; then
		DEPS_NEEDED_ARRAY[$NUMBER_OF_DEPS_NEEDED]=${DEPS[$i]}
		NUMBER_OF_DEPS_NEEDED=$((NUMBER_OF_DEPS_NEEDED + 1))
	else
		NUMBER_OF_DEPS_INSTALLED=$((NUMBER_OF_DEPS_INSTALLED + 1))
	fi
	NUMBER_OF_DEPS_TOTAL=$((NUMBER_OF_DEPS_TOTAL + 1))
	i=$((i+1))
done

echo Total number of dependencies: $NUMBER_OF_DEPS_TOTAL
echo Number of dependencies already installed: $NUMBER_OF_DEPS_INSTALLED
echo Number of dependencies needed: ${#DEPS_NEEDED_ARRAY[*]}
echo The dependencies you need are: ${DEPS_NEEDED_ARRAY[*]}

if [ `whoami` = "root" ] ; then
	echo -n "Do you want to install the dependencies now?: [Y/n] "
	read -t 30 decision				# We got 30 seconds to make a choice

        case $decision in

        [Yy][Ee][Ss] | [Yy] )
				yum --enablerepo=epel install ${DEPS_NEEDED_ARRAY[*]}
                ;;
        [Nn][Oo] | [Nn] )
                echo 'Aborted!'
                exit 1
                ;;
        *)
                echo "No input received. Terminating."
                exit 1
                ;;
        esac
fi

# Clean exit
exit 0

If you are calling the script as the root user, then you are asked, if you want to download and install the missing packages. If you create you own script, then you just need to adjust the DEPS variable and the way the are downloaded/installed on your UNIX/Linux system.