Automate package installation via shell script for macOS

bashhomebrewscriptterminal

I am trying to write a shell script to automate installation of a bunch of dependencies for a project on macOS which will be an equivalent script for the same that works on EL6/EL7 and uses the yum package manager. This script will try to leverage HomeBrew to do the same.

A few of the tools/dependencies/packages that I want to automate installation are gcc, wget, make, cmake and git and so forth. I want to first check if any of these exist already, then update them or else install them. As you can see in this following snippet I am first trying to check if brew exists already, then update it or else simply install it.

#!/bin/sh

echo Checking brew
#Check if Homebrew is already installed else prompt the user to do so.

BREW_INSTALLED="/usr/local/bin/brew"


#Variables to check if dependencies exist.
HOMEBREW=$(which brew)

echo $HOMEBREW

if [ "$HOMEBREW" == "$BREW_INSTALLED" ]; then
    echo "Brew is already installed."
    echo "Update Brew"
    brew update
else
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi

Now, my questions are (multipart question!)

  1. Is it the right way to use which brew or which X where X can be gcc, make etc. and then compare them with hardcoded /usr/bin/X to check their existence? Since I have quite a list of them I need to check and install so declaring variables for all and cross checking with paths somehow doesn't seem appropriate to me.
  2. How to handle the situation where the user has these packages installed but at some different location for whatever reason they might have? (This seems a bit objective but to ensure the project compiles and builds upon these dependencies should I force these paths only?)
  3. Also, one or more packages might be git repos and need to be cloned, compiled, make installed on the fly. What should be the proper place to put the generated .dylibs and create symlinks to make sure all these interdependent packages can find one another and work in peace?
  4. Is there anything equivalent to virtual_env or jenv to isolate all these installations (of course using HomeBrew they end up in /usr/local/Cellar)? But, as macOS does come with at least a few of them (git, gcc etc.) I don't want to duplicate installations.

P.S. Any help in terms of a direction, resource or an example project working with large amount of dependencies will help. I just can't figure out the right keywords or the right resources to look for a solution or a partial solution. Thanks. 🙂

Best Answer

To piggyback off nohillside, I was gonna say you might be able to just achieve a lot of that by creating your own tap! You can even tap a repo hosted elsewhere (say like a private one, bitbucket, or anything) as long as you specify the git url after brew tap