"php -v" no such file or directory [duplicate] - php

I would like to test my application on PHP 5.3 up to PHP 7.0.
Where and how can I install the versions and how can I switch them by running a small script?

If you have both versions of PHP installed, you can switch between versions using the link and unlink brew commands.
For example, to switch between PHP 7.4 and PHP 7.3
brew unlink php#7.4
brew link php#7.3
PS: both versions of PHP have be installed for these commands to work.

I found this very good tutorial on how to install and switch php versions on OSX.
I can switch the version like
$ sphp 7.0 => PHP 7.0
$ sphp 7.3 => PHP 7.3
$ sphp 7.4 => PHP 7.4
Exactly what I want!

Example: Let us switch from php 7.4 to 7.3
brew unlink php#7.4
brew install php#7.3
brew link php#7.3
If you get Warning: php#7.3 is keg-only and must be linked with --force
Then try with:
brew link php#7.3 --force

Using brew
Show current version
$ php -v
Change to different version
(eg. changing from 5.5.x to version 7.0.latest) :
$ brew unlink php55
$ brew install php70

I liked the switcher idea because I'm working of different version at the moment so what you need is
brew install brew-php-switcher
then brew-php-switcher version
for example brew-php-switcher 7.4

i think unlink & link php versions are not enough because we are often using php with apache(httpd), so need to update httpd.conf after switch php version.
i have write shell script for disable/enable php_module automatically inside httpd.conf, look at line 46 to line 54 https://github.com/dangquangthai/switch-php-version-on-mac-sierra/blob/master/switch-php#L46
Follow my steps:
1) Check installed php versions by brew, for sure everything good
> brew list | grep php
#output
php56
php56-intl
php56-mcrypt
php71
php71-intl
php71-mcrypt
2) Run script
> switch-php 71 # or switch-php 56
#output
PHP version [71] found
Switching from [php56] to [php71] ...
Unlink php56 ... [OK] and Link php71 ... [OK]
Updating Apache2.4 Configuration /usr/local/etc/httpd/httpd.conf ... [OK]
Restarting Apache2.4 ... [OK]
PHP 7.1.11 (cli) (built: Nov 3 2017 08:48:02) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
3) Finally, when your got above message, check httpd.conf, in my laptop:
vi /usr/local/etc/httpd/httpd.conf
You can see near by LoadModule lines
LoadModule php7_module /usr/local/Cellar/php71/7.1.11_22/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/Cellar/php56/5.6.32_8/libexec/apache2/libphp5.so
4) open httpd://localhost/info.php
i hope it helpful

If you install PHP with homebrew, you can switch between versions very easily. Say you want php56 to point to Version 5.6.17, you just do:
brew switch php56 5.6.17

If you have installed php with latest version on your local system and using homebrew then to switch to older version, run following commands.
Here, we are switching to 7.4
brew unlink php
brew install php#7.4
brew link php#7.4

How to:
Find installed PHP versions
Switch from default PHP package to a versioned package
% brew search php
brew-php-switcher php-cs-fixer php#7.3 phplint phpstan pup
php ✔ php-cs-fixer#2 php#7.4 ✔ phpmd phpunit
php-code-sniffer php#7.2 phpbrew phpmyadmin pcp
% brew unlink php
% brew link php#7.4

brew link --overwrite php#7.3
Works perfectly for me :-)

delete all row with "php" in:
nano ~/.zshrc
after that it will work:
brew unlink php#7.4 && brew link php#8.0

if you are using homebrew, then it allows multiple versions of a formula to be installed.
hence,
brew unlink is used to detach a version of formula from usage.
brew link is used to attach a version of formula to usage.
for example, if you have php 7.4 and 8.0 both installed and want to switch between them assuming you are currently using 7.4, then it can be done as :
brew unlink php#7.4
brew link php#8.0

I prefer to use phpbrew where you can easily install ($ phpbrew install 8.1.3 +default) and switch (phpbrew switch 8.1.3) the used php version.
https://github.com/phpbrew/phpbrew

Old question but it's still a difficulty to have a quick way to switch between php versions in 2022. I use Laravel valet and switching PHP versions requires a re install of valet to work. I basically have two custom functions defined in my .zhsrc file, but I think this will work for bash too:
switchphp7() {
valet stop
brew unlink php#8.1
brew services stop php#8.1
brew link --force --overwrite php#7.4
brew services start php#7.4
composer global update
rm -f ~/.config/valet/valet.sock
valet install
valet start
}
switchphp8() {
valet stop
brew unlink php#7.4
brew services stop php#7.4
brew link --force --overwrite php#8.1
brew services start php#8.1
composer global update
rm -f ~/.config/valet/valet.sock
valet install
valet start
}
And then just run them in the command line:
switchphp8
This works reliably for me, however, if you use Valets virtual SSL certificates, the command line will ask for admin account password for every ssl certificate you have linked which is potentially annoying if you have loads of them. It's not a bug if it keeps asking, eventually you will allow all if you keep typing password/use touch id.

Since the OP has specifically asked for switching from "PHP 5.3 up to PHP 7.0", a lot has been deprecated and/or removed in brew due to official support ending for them - as of Dec 2022.
So if we are to switch versions and install any of these unsupported versions brew install php#7.3, it will fail (Question and its answer here). You'll need to use the tap shivammathur/php (praise the devs responsible for this) which can be used to install unsupported versions of PHP.
brew tap shivammathur/php //need to do only once
brew install shivammathur/php/php#7.0 //or brew install php#7.4 depending on your choice
brew search php //optional, shows all the versions available if you want some other version
brew link php#7.0
Switching henceforth is simple:
brew unlink php#7.0
brew link php#5.6 //OP asked 5.3 but 5.6 is the lowest version supported by the tap
All versions <= php#7.4 have been deprecated and removed from brew because they reached their end of life. As of Dec 2022, the only officially supported versions are 8.0 and 8.1 which brew supports.
This too eventually will get outdated as more versions reach EOL but the steps should remain the same as long as that tap remains in development.
The link provided by Xaver's answer served as a good further reading.

Don't forget to change you environment PATH with this command :
echo 'export PATH="/usr/local/opt/php#8.1/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

My purpose was install old patch of php#7.4.24 to MacOS and I done that this way:
I found revision of brew, which contains old php patch, which i need, and reset git to them
repository: https://github.com/Homebrew/homebrew-core/commit/3b342c146da0452d71d982324977567960da2979
git -C $(brew --repo)/Library/Taps/homebrew/homebrew-core reset --hard 3b09794
brew reinstall php#7.4

First install or reinstall the version you want
brew reinstall php#7.4
Secondly you need to switch to the version you want
echo 'export PATH="/usr/local/opt/php#7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php#7.4/sbin:$PATH"' >> ~/.zshrc
Then you need to add in your PATH by running below command
brew-php-switcher 7.4

If you don't need to play with apache then there is a very easy way with one function, just add it to your ~/.bash_profile or ~/.zshrc (depends on your terminal)
function php-toggle() {
currentVersion=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
switchTo='7.4'
[[ $currentVersion == '7.4' ]] && switchTo='8.1'
brew unlink php#"$currentVersion"
brew services stop php#"$currentVersion"
brew link php#"$switchTo"
brew services start php#"$switchTo"
exportString="export PATH=\"/opt/homebrew/opt/php#${switchTo}/bin:\$PATH\""
echo $exportString > ~/.php_version
source ~/.php_version
}
source ~/.php_version
Open new terminal and run php-toggle.

Related

Using PHP on macOS Monterey - many homebrew errors on reinstall after updating OS

I made the seemingly bad decision to update to macOS Monterey 12.3.1, and am having a hell of a time trying to get PHP working on it again. From what I've seen, Apple has decided that PHP is deprecated - at least from their machines, and they removed every php version and setting that I had set up for all my Laravel and Wordpress projects!
I tried reinstalling PHP using homebrew but I got a lot of errors about certain packages not being able to be downloaded due to authorization denial?
So then I attempted to uninstall and reinstall Homebrew, and I did the following:
Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Add the following to zshrc
eval "$(/opt/homebrew/bin/brew shellenv)"
Test brew version to see if it's working
brew --version
Run brew doctor to make sure everything is configured correctly
brew doctor
I also saw that it was likely that there would be libraries missing and that i should reinstall open ssl so i did that as well:
brew install openssl
Then for PHP I ran:
brew tap shivammathur/php
and then
brew install shivammathur/php/php#7.4
brew install shivammathur/php/php#8.0
and I even restarted the php services just in case
brew services restart shivammathur/php/php#7.4
brew services restart shivammathur/php/php#8.0
Then I ran
php -v
and I got the following error:
zsh: command not found: php
and which php says php not found
what the heck!
I then tried to unlink php and force a link to 7.4:
brew unlink php && brew link --overwrite --force php#7.4
and I got the following brew error:
Error: No such keg: /usr/local/Cellar/php
I tried digging into fixing this keg issue but i haven't been able to figure it out, do you have any suggestions on next steps
Check that Homebrew's bin directory i.e. $(brew --prefix)/bin is in your PATH.
If you want to link PHP 7.4, run
brew link --overwrite --force php#7.4
If you get an error mentioning that it is already linked, you can relink it by unlinking it first and linking it again to be sure. Make sure you unlink the correct version.
brew unlink php#7.4
brew link --overwrite --force php#7.4

Mac: switch to php8.0 installed by brew

After Mac OS full reinstall I perform php 8.0 install by brew install php.
Unfortunately, brew didn't make this version active: php --version report about 7.3 (not recommended version).
I cannot unlink this version because it's not installed by brew, so brew link also doesn't have much sense.
How to bring latest php to system level and completely forget about php7.3 ?
You'll need to tell your terminal where to find your new installed php.
export PATH="/usr/local/opt/php#8.0/sbin:$PATH"
And you need to add it to the bash_profile if you want all terminals to keep using your installed version.
echo 'export PATH="/usr/local/opt/php#8.0/sbin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
This is for terminal use only. So not for your web servers.
In the future, when you install other versions, you can update the profile with:
nano ~/.bash_profile

homebrew - upgrade php broke php 5.6 dependency

