Skip to content

Instantly share code, notes, and snippets.

@JunaidQadirB
Created September 8, 2023 10:22
Show Gist options
  • Save JunaidQadirB/d07244930671920367d4b3e64ee1be94 to your computer and use it in GitHub Desktop.
Save JunaidQadirB/d07244930671920367d4b3e64ee1be94 to your computer and use it in GitHub Desktop.
Switch PHP Versions
#!/bin/bash
php_new_version=$1
if [ -z "$php_new_version" ]
then
echo "Please enter which PHP version do you want to switch to."
exit 1
fi
php_version=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
if [ $php_version == $php_new_version ]
then
echo "You are already uisng PHP $php_version"
exit 1
fi
echo "Switching PHP from $php_version to $php_new_version..."
echo ""
echo "Disabling PHP $php_version..."
sudo a2dismod php$php_version
echo "Enabling PHP $php_version..."
sudo a2enmod php$php_new_version
echo "Updating system to use PHP $php_new_version..."
sudo update-alternatives --set php /usr/bin/php$php_new_version
php -v
@JunaidQadirB
Copy link
Author

Make sure you already install the PHP versions you want to switch between.

Usage:

switchphp [version-number]

To switch to PHP 7.4, type

switchphp 7.4

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