Created
November 8, 2021 10:20
-
-
Save infostreams/3753a31205e3b6c47039046e9fa40ba5 to your computer and use it in GitHub Desktop.
Change PHP version on MacOS
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
#!/bin/bash | |
available=$(brew list --formula -1 | egrep '^php@|^php$') | |
syntax () { | |
echo "SYNTAX: $0 <version>" | |
echo "" | |
echo -n "<version> needs to be one of: " | |
echo $available | |
echo "" | |
echo "Choose the latest version of PHP by naming 'php' as the version" | |
exit 2 | |
} | |
# check if enough arguments | |
if [ "$#" -ne 1 ]; then | |
syntax | |
fi | |
version=$(echo "${available}" | grep "${1}" | head -n 1) | |
# check if valid version | |
if [[ -z "$version" ]]; then | |
syntax | |
fi | |
# do it | |
echo "Will use version $version" | |
echo $available | xargs brew unlink | |
# first default to a known 'safe' version, | |
# otherwise we cannot always do the change | |
brew link [email protected] | |
valet use [email protected] --force | |
# now change to the requested version | |
valet use "$version" --force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@infostreams Thanks, this was really useful for me as I also use Laravel Valet. To run the script I did the following:
$ chmod +x php.sh
$ ./php.sh [email protected]
Notice having to use the
@
symbol to install a PHP version as it didn't work first time around.