I have multiple versions of PHP installed on my localhost using homebrew:
the standard core php package (v7.3.12)
php 5.6 from exolnet/homebrew-deprecated
Now when I upgraded php to 7.3.12 recently it upgraded a dependency that php#5.6 requires and now I am getting this error:
dyld: Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
Referenced from: /usr/local/Cellar/php#5.6/5.6.40/bin/./php
Reason: image not found
[1] 83775 abort ./php
And when I navigate to /usr/local/opt/openssl/lib/ I see that I now have the upgraded libcrypto.1.1.dylib installed.
I've tried brew upgrade and reinstalling php#5.6 but no joy.
Does anyone have a suggestion? I really need both versions of php working on my localhost to continue development.
From my experience, trying to manage multiple PHP versions via homebrew is an absolute PITA, and I don't think I've ever got to the point where I can easily have both versions available side by side (until now!). Extensions are also supported, the details of which are provided later on in this answer.
That said, I have found a guide that gives some instructions on how to get this working and it works an absolute treat:
The linked guide references this method in the context of running PHP within Apache, so if that's your use case that's a nice little bonus for you too.
Now, because just posting links as answers on StackOverflow just isn't the done thing, I've detailed the main steps you need to follow in order below. Homebrew uses different paths on Apple Silicon hardware vs Intel because of course it does, so if I've missed any path changes please let me know so I can update my answer.
For anyone following this guide that doesn't yet have the prerequisites installed, ensure you have the Xcode command line tools and Homebrew installed and up to date.
If you want to check Homebrew for any issues, you can run
brew doctor
It's also never a bad idea to ensure that you have OpenSSL installed, especially if starting a fresh on macOS Monterey:
brew install openssl
So, let's get started.
Reset your environment
First, update everything then upgrade:
brew update
brew upgrade
brew cleanup
The reason for this is: "This will actually 'migrate' the core PHP packages (which are the only ones supported), but there's a bunch of symlinks utilized that could cause problems down the road, so after upgrading, we'll remove all PHP packages, to provide a fresh start"
Once you've done that, check what's actually installed:
brew list | grep php
Then remove whatever you find, for example:
brew uninstall --force php56 php56-apcu php56-opcache php56-xdebug
brew uninstall --force php70 php70-apcu php70-opcache php70-xdebug
brew uninstall --force php71 php71-apcu php71-opcache php71-xdebug
brew uninstall --force php72 php72-apcu php72-opcache php72-xdebug
brew cleanup
Double check there's nothing left:
brew list | grep php
and clean up any old configuration files:
Intel:
rm -Rf /usr/local/etc/php/*
Apple Silicon:
rm -Rf /opt/homebrew/etc/php/*
If have the exolnet/deprecated tap installed, you'll need to remove it first using
brew untap exolnet/deprecated
If you don't, you can get some weird conflicts
Add the new Tap and Install
We're then going to tap a different repository. This repository has many versions of PHP pre-built which may or may not work for you, however installation should be much faster as we don't have to compile from source.
brew tap shivammathur/php
You can then install any PHP versions you require:
brew install shivammathur/php/php#7.0
brew install shivammathur/php/php#7.1
brew install shivammathur/php/php#7.2
brew install shivammathur/php/php#7.3
brew install shivammathur/php/php#7.4
brew install shivammathur/php/php#8.0
At the time of writing, this repository has versions 5.6 through 8.1 available for installation
The php.ini files are located in the following directories:
Intel:
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.3/php.ini
/usr/local/etc/php/7.4/php.ini
/usr/local/etc/php/8.0/php.ini
Apple Silicon:
/opt/homebrew/etc/php/7.0/php.ini
/opt/homebrew/etc/php/7.1/php.ini
/opt/homebrew/etc/php/7.2/php.ini
/opt/homebrew/etc/php/7.3/php.ini
/opt/homebrew/etc/php/7.4/php.ini
/opt/homebrew/etc/php/8.0/php.ini
Link your desired version
Once you've got here, close and reopen any terminal windows you have open to avoid strange path issues.
Now, these versions are installed, but not linked. To switch to PHP 7.3 for example, run the following command:
brew unlink php && brew link --overwrite --force php#7.3
And then check we have the correct version:
php -v
If we want to switch to PHP 7.4:
brew unlink php && brew link --overwrite --force php#7.4
And then check we have the correct version:
php -v
Easier Switching
If you want a faster way of switching PHP versions, check out the sPHP script from rhukster
You can install this by running:
Intel:
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp
Apple Silicon:
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /opt/homebrew/bin/sphp
chmod +x /opt/homebrew/bin/sphp
and execute it using:
sphp 8.0
Of course, with anything like this YMMV, but this should hopefully be a good starting point and actually get you where you need to be and remove some headaches.
What about extensions?
Well I'm glad you asked. For the later versions, pecl does work. However, if you want to install xdebug for 5.6 for example, pecl throws its toys out of its pram.
There is another repository you can tap, specifically for extensions:
brew tap shivammathur/extensions
Once you've done this, it's as simple as running:
brew install xdebug#5.6
brew install xdebug#8.1
The list of available extensions is detailed in the link above. I won't include them here because it's a dynamic list and it'll soon be out of date, but most of them support 5.6 through to 8.1
Wrap up
Having followed this process myself, this is the easiest approach I'm yet to find for managing multiple PHP versions on macOS. Feel free to run brew doctor and brew cleanup at any points during the process, it can't hurt after all and might in fact help with debugging any issues you happen across. That said, my environment was an absolute mess and this tidied it up in about ten minutes. The longest step in this process for me was updating Homebrew itself.

PHPInfo / Valet - Two different PHP versions

OK so I am experiencing something quite odd. My phpinfo() shows one version while php -v shows another.
I am running locally on a MacBook Air (Mojave), with Laravel and Valet.
phpinfo():
PHP Version 7.2.13
Configuration File (php.ini) Path /usr/local/etc/php/7.2
Loaded Configuration File /usr/local/etc/php/7.2/php.ini
Scan this dir for additional .ini files /usr/local/etc/php/7.2/conf.d
While in my terminal, it shows:
php -v
PHP 7.3.0 (cli) (built: Dec 7 2018 11:00:11) ( NTS )
Now, if I do a which php I get this:
/usr/local/bin/php
In my ~/.bash-profile, I have this:
export "PATH=~/.composer/vendor/bin:$PATH"
I have tried to:
Restart Nginx
Restart Valet
Unlink PHP#7.2 and link PHP#7.3with Homebrew:
Olivers-MacBook-Air:~ oliverbusk$ brew unlink php#7.2
Error: No such keg: /usr/local/Cellar/php#7.2
How can I get my actual valet site to use php#7.3?
Below simple fix worked for me.
rm ~/.config/valet/valet.sock
valet restart
In my case it worked after forcing the specific PHP version:
valet use php#7.4 --force
Good news, please upgrade to latest valet version, you can easily switch php version.
Source: https://laravel.com/docs/5.8/valet
PHP Versions
Valet allows you to switch PHP versions using the valet use php#version command. Valet will install the specified PHP version via Brew if it is not already installed:
valet use php#7.2
valet use php //without any specification, it will be using latest version
Something so basic as rebooting my Macbook solved this issue. Once rebooted, the correct version 7.3 showed in my phpinfo();
From MacBook pro (Mojave), with Laravel and Valet:
valet use php#7.2
To be able to effectively change the version of php on my machine I use the following script:
Add to your user's .bash_profile
Then use the following command to switch
switch-php7.x
switch-php() {
valet stop
brew unlink php#7.2 php#7.3 php#7.4
brew link --force --overwrite php#$1
brew services restart php#$1
composer global update
rm -f ~/.config/valet/valet.sock
valet install
}
https://gist.github.com/r1tt3r/e0f199eb274d5ff186b73956af594316
I am running linux valet on Ubuntu 20.04 , The following command works for me
valet use 8.0
Here, 8.0 refers to the php version.
NB: Tested this solution on Ubuntu 20.04 Desktop
If you are using Valet Linux Plus(Has more features than Valet Linux) Just do valet use {your PHP version} --update-cli. For instance to use php8.0 just run:
valet use 8.0 --update-cli
This will update both php-fpm version plus php-cli version too.
Basically the php valet uses and the one used by your macbook is different.
But as already mentioned by #Shiro and #Israel Alexis Palma Quezada, in newer versions valet use php73 would solve your problem.
If it's not working, try running composer global update to update your global packages, thus updating valet
To update the mac php version, execute the following lines:
export PATH="your/path/to/php#7.3/bin:$PATH"
export PATH="your/path/to/php#7.3/sbin:$PATH"
or simply add these two lines to your ~/.profile or ~/.bash_profile
Don't forget to run source ~/.profile or source ~/.bash_profile or whatever file you chose to edit.
Yesterday I encountered the same problem on Ubuntu 18.
At first I changed the PHP version:
$ sudo a2dismod php7.2
$ sudo a2enmod php7.3
$ sudo update-alternatives --set php /usr/bin/7.3
But valet was still showing the wrong PHP version. Rebooting did not work. In fact, phpinfo() was displaying PHP 7.1.
After run ps -aux | grep php I noticed few instances of php-fpm (5.6, 7.1 and 7.2) running.
I stopped them
$ sudo service php5.6-fpm stop
$ sudo service php7.1-fpm stop
$ sudo service php7.2-fpm stop
Then started 7.3
$ sudo service php7.3-fpm start
And now my phpinfo() display the correct version of PHP
I ran into this same problem while using valet plus today. When I switched PHP versions it was showing the previous one in my browser but the correct one in my terminal. Looking at the my brew services list:
brew services list
It showed that one of the valet-php services (valet-php#7.2) was not in the service list but somehow my valet was able to switch to it. Valet didn't install this PHP properly and that's what was causing the issue. I did the following to uninstall that PHP and reinstall it with brew.
brew uninstall valet-php#7.2
sudo rm -rf /usr/local/Cellar/valet-php#7.2/7.2.34_1
brew install valet-php#7.2
The PHP version that was once not showing in the brew services list now showed and I was able to "stop" that php version's service:
brew services stop valet-php#7.2
which made everything work properly again without a restart.

How can I easily switch between PHP versions on Mac OSX?

I would like to test my application on PHP 5.3 up to PHP 7.0.
Where and how can I install the versions and how can I switch them by running a small script?
If you have both versions of PHP installed, you can switch between versions using the link and unlink brew commands.
For example, to switch between PHP 7.4 and PHP 7.3
brew unlink php#7.4
brew link php#7.3
PS: both versions of PHP have be installed for these commands to work.
I found this very good tutorial on how to install and switch php versions on OSX.
I can switch the version like
$ sphp 7.0 => PHP 7.0
$ sphp 7.3 => PHP 7.3
$ sphp 7.4 => PHP 7.4
Exactly what I want!
Example: Let us switch from php 7.4 to 7.3
brew unlink php#7.4
brew install php#7.3
brew link php#7.3
If you get Warning: php#7.3 is keg-only and must be linked with --force
Then try with:
brew link php#7.3 --force
Using brew
Show current version
$ php -v
Change to different version
(eg. changing from 5.5.x to version 7.0.latest) :
$ brew unlink php55
$ brew install php70
I liked the switcher idea because I'm working of different version at the moment so what you need is
brew install brew-php-switcher
then brew-php-switcher version
for example brew-php-switcher 7.4
i think unlink & link php versions are not enough because we are often using php with apache(httpd), so need to update httpd.conf after switch php version.
i have write shell script for disable/enable php_module automatically inside httpd.conf, look at line 46 to line 54 https://github.com/dangquangthai/switch-php-version-on-mac-sierra/blob/master/switch-php#L46
Follow my steps:
1) Check installed php versions by brew, for sure everything good
> brew list | grep php
#output
php56
php56-intl
php56-mcrypt
php71
php71-intl
php71-mcrypt
2) Run script
> switch-php 71 # or switch-php 56
#output
PHP version [71] found
Switching from [php56] to [php71] ...
Unlink php56 ... [OK] and Link php71 ... [OK]
Updating Apache2.4 Configuration /usr/local/etc/httpd/httpd.conf ... [OK]
Restarting Apache2.4 ... [OK]
PHP 7.1.11 (cli) (built: Nov 3 2017 08:48:02) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
3) Finally, when your got above message, check httpd.conf, in my laptop:
vi /usr/local/etc/httpd/httpd.conf
You can see near by LoadModule lines
LoadModule php7_module /usr/local/Cellar/php71/7.1.11_22/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/Cellar/php56/5.6.32_8/libexec/apache2/libphp5.so
4) open httpd://localhost/info.php
i hope it helpful
If you install PHP with homebrew, you can switch between versions very easily. Say you want php56 to point to Version 5.6.17, you just do:
brew switch php56 5.6.17
If you have installed php with latest version on your local system and using homebrew then to switch to older version, run following commands.
Here, we are switching to 7.4
brew unlink php
brew install php#7.4
brew link php#7.4
How to:
Find installed PHP versions
Switch from default PHP package to a versioned package
% brew search php
brew-php-switcher php-cs-fixer php#7.3 phplint phpstan pup
php ✔ php-cs-fixer#2 php#7.4 ✔ phpmd phpunit
php-code-sniffer php#7.2 phpbrew phpmyadmin pcp
% brew unlink php
% brew link php#7.4
brew link --overwrite php#7.3
Works perfectly for me :-)
delete all row with "php" in:
nano ~/.zshrc
after that it will work:
brew unlink php#7.4 && brew link php#8.0
if you are using homebrew, then it allows multiple versions of a formula to be installed.
hence,
brew unlink is used to detach a version of formula from usage.
brew link is used to attach a version of formula to usage.
for example, if you have php 7.4 and 8.0 both installed and want to switch between them assuming you are currently using 7.4, then it can be done as :
brew unlink php#7.4
brew link php#8.0
I prefer to use phpbrew where you can easily install ($ phpbrew install 8.1.3 +default) and switch (phpbrew switch 8.1.3) the used php version.
https://github.com/phpbrew/phpbrew
Old question but it's still a difficulty to have a quick way to switch between php versions in 2022. I use Laravel valet and switching PHP versions requires a re install of valet to work. I basically have two custom functions defined in my .zhsrc file, but I think this will work for bash too:
switchphp7() {
valet stop
brew unlink php#8.1
brew services stop php#8.1
brew link --force --overwrite php#7.4
brew services start php#7.4
composer global update
rm -f ~/.config/valet/valet.sock
valet install
valet start
}
switchphp8() {
valet stop
brew unlink php#7.4
brew services stop php#7.4
brew link --force --overwrite php#8.1
brew services start php#8.1
composer global update
rm -f ~/.config/valet/valet.sock
valet install
valet start
}
And then just run them in the command line:
switchphp8
This works reliably for me, however, if you use Valets virtual SSL certificates, the command line will ask for admin account password for every ssl certificate you have linked which is potentially annoying if you have loads of them. It's not a bug if it keeps asking, eventually you will allow all if you keep typing password/use touch id.
Since the OP has specifically asked for switching from "PHP 5.3 up to PHP 7.0", a lot has been deprecated and/or removed in brew due to official support ending for them - as of Dec 2022.
So if we are to switch versions and install any of these unsupported versions brew install php#7.3, it will fail (Question and its answer here). You'll need to use the tap shivammathur/php (praise the devs responsible for this) which can be used to install unsupported versions of PHP.
brew tap shivammathur/php //need to do only once
brew install shivammathur/php/php#7.0 //or brew install php#7.4 depending on your choice
brew search php //optional, shows all the versions available if you want some other version
brew link php#7.0
Switching henceforth is simple:
brew unlink php#7.0
brew link php#5.6 //OP asked 5.3 but 5.6 is the lowest version supported by the tap
All versions <= php#7.4 have been deprecated and removed from brew because they reached their end of life. As of Dec 2022, the only officially supported versions are 8.0 and 8.1 which brew supports.
This too eventually will get outdated as more versions reach EOL but the steps should remain the same as long as that tap remains in development.
The link provided by Xaver's answer served as a good further reading.
Don't forget to change you environment PATH with this command :
echo 'export PATH="/usr/local/opt/php#8.1/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
My purpose was install old patch of php#7.4.24 to MacOS and I done that this way:
I found revision of brew, which contains old php patch, which i need, and reset git to them
repository: https://github.com/Homebrew/homebrew-core/commit/3b342c146da0452d71d982324977567960da2979
git -C $(brew --repo)/Library/Taps/homebrew/homebrew-core reset --hard 3b09794
brew reinstall php#7.4
First install or reinstall the version you want
brew reinstall php#7.4
Secondly you need to switch to the version you want
echo 'export PATH="/usr/local/opt/php#7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php#7.4/sbin:$PATH"' >> ~/.zshrc
Then you need to add in your PATH by running below command
brew-php-switcher 7.4
If you don't need to play with apache then there is a very easy way with one function, just add it to your ~/.bash_profile or ~/.zshrc (depends on your terminal)
function php-toggle() {
currentVersion=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
switchTo='7.4'
[[ $currentVersion == '7.4' ]] && switchTo='8.1'
brew unlink php#"$currentVersion"
brew services stop php#"$currentVersion"
brew link php#"$switchTo"
brew services start php#"$switchTo"
exportString="export PATH=\"/opt/homebrew/opt/php#${switchTo}/bin:\$PATH\""
echo $exportString > ~/.php_version
source ~/.php_version
}
source ~/.php_version
Open new terminal and run php-toggle.

Categories