Shell – Mac OS X how to verify particular packages are installed

command lineosxpackage-managementshell-script

In a BASH script I need to check if gcc, g++, cpp, make, libpng devel, zlib devel, git, Java (including devel files), ant and pkg-config are available on Mac OS X and if not, I need to prompt user to install them.

This is easy task on Linux as it has package management tools, but I have no idea how to do this in Mac OS X shell. E.g. on openSUSE I can use rpm -q and zypper in, od Debian-based distros dpkg-query and apt-get install. But how to do it on a Mac?

Best Answer

Apple's package management system is often subject to criticism. The utility pkgutil can be used to list and query the package receipts.

List all the packages installed with Apple's installer

     pkgutil --pkgs

Regex for a package id

    pkgutil --pkgs=.\+Xcode.\+

List all the files in a package id

    pkgutil --only-files --files com.apple.pkg.update.devtools.3.2.6.XcodeUpdate

Then again you could use lsbom and read the bom files in /var/db/receipts

Users also install other package management systems such as MacPorts, fink, or Homebrew. Or compile their own in whatever prefix. pkgutil will not list packages installed by these methods.

If your target operating system is OS X10.9 or OS X 10.10 then

    gcc --version

Either the command will output the gcc version or you will be prompted to install the XCode command line tools. gcc, g++, cpp, make, and git will be installed along with other tools. The Java package is offered from Oracle. You could test with java --version though you'll need to familiarize yourself with Apple's frameworks, plugins, and bundles to search for header files. pkgutil would be a good candidate for this process.

The other packages you mentioned could possibly be compiled with in a shell script or compiled and put into an Apple installer package then installed via a shell script. There isn't an easy method.

Related Question