-
-
Save edufabra/1db068befcd27afee1e5a0dd668a896d to your computer and use it in GitHub Desktop.
Install phpenv on macOS with homebrew php packages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# vim: ai ts=2 sw=2 et sts=2 ft=sh | |
# Install or re-install phpenv with | |
# multiple versions of PHP on macOS. | |
# | |
# Usage: | |
# | |
# curl -L https://git.io/JvG7i | bash | |
# | |
# Bash strict mode. | |
set -o pipefail | |
set -o errexit | |
set -o nounset | |
# Allow empty globs. | |
shopt -s nullglob | |
IFS=$' ' | |
# Check OS. | |
if [[ "${OSTYPE//[0-9.]/}" != "darwin" ]]; then | |
(>&2 echo "Error: This script is for macOS not '${OSTYPE}'.") | |
exit 1; | |
fi | |
brew_install() { | |
# 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 | |
} | |
php_install() { | |
local PHP_VER; | |
local PHP_EXT; | |
brew_install | |
# Install PHP versions. | |
for PHP_VER in "7.4" "8.0" "8.1" "8.2" "8.3"; do | |
# Do not overwrite existing php install. | |
if brew list --versions "php@${PHP_VER}" 1>/dev/null; then | |
echo "\x1B[36m\t ℹ️ Skipping installation of PHP ${PHP_VER} because it is already installed\x1B[39m" | |
# todo: backup global composer config and update php version. | |
continue | |
fi | |
echo "\x1B[36m\t ℹ️ Installing PHP ${PHP_VER}\x1B[39m" | |
brew install "shivammathur/php/php@${PHP_VER}" || true | |
echo "date.timezone = 'Europe/Madrid'" > "$(brew --prefix)/etc/php/${PHP_VER}/conf.d/date.ini" | |
# Install PHP extensions. | |
IFS=$' ' | |
for PHP_EXT in "opcache" "xdebug" "yaml" "zlib" "zip" "pdo_mysql" "pdo_pgsql" "bcmath"; do | |
echo "\x1B[36m\t\t - Installing PHP ${PHP_VER} extension ${PHP_EXT} \x1B[39m" | |
brew install "php${PHP_VER/./}-${PHP_EXT}" 2>/dev/null || true | |
done; | |
# Cleaning up. | |
brew unlink "php@${PHP_VER}" || true | |
done; | |
echo "\x1B[36m\t 📌 Now you can proceed to install PHPEnv \x1B[39m" | |
} | |
phpenv_install() { | |
# Install phpenv. | |
export PHPENV_ROOT="${HOME}/.phpenv" | |
# shellcheck disable=SC2016 | |
if ! command -v phpenv 1>/dev/null; then | |
echo "\x1B[36m\t ℹ️ Installing PHPEnv \x1B[39m" | |
( curl -fsSL https://raw.githubusercontent.com/phpenv/phpenv-installer/master/bin/phpenv-installer | bash ) || true | |
{ 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}/.zshrc" | |
export PATH="${PHPENV_ROOT}/bin:${PATH}" | |
eval "$(phpenv init -)" | |
else | |
echo "\x1B[36m\t ℹ️ Skipping PHPEnv installation because it is already installed \x1B[39m" | |
phpenv update | |
fi | |
phpenv_versions_cleanup | |
phpenv_versions_rehash | |
echo "\x1B[36m\t 📌 Now you are ready to use PHPEnv.\nPlease ensure that you have this entry into .bashrc or .zshrc and that you don't have a symbolic link in /usr/local/bin/php (if so, remove it)\n" | |
echo "export PHPENV_ROOT=\"${PHPENV_ROOT}\"" | |
echo "if [ -d \"\${PHPENV_ROOT}\" ]; then" | |
echo " export PATH=\"\${PHPENV_ROOT}/bin:\${PATH}\"" | |
echo " eval \"\$(phpenv init -)\"" | |
echo "fi" | |
echo "\x1B[36m\t 💡 To use a specific PHP version for a repository: cd repository_name; phpenv VERSION_NUMBER \x1B[39m" | |
} | |
phpenv_versions_cleanup() { | |
local _shim_link; | |
local _shim_realpath; | |
if [[ ! -d "${HOME}/.phpenv/versions" ]]; then | |
mkdir -p "${HOME}/.phpenv/versions" | |
fi | |
for _shim_link in "${HOME}"/.phpenv/versions/[0-9].[0-9]*/; do | |
_shim_realpath="$(cd -P "$_shim_link" && pwd)" | |
if [[ "$_shim_realpath" == "$(brew --cellar)"* ]]; then | |
unlink "$_shim_link" 2>/dev/null || true | |
fi | |
done | |
} | |
phpenv_versions_rehash() { | |
local _php_path; | |
local _php_full_ver; | |
local _php_version; | |
if [[ ! -d "${HOME}/.phpenv/versions" ]]; then | |
mkdir -p "${HOME}/.phpenv/versions" | |
fi | |
for _php_path in "$(brew --cellar)"/php*/[0-9].[0-9].*; do | |
_php_full_ver="${_php_path##*/}"; | |
_php_version="${_php_full_ver%.*}"; | |
unlink "${HOME}/.phpenv/versions/${_php_version}" 2>/dev/null || true | |
ln -s "${_php_path}" "${HOME}/.phpenv/versions/${_php_version}" 2>/dev/null || true | |
done | |
phpenv rehash | |
} | |
echo "\x1B[34m ℹ️\tWelcome to the local PHP versions software installation for MacOS\t ℹ️\n \x1B[39m" | |
echo "\x1B[35m 👉🏻 Choose an option: Install PHP Versions (via brew) or Install and configure PHPEnv?" | |
all_done=0 | |
while (( !all_done )); do | |
options=("[P] Install all PHP versions" "[E] Install and configure PHPEnv (after installing all the PHP versions" "[Q] Quit") | |
select opt in "${options[@]}"; do | |
case $REPLY in | |
1|p|P) php_install; all_done=1; break ;; | |
2|E|e) phpenv_install; all_done=1; break ;; | |
q|3) all_done=1; break ;; | |
*) echo "Invalid option" ;; | |
esac | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment