MAMP and system php synchronization

I recently decided to start using MAMP again and this time I found myself looking at the ability to specify the version of PHP to use as a real plus. I am actually using MAMP Pro, so it may be a feature of that but since they are stored in the MAMP folder structure I assume that is a feature of MAMP itself and not restricted to the Pro version.

I am wanting to do some things that require specific minimal versions of PHP and I havee found that some of them also are not compatible with newer versions. As time goes on, most will inevitably become compatible with PHP7 for instance but at this time enough are not.

I really am looking forward to being able to select specific versions of PHP. I did, however soon discover that this is only with respect to the MAMP environment. At the command line, “php –version” clearly showed that they were not the same. I did a little digging and found that all the versions that I had downloaded as part of MAMP were neatly tucked away in the MAMP package and that I could easily go to my personal bin folder, one I created in my home directory and added to the front of the PATH environment variable by adding a line, “export PATH=~/bin:$PATH” to the .bashrc file. Because of this, I could easily create a sym-link, called ~/bin/php, to any of the versions of PHP stored in the MAMP package. I could then either remove that sym-link or simply sym-link that file to the system installed file in /usr/bin to get back to the system installed version.

Well, after a bit of playing around I decided that now what I needed was a script that would let me simply tell it which version I wanted to select and it would establish the appropriate sym-link for me. What follows is the script I created and you can get a copy here.

#!/bin/bash

# phpSelect - this script creates a symbolic link in the user's ${HOME_BIN} directory
#             it depends on this directory existing and being the first item in the $(PATH}
#             environment variable.
#
#             It is suggested that this script be placed in your ${PATH} allowing it to be run from anywhere
#
#             The link points to a version of php from the MAMP install
#               OR to the system installed version of php found on the path (excluding the ${HOME_BIN} path)
#
#             At the time this script was created, MAMP would let you "install" various versions of php for use
#             in the MAMP environment, but those were not exposed to the host operating system (OS/X in my case)
#             so trying to do things from a system prompt on projects in the MAMP environment results in a version
#             mismatch. This script lets you easily syncronize the version called from a system prompt with that 
#             used in the project. It also lets you "revert" back to the system installed version if that applies.
#
#        This was created for use on OS/X and would not like work with MAMP's Window's version.
#
#        One would suppose that other virtual environments like XAMP would work simularly and this script could
#        be modified to work with them.
#
#             It is possible to use this script as a basis for establishing sym-links to other commands
#
# author: Steven Hill Sr (www<dot>stevenhillsr<dot>com)
#
# license: Unlicensed <http://unlicense.org>
#

# "phpSelect help" should produce a list of available versions to choose from

# "phpSelect system" will create link to system installed version if there is one, if not produces list of MAMP versions

# if there are no versions of php on the system, either under MAMP or on the path, then you get not found message with empty list to choose from
#     so if you think that it is not working, then
# uncomment next line for debug...
#set -x

HOME_BIN=$(ls -d ~/bin)

