Switch of php versions not working on mac - php

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 php#7.2
brew services install php#7.3
brew link php#7.3
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?

Here is my installation script:
brew install php#7.2
brew link --force php#7.2
brew services start php#7.2
export PATH="/usr/local/opt/php#7.2/bin:$PATH"
export PATH="/usr/local/opt/php#7.2/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.

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 php#7.4 && brew link php#7.2 --force
nano ~/.zshrc -> Update Export Path From 7.4 to 7.2
Save It.
brew services stop php#7.4
brew services start php#7.2
Voila.
To go back to 7.4 Run brew unlink php#7.2 && brew link php#7.4 --force & uncomment export files. That's it.

Until I restarted Terminal I kept seeing the old version.

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/php#7.4/bin:$PATH"
#export PATH="/usr/local/opt/php#7.4/sbin:$PATH"
export PATH="/usr/local/opt/php#8.2/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

#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

Related

php -v stuck at 7.1.33 and not switching to 8.0.9

General
I am using macOS 10.13.6
So, what I want is to work with laravel 8, which requires php 8, and what I did was installing php#8.0 via homebrew:
$ brew update
$ brew tap shivammathur/php
$ brew install shivammathur/php/php#8.0
PHP 8 was installed successfully and after that, I ran this command:
$ brew link --overwrite --force php#8.0
Linking /usr/local/Cellar/php/8.0.9... 24 symlinks created.
After that I went to .bash_profile and exported /usr/local/Cellar/php/8.0.9/bin and /usr/local/Cellar/php/8.0.9/sbin to the $PATH sysvar, saved the changes and executed the command:
$ source .bash_profile restart
$ php -v
PHP 7.1.33 (cli) (built: Jan 18 2020 13:49:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
What I did next
Then I decided to check StackOverflow to find some useful info that could help me out.
Here it said:
tell your terminal where to find your new installed php.
export PATH="/usr/local/opt/php#8.0/sbin:$PATH"
Since /usr/local/opt/php#8.0 is a pointer to /usr/local/Cellar/php/8.0.9/, it must work as well (but it does not). Messing up with $PATH for some time has lead to nothing but a strong headache, so I decided to continue my search… Here it said:
You have to make your Apache use the PHP that you just downloaded.
I didn't know if I really needed that, but just in case I did, I added
LoadModule php8_module /usr/local/Cellar/php/8.0.9/lib/httpd/modules/libphp.so
to the apache httpd.conf file and ran:
$ sudo apachectl restart
Nothing happened. Another one contained the list of instructions, which looked like this:
brew install php#<desired_package>
brew link --force php#<desired_package>
brew services start php#<desired_package>
export PATH="/usr/local/opt/php#<desired_package>/bin:$PATH"
export PATH="/usr/local/opt/php#<desired_package>/sbin:$PATH"
That one did not work either. I'm stuck. I do not know what to do. Please, help. Thanks in advance.
EDIT: These steps did work for php#7.4 actually. Seems one cannot upgrade to php 8 on a machine running macOS High Sierra…
Add the bin directory in Homebrew's prefix to your path, then the linked PHP version should work.
echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile && source ~/.bash_profile
Also, please upgrade your macOS. Homebrew does not support High Sierra now.

Can't update from PHP 7.1 to PHP 7.2 on Mac

To be more precise, I don't even see where PHP 7.1 is installed.
When I run php -v in the terminal, I get:
PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
So I thought I would use Homebrew to unlink 7.1 and install 7.2. Should be straightforward. However, running brew unlink php71 returns an error saying:
No such keg: /usr/local/Cellar/php71
And, indeed, in /usr/local/Cellar there is nothing resembling a PHP version.
I tried running brew update and brew upgrade just in case, but it didn't help.
I also tried running a PHP application locally and did phpinfo() within the application, and I got the same version info I got in the console.
When I ran whereis php it gave me the result /usr/bin/php. When I ran whereis php71 it came up with nothing which, I suppose, is to be expected in this situation.
The question is, how do I replace PHP 7.1 with 7.2 in the least painful way? Ideally, I would like to get brew to do this so I can update future version through brew as well.
Mac OS has its own PHP version installed, which is complete independent from brew.
You need to force link your brew version.
brew link php#7.2 --force

How to enable PHP Intl extension on macOS Mojave?

I'm trying to install Magento (2.3.0) on macOS Mojave. Magento shows PHP Extension intl. is missing.
I tried the below to resolve:
Made a copy of php.ini using cp /etc/php.ini.default php.ini
Removed ";" before extension=php_intl.dll
Restart Apache sudo apachectl restart
But the above did not resolve.
On checking php -v, i'm seeing the below error:
PHP Warning: PHP Startup: Unable to load dynamic library
'/usr/lib/php/extensions/no-debug-non-zts-20160303/php_intl.dll' -
dlopen(/usr/lib/php/extensions/no-debug-non-zts-20160303/php_intl.dll,
0x0009): dlopen(): file not found: /usr/lib/php/extensions/no-debug-
non-zts-20160303/php_intl.dll in Unknown on line 0
PHP 7.1.19 (cli) (built: Aug 17 2018 20:10:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
There are only 2 files under /usr/lib/php/extensions/no-debug-non-zts-20160303 namely opache.so and xdebug.so
How can i install or enable "PHP Extension intl" on my macOS Mojave?
Here's a solution that worked for me:
Find all PHP versions installed brew list | grep php
Remove all versions of PHP brew remove --ignore-dependencies --force php70 php71 php72 (based on what you see above)
Install PHP brew install php72 (i chose 7.2, 7.3 is not supported yet by several vendors)
Run the command which php should show you the path to the installed PHP. Copy the path.
Update your bash_profile vi ~/.bash_profile and add this line to the file:
export PATH=/usr/local/php5/bin:$PATH
Save and run this source ~/.bash_profile
Check if PHP Intl Extension is installed using php -m | grep intl. If the installation went well, we will see intl listed. If not the extension is not installed.
I think from PHP 7 (not sure of the version), the extensions are available by default and we need not enable them in php.ini file explicitly.
If you installed Homebrew's php, linking it to a directory in your path will fix the issue.
brew link --force php#7.3
I had the same issue and that fixed it.
Here is a link where I got a detailed answer from
Got help from the link and able to compile https://donatstudios.com/Install-PHP-Mcrypt-Extension-in-OS-X
Next we will download the PHP source. Verify the exact version of PHP you are running. This can be retrieved as follows. The version is highlighted.
$ php --version
PHP 7.1.19 (cli) (built: Aug 17 2018 18:03:17) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Now we move into a working directory and download the source making sure to update the following for the version from above.
$ cd /tmp
$ curl -L http://php.net/get/php-{{php-version}}.tar.bz2/from/this/mirror > php.tar.bz2
$ open php.tar.bz2
Now we will compile and test the extension.
$ cd php-{{php-version}}/ext/{{extension}}
$ phpize
$ ./configure
$ make
$ make test
$ sudo make install
If all that goes well finally we'll need to add the following to our php.ini - I usually add at it at the end of the file.
extension = {{extension}}
.so
You can verify your installation with the following:
$ php --info | grep {{extension}}\\.
Lastly, depending on your setup now you may want to restart apache.
$ sudo apachectl restart

Where does homebrew install PHP on Mac High Sierra?

I'm using Mac High Sierra. I installed PHP (with GMP) using
brew install php72-gmp
Everything installed successfully, per the messages given, but it would seem I still have an old version of PHP 7.1 somewhere on my machine because when I run
localhost:php-7.2.1 davea$ which php
/usr/bin/php
localhost:php-7.2.1 davea$ /usr/bin/php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
So I'd like to figure out where to put the homebrew version of PHP ahead in my PATH so that that version gets picked up first.
EDIT:
As of 2018 and the latest updates to Brew your php formula names have the following format php#7.2.
When you run brew info php#7.2 your path should be /usr/local/Cellar/php#7.2/
And there will be a symbolic link to /usr/local/bin/opt/php#7.2/
So in order to change your php installation you just need to run this in terminal:
echo 'export PATH="/usr/local/opt/php#7.2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php#7.2/sbin:$PATH"' >> ~/.bash_profile
or put it manually in your .bash_profile like this:
export PATH="/usr/local/opt/php#7.2/bin:$PATH"
export PATH="/usr/local/opt/php#7.2/sbin:$PATH"
===========================================================================
The location of your Homebrew installed php should be /usr/local/Cellar/php72
These are the steps you need to do to setup your PHP on macOS:
After you install php with brew run this in terminal:
export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH"
Then run:
source ~/.bash_profile
To check your current active version of php run this in terminal:
which php
EDIT:
run brew info php72 in console to get all info, hints and caveats for php, it is really useful, for example this comes from brew info:
✩✩✩✩ PHP CLI ✩✩✩✩
If you wish to swap the PHP you use on the command line, you should add the
following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent
configuration file:
export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"
Check if you have two php directories in '/usr/bin/' or '/usr/local/bin/'. If yes do a link to the good directory. See this link, may be it helps you.
As you are familiar with linux, homebrew has its own /opt and /etc path
# > echo $(brew --prefix)
# /opt/homebrew
so the paths are (for Apple Silicon):
/opt/homebrew/opt
/opt/homebrew/etc
in linux, php is installed in /etc/php/7.4
in mac homebrew, php is installed in /opt/homebrew/etc/php/7.4.
Please find the homebrew document on the default installation path for Apple Silicon 1
ref 2

How do I enable fastcgi on my Mavericks using PHP 5.4.24?

I am trying to use PHPStorm 7's built-in web server feature and I have specified the interpreter as PHP 5.4.24 but it keeps telling me this below.
How do I enable FastCGI on a Mavericks machine. I checked that it isn't configured/enabled by doing php -i on my Terminal. Any help would be appreciated. I want to test apps in the browser.
php-cgi not found
Please ensure that configured PHP Interpreter built as CGI program (--enable-fastcgi was specified)
If it's not already done install Homebrew :
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now, that we've Homebrew, tap php repositories by entering this on your terminal :
brew tap josegonzalez/php
brew tap homebrew/dupes
Check what options are available for PHP 5.4 :
brew options php54
Now install/build PHP 5.4 with some option (in your case PHP-FPM with CGI) :
brew install php54 --with-fpm --with-debug --with-cgi --with-libmysql --with-homebrew-curl
brew install fastcgi
Note : If you're not going to use Apache add --without-apache, if you need others things, just check the options and add what you need
Now, check if PHP-FPM is enable by typing this in your terminal :
php-fpm -v
If you get this :
PHP 5.4.24 (fpm-fcgi) Copyright (c)
1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013
Zend Technologies
You just installed PHP with FCGI like a boss..

Categories