Reputation: 3614
I have High Sierra installed and it comes with php 7.1. During the environment I ended up being able to upgrade to php7.2 but wasn't able to document it, so I dont exactly know what I did. Now, I am trying to switch to php 7.3
Using brew, I ran the following commands:
brew unlink [email protected]
brew services install [email protected]
brew link [email protected]
If I restart my terminal and check for the php version:
php -v
I still see 7.2.25 version and not 7.3 as I desire
I also tried with a node package that I found in this link here but no success.
How do I successfully switch between php versions?
Upvotes: 25
Views: 40829
Reputation: 41
If you're using Valet:
brew link [email protected] --overwrite
, if it doesn't work, rerun it again and again, literally. or you can copy the command it suggests in the printed error.valet use [email protected] --force
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
and export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
Sometimes, when you are upgrading/downgrading, for some reason, you might need to change the host version, change Valet version, THEN you export the PATH.
Upvotes: 0
Reputation: 53
Most of these answers is for php-cli (Command Line Interface). It's for when you use php scripts from command line only. With a
php -v
PHP 8.1.29 (cli) (built: Jun 5 2024 05:51:57) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.29, Copyright (c) Zend Technologies with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies
You only get CLI php version. It's not the same as php-FPM. If you want to know what version apache uses there are other ways. You can use in your php file
echo PHP_VERSION;
or function to know more details (including version)
phpinfo();
And load it via browser. That's why i'm writing this comment. I'm new to mac and was looking for a way to make httpd use other versions. brew link && brew unlink only changes CLI php versions for me, but not what httpd uses. I don't know if it's a bug or i did something wrong. Only way i can change php versions httpd uses is to modify my httpd.conf (/opt/homebrew/etc/httpd/httpd.conf) file: I commented the line:
#LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
Because "/opt/homebrew/opt/php/" is a link to php version that apache uses. Add some lines and uncomment only the one with php version you need (they have to be installed with "brew install php@version":
LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Finally, check DirectoryIndex includes index.php
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
After that:
brew services restart httpd
And it works. No need no brew link/unlink.
I don't know if it's a best way, but for me was the only one that worked.
Upvotes: 1
Reputation: 1148
In 2024, the simplest option, by far, is to use Laravel's Herd, even when not using Laravel.
Upvotes: 0
Reputation: 267
You can easily switch between any number of PHP versions on MacOS. I had the same problem while switching between PHP 7.4 and 8.3. Just follow the steps mentioned below and you are ready to go.
First install the PHP versions you need. If you've already installed it, you can skip this step or it'll tell you that the version is already installed.
brew install [email protected]
brew install [email protected]
Now if you are using the default terminal on macOS, you'll use /~.bash_profile but if you are using zsh terminal, use /~.zshrc
sudo nano /~.zshrc
Add these lines at the end of that file
# Add PHP 7.4 paths
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
# Add PHP 8.3 paths
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
# Aliases
alias usephp8='brew unlink [email protected] && brew link --overwrite --force [email protected] && export PATH="/usr/local/opt/[email protected]/bin:$PATH" && export
PATH="/usr/local/opt/[email protected]/sbin:$PATH"'
alias usephp7='brew unlink [email protected] && brew link --overwrite --force [email protected] && export PATH="/usr/local/opt/[email protected]/bin:$PATH" && export
PATH="/usr/local/opt/[email protected]/sbin:$PATH"'
Now run this command
source ~/.zshrc
To switch between php8.3 and php7.4, use these aliases
usephp7
usephp8
You can also run a PHP project at localhost without installing XAMPP. Open terminal and go to the project path and run this command
php -S localhost:8000
Upvotes: 2
Reputation: 61
here is my approach to switch between php7.3 and php 8.2 and want to use 8.2
PHP 7.3.33 (cli) (built: Jul 13 2023 17:29:11) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies
we can change to php 7.3 by this steps
PHP 8.2.10 (cli) (built: Aug 31 2023 18:52:55) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.10, Copyright (c) Zend Technologies with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies
Upvotes: 2
Reputation: 1211
Open the terminal then run
nano ~/.zshrc
In the file that you open, you'll find the exported path of PHP as follows:
#export PATH="/usr/local/opt/[email protected]/bin:$PATH"
#export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
Then comment the old version by adding #
at the first of line and save file CTRL+x
after that close the terminal or open new one then get the php version again
php --version
I hope you have completely switched to the new PHP version
Upvotes: 9
Reputation: 598
Since I had to face this issue as well, let me share how to make this work. If you have to switch back and forth on mac then this is what works for me.
Let's say you have multiple PHP versions installed 7.2 and 7.4
Now my current PHP version is 7.4 & I have to switch back to 7.2, steps will be.
brew unlink [email protected] && brew link [email protected] --force
nano ~/.zshrc -> Update Export Path From 7.4 to 7.2
Save It.
brew services stop [email protected]
brew services start [email protected]
Voila.
To go back to 7.4 Run brew unlink [email protected] && brew link [email protected] --force
& uncomment export files. That's it.
Upvotes: 12
Reputation: 1
@chenrui's is perfect. I just had to create the sbin directory additionally as well.
You can find it [question]: brew link php71: Could not symlink sbin/php-fpm
Upvotes: 0
Reputation: 9886
Here is my installation script:
brew install [email protected]
brew link --force [email protected]
brew services start [email protected]
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"
Now my output would be as:
$ php -v
PHP 7.2.25 (cli) (built: Nov 22 2019 10:27:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.25, Copyright (c) 1999-2018, by Zend Technologies
I think the PATH
environment setup is something matters. And it does show in instructions as part of the installation process though.
Hope it helps resolving your issue.
Upvotes: 74