DEV Community

Renzo Castillo
Renzo Castillo

Posted on

Simple handy bash script to update your PHP versions in cli and fpm with valet Linux

Please consider that this script assumes that you already have php and valet Linux installed.

Just add this to your bash script

load_php() {
    version=$1
    case $version in
        7.4)
            sudo update-alternatives --set php /usr/bin/php7.4
            valet use 7.4
            valet restart
            ;;
        8.1)
            sudo update-alternatives --set php /usr/bin/php8.1
            valet use 8.1
            valet restart
            ;;
        8.2)
            sudo update-alternatives --set php /usr/bin/php8.2
            valet use 8.2
            valet restart
            ;;
        *)
            echo "Invalid PHP version: $version"
            ;;
    esac
}

phpvm() {
    if [ "$1" == "use" ]; then
        shift
        load_php "$1"
    else
        echo "Usage: phpvm use <php_version>"
    fi
}
Enter fullscreen mode Exit fullscreen mode

Now you can do something like phpvm use 7.4 and you will get your php version 7.4 set at your cli and at your php-fpm service.

Credits to Kenmutesh's post

Top comments (0)