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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay