Sidney Sousa
Sidney Sousa

Reputation: 3614

Switch of php versions not working on mac

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

Answers (10)

hbakouane
hbakouane

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.
  • After you've successfully changed your host php version, now you have to communicate that to Valet: valet use [email protected] --force
  • Finally: 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

arec
arec

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

jezmck
jezmck

Reputation: 1148

In 2024, the simplest option, by far, is to use Laravel's Herd, even when not using Laravel.

Upvotes: 0

Haroon Mirza
Haroon Mirza

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

Gwoks
Gwoks

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

Hamed Yarandi
Hamed Yarandi

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

chillywilly
chillywilly

Reputation: 434

Until I restarted Terminal I kept seeing the old version.

Upvotes: 7

Zaid Haider
Zaid Haider

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

Services List and Current version

Now my current PHP version is 7.4 & I have to switch back to 7.2, steps will be.

  1. brew unlink [email protected] && brew link [email protected] --force

  2. nano ~/.zshrc -> Update Export Path From 7.4 to 7.2

    zshrc file

  3. Save It.

  4. brew services stop [email protected]

  5. brew services start [email protected]

enter image description here

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

pannu
pannu

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

chenrui
chenrui

Reputation: 9886

Here is my installation script:

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

Related Questions