Skip to content

Instantly share code, notes, and snippets.

@pbredenberg
Forked from christopher-hopper/phpenv-macos.sh
Last active April 5, 2020 09:20
Show Gist options
  • Save pbredenberg/4eac95a0940e58012b5ebe9c75eba21e to your computer and use it in GitHub Desktop.
Save pbredenberg/4eac95a0940e58012b5ebe9c75eba21e to your computer and use it in GitHub Desktop.
Install phpenv on OSX from homebrew packages.
#!/usr/bin/env bash
# vim: ai ts=2 sw=2 et sts=2 ft=sh
# Use this script to install or re-install
# multiple versions of PHP on MacOS.
#
# Usage:
# curl -L https://git.io/v52yY | bash
# Check OS.
if [[ "${OSTYPE//[0-9.]/}" != "darwin" ]]; then
(>&2 echo "Error: This script is for MacOS not '${OSTYPE}'.")
exit 1;
fi
# Install homebrew.
if ! command -v brew 1>/dev/null; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
# Intall taps.
brew tap homebrew/php
# Upgrade everything.
brew update
brew upgrade
# Install PHP versions.
for PHP_VER in "5.5" "5.6" "7.0" "7.1" "7.2"; do
brew install "php${PHP_VER/./}"
brew link "php${PHP_VER/./}"
echo "date.timezone = UTC" > "$(brew --prefix)/etc/php/${PHP_VER}/conf.d/date.ini"
# Install PHP extensions.
for PHP_EXT in "mcrypt" "opcache" "xdebug" "yaml"; do
brew install "php${PHP_VER/./}-${PHP_EXT}" 2>/dev/null
done;
# Clean up.
brew unlink "php${PHP_VER/./}"
done;
# Install phpenv.
export PHPENV_ROOT="${HOME}/.phpenv"
curl -fsSL http://git.io/phpenv-installer | bash
# shellcheck disable=SC2016
if ! command -v phpenv 1>/dev/null; then
{ echo 'export PHPENV_ROOT="${HOME}/.phpenv"'
echo 'if [[ -d "${PHPENV_ROOT}" ]]; then'
echo ' export PATH="${PHPENV_ROOT}/bin:${PATH}";'
echo ' eval "$(phpenv init -)";'
echo 'fi'
} >> "${HOME}/.bash_profile"
export PATH="${PHPENV_ROOT}/bin:${PATH}"
eval "$(phpenv init -)"
fi
if [[ ! -d "${HOME}/.phpenv/versions" ]]; then
mkdir -p "${HOME}/.phpenv/versions"
fi
find "${HOME}/.phpenv/versions" -maxdepth 1 -mindepth 1 -type l -delete
# Allow globs to return no results.
shopt -s nullglob
for PHP_PATH in $(brew --prefix)/Cellar/php[0-9][0-9]/*; do
PHP_VERSION=${PHP_PATH##*/};
ln -s "${PHP_PATH}" "${HOME}/.phpenv/versions/" 2>/dev/null
unlink "${HOME}/.phpenv/versions/${PHP_VERSION%.*}" 2>/dev/null
ln -s "${PHP_PATH}" "${HOME}/.phpenv/versions/${PHP_VERSION%.*}" 2>/dev/null
done
phpenv rehash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment