0

I'm making a script to build a git package in Debian format (.deb). This package requires the following debian packages:

cmake, libturbojpeg0-dev, libjpeg62-turbo-dev, libglx-dev, opencl-headers, libgl-dev, libegl-dev, libx11-dev, libxtst-dev, libglu1-mesa-dev, libxcb-keysyms1-dev, libxcb-glx0-dev and libx11-xcb-dev

So I am using this:

for file in cmake libturbojpeg0-dev libjpeg62-turbo-dev libglx-dev opencl-headers libgl-dev libegl-dev libx11-dev libxtst-dev libglu1-mesa-dev libxcb-keysyms1-dev libxcb-glx0-dev libx11-xcb-dev; do apt -y install $file;done

my question is, how to check if package is already installed and then install if is not installed? Something like this

dpkg -l packagename

if is installed go to next, if not use apt -y install. Thanks

Garo
  • 2,023
  • 9
  • 15
elbarna
  • 12,050
  • 22
  • 92
  • 170
  • 1
    1) Use `apt-get` instead of `apt` in scripts. `apt` is meant for interactive usage. – Garo Nov 13 '22 at 23:59
  • 1
    2) If just use `apt-get -y install package1 package2 package3 ...` it will figure out by itself which packages you already have. No need for the loop and no need for the check. – Garo Nov 14 '22 at 00:01

1 Answers1

3

Instead of writing your own script to do this, you should list the build dependencies in the package’s control file and use mk-build-deps (or any package-resolving build tool, e.g. pbuilder, sbuild etc.). See Automatically install unmet build dependencies as detected by dpkg-checkbuilddeps for details.

If you don’t want to do that, describe the desired state and let apt take care of things:

sudo apt-get install --no-install-recommends cmake libturbojpeg0-dev libjpeg62-turbo-dev libglx-dev opencl-headers libgl-dev libegl-dev libx11-dev libxtst-dev libglu1-mesa-dev libxcb-keysyms1-dev libxcb-glx0-dev libx11-xcb-dev

apt won’t re-install packages which are already installed, but it will upgrade any that have a newer candidate version in the repositories configured on the system (this can be disabled with the --no-upgrade option).

If you really want to write your own script, you could run dpkg -l for each package, and filter on ^ii — packages which don’t match need to be installed:

for package in …; do
    if ! dpkg -l "$package" | grep -q ^ii; then
        sudo apt-get install --no-install-recommends "$package"
    fi
done

(--no-install-recommends skips installing weak dependencies, which is what you want for build dependencies.)

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164