# look for a system installed version of php
# Replace colons with spaces to create list.
for dir in ${PATH//:/ }; do
    # capture first location in path
	if test -z ${FIRST_PATH}
	then
		FIRST_PATH=${dir};
	fi
    if test "${dir}" != "${HOME_BIN}"
    then
	    PHP_BINARY="${dir}/php"
		if test ! -d $PHP_BINARY -a -x $PHP_BINARY
		then
			SYS_PHP_BINARY=$PHP_BINARY
			break
		fi
	fi
done

# verify that {HOME_BIN} is first location in path
if test "${FIRST_PATH}" != "${HOME_BIN}"
then
	echo "${HOME_BIN} not first item in path! This script will not work properly."
	exit
fi


MAMP_PATH="/Applications/MAMP/bin/php";

# find the highest version of php within MAMP
for mamp in $(ls $MAMP_PATH); do
    PHP_BINARY="${MAMP_PATH}/${mamp}/bin/php"
	if test ! -d $PHP_BINARY -a -x $PHP_BINARY
	then
		# strip off the 'php' at beginning - depends on naming being php<version>
		HIGHEST_MAMP_VERSION=${mamp#php}
	fi
done

# what version did user ask for?
PHP_VERSION=$1

# check for no specific request and set to MAMP if available or else system
if test -z "${PHP_VERSION}"
then
  if test -z "${HIGHEST_MAMP_VERSION}"
  then
  	PHP_VERSION="system"
  else
  	PHP_VERSION="${HIGHEST_MAMP_VERSION}";
  fi
fi


# determine binary file to use
if test "${PHP_VERSION}" == "system"
then
  PHP_BINARY="${SYS_PHP_BINARY}";
else
  PHP_BINARY="${MAMP_PATH}/php${PHP_VERSION}/bin/php";
fi

# check that the binary does exist and set sym-link to it OR report error
if test ! -d "${PHP_BINARY}" -a -x "${PHP_BINARY}"
then
  rm ${HOME_BIN}/php
  ln -s ${PHP_BINARY} ${HOME_BIN}/php
  echo "Setting php to version ${PHP_VERSION}"
else
  echo "PHP version ${PHP_VERSION} not found, please chose one of these: (do not include 'php' - ie. php5.1.6 is version 5.1.6"
  echo
  echo
  ls ${MAMP_PATH}
  if test ! -z $SYS_PHP_BINARY
  then
    echo
    echo
    echo "    OR choose 'system' to use the system installed version of php"
  fi
fi

echo
echo

I hope this helps someone in some way and if you see anything that could be improved please let me know.

2 thoughts on “MAMP and system php synchronization

  1. I’ve been interested in data and analytics for a long, long time. As a ten-year-old, I remember creating Excel spreadsheets with my dad for his annual Fantasy Football pool. By the time I was 16, I was building Pivot Tables with my cell phone bill to figure out who I texted the most. (I promise I had other hobbies.)

    Since then, I’ve learned how much more there is to data analysis besides Pivot Tables and Excel. There’s a whole world of analytics out there — and I’ve barely scratched the surface.There’s always more to learn, so I’ve made it my mission to learn as much as possible about analytics by talking with people and, of course, consuming as much content as I can online. Here are my favorite go-to resources for continuing my marketing analytics education.

    1) The KISSmetrics Blog

    The KISSmetrics blog is a great resource to learn about marketing analytics, testing, and experimenting with your data. Content is posted daily and will teach you everything from A/B testing to growing your business with analytics to lead generation on your different marketing channels. No matter what company you work at, you’ll be able to learn something about data analysis by reading and following this blog.
    2) “Occam’s Razor” Blog

    Avinash Kaushik is known for his book, Web Analytics 2.0 & Web Analytics: An Hour A Day. “Occam’s Razor” is his blog, where he writes some of the best analyses, explanations, and analytical materials out there. From these posts, you’ll learn how to approach advanced analytics situations and take your data analysis to the next level from a true expert.

    Occam’s Razor by Avinash Kaushik

    3) HubSpot’s Inbound Certification

    HubSpot offers a free inbound marketing certification for anyone interested in learning about how to implement inbound marketing in their business. The curriculum covers many parts of inbound marketing, including analyzing your efforts to make smarter decisions on where to invest in your future. Not only is it free, but it’s available to anyone interested in learning these best practices.

    Academy Certification

    4) Google’s Analytics Academy

    Google Analytics is one of the most popular platforms for companies analyzing their marketing efforts. To help teach people how they should approach their data analysis, Google offers different classes through their Analytics Academy. In addition to courses to teach users how to use Google Analytics, there are also courses that teach about data analysis in general. From mobile analysis to digital analytics, users can take these free courses and learn a lot more about how they should approach their next analytics challenge.

    Google Analytics Academy

    5) Quora’s Analytics Section

    The Analytics section of Quora is one of the best places to get quick analytics help with little effort. In this section, you can search or browse through thousands of common analytics questions and skim through answers from analytics experts. Many of the most respected analytics experts are following and answering topics related to analytics on Quora. And if, you can’t find what you are looking for, you can always ask a question yourself.

    Quora

    6) General Assembly’s Data Analysis Courses

    General Assembly is an online resource offering classes, workshops, courses, and on “the most relevant skills of the 21st century.” Among them are a ton of data analysis classes for all levels, including introductory classes on fundamental modeling techniques and making meaning out of large data sets, and some more advanced classes on data analysis through SQL.

    General Assembly

    7) “Online Behavior” Blog

    Google Analytics Advocate Daniel Waisberg is the founder of Online Behavior, a blog that focuses on marketing measurement and optimization and covers a wide range of topics. Most of the content focuses on how to conduct different types of analysis using Google Analytics. Some focuses on data analysis and reporting in general and provides best practices on these topics. No matter what your interest is, you’re bound to find helpful advice on this blog.

    Online Behavior

    8) The Moz Blog’s Analytics Section

    Moz’s blog covers topics about inbound marketing and SEO — but if you dig into the “Analytics” tag specifically, you’ll find a ton of helpful articles about topics ranging from general best practices to instructions for conducting experiments and analyses with your data. I find the advice on here very actionable, and I think it’d be helpful for readers at every level.

    Moz

    9) Predictive Analytics World Conferences

    Predictive Analytics World is a series of conferences around the world that are focused on analytics. Each event in each different city is focused on its own theme. For example, in Chicago there are two simultaneous events: one focused on manufacturing and one focused on business. In Washington, D.C., the conference is focused on how government agencies use data analysis. Many of the other conferences are focused on business in general and can appeal to anyone interested in learning more about data analysis and upcoming trends in predictive analytics.

    Predictive Analytics World

    BONUS: HubSpot’s “How to Use Excel” Blog Post

    While this is a single blog post rather than an ongoing publication, I still think worth including here as a helpful resource. I get asked about my favorite Excel tips all the time, so I finally decided to compile some of the most common ones into a single blog post listing 14 simple Excel shortcuts, tips, and tricks.

    Excel Tips

    What are your favorite resources for learning more about analytics? Share with us in the comments below.

  2. I thought about marking that comment as spam since it does not really seem to pertain to the post, but after looking it over it does seem to offer some useful information. I have not yet checked out any of the resources mentioned, but I thought I would leave the comment for now.

Leave a Reply

Your email address will not be published. Required fields are marked *