Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Switch PHP Version in Laravel Valet between PHP 7.1 and PHP 5.6

How to Switch PHP Version in Laravel Valet between PHP 7.1 and PHP 5.6

Valet switch PHP version with these commands

Install PHP 5.6 and switch Valet to PHP 5.6

valet stop
brew unlink php71
brew install php56
brew install php56-mcrypt
brew link php56
valet start

Then to switch back to PHP 7.1 it is as simple as:

valet stop
brew unlink php56
brew link php71
valet start

Then to switch to PHP 5.6:

valet stop
brew unlink php71
brew link php56
valet start
@jinva

This comment has been minimized.

Copy link

@jinva jinva commented Mar 10, 2018

What should I do on Ubuntu? please help me.

@kjbrum

This comment has been minimized.

Copy link

@kjbrum kjbrum commented Mar 19, 2018

I needed to do a few more things to get it working:

# Update global composer packages
$ composer global update

# Reinstall Valet
$ valet install
@willemvb

This comment has been minimized.

Copy link

@willemvb willemvb commented Apr 17, 2018

In Homebrew ^1.5 a lot of PHP extensions were removed, causing brew install php56-mcrypt not to work anymore.
See the official announcement.
Luckily, mcrypt seems included in the newer formula for php@5.6

Other extensions can be installed with PECL. It looks like you need to do this every time you switch between versions though.

pecl uninstall -r imagick
pecl install imagick
@fonzizi

This comment has been minimized.

Copy link

@fonzizi fonzizi commented Apr 28, 2018

Just to add to @kjbrum extra steps - unless you use valet plus, you may also have to edit your ~/.composer/composer.json file before you run composer global update.

@ruslanashaari

This comment has been minimized.

Copy link

@ruslanashaari ruslanashaari commented May 12, 2018

For those who're using Ubuntu Valet (unofficial), after installing certain version of php, you just need to stop the valet before switching the php version.

valet stop

#choose your preffered php (you need to install php first)
update-alternatives --config php

valet start

For me, php7.2 already pre-installed in my Ubuntu 18.04. So I installed php5.6, php5.6-fpm and php5.6-cli before I can switch between my preferred php version.

@subdesign

This comment has been minimized.

Copy link

@subdesign subdesign commented Aug 6, 2018

the problem is, if I'm on php 5.6 and want to switch to php 7.1, Valet fails:

PHP Parse error:  syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /Users/x/.composer/vendor/tightenco/collect/src/Collect/Support/Collection.php on line 179

so can't stop Valet before php change..

@opdavies

This comment has been minimized.

Copy link

@opdavies opdavies commented Sep 27, 2018

Had the same thing, had to remove the global composer.lock file and vendor directory, and then re-required it.

$ rm -rf ~/.composer/composer.lock ~/.composer/vendor
$ composer global req laravel/valet
@altynbek07

This comment has been minimized.

@wakjoko

This comment has been minimized.

Copy link

@wakjoko wakjoko commented Jun 20, 2020

on Ubuntu or linux, put this in your .profile

#switch php version
switch-php() {
    valet stop
    sudo update-alternatives --set php /usr/bin/php$1
    rm -f ~/.valet/valet.sock
    valet install
}

then use it as switch-php 7.1 or switch-php 7.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.