Skip to content

Instantly share code, notes, and snippets.

@infostreams
Created November 8, 2021 10:20
Show Gist options
  • Save infostreams/3753a31205e3b6c47039046e9fa40ba5 to your computer and use it in GitHub Desktop.
Save infostreams/3753a31205e3b6c47039046e9fa40ba5 to your computer and use it in GitHub Desktop.
Change PHP version on MacOS
#!/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
@Web-Assembler
Copy link

@infostreams Thanks, this was really useful for me as I also use Laravel Valet. To run the script I did the following:

  1. $ chmod +x php.sh
  2. $ ./php.sh [email protected]
  3. Enter sudo password

Notice having to use the @ symbol to install a PHP version as it didn't work first time around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment