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
Related
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.
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
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
I have a problem with PHP which is call through bash command "php":
In bash:
php -v # PHP 5.3.17
/usr/bin/php # PHP 5.3.15
I want secondary PHP version 5.3.15 as mainly, because in this version I have correctly installed extensions etc.
How can I set /usr/bin/php as main -> I want when call "php -v" return my 5.3.15 version.
Execuse my bad English, I hope that you understand.
Thank you so much.
I have Mac OSx Lion
Another way without changing the original php file / link is if you edit your .bashrc file as follows (works with other commands as well):
Open your Terminal / Commandline and type in:
vim ~/.bashrc
Then press "i" to get into edit-mode (if there shows up a problem before that, because of a swap file, then press "E" to edit the file anyway)
Add the following line (change the path as needed):
alias php="/Applications/MAMP/bin/php/php5.4.10/bin/php"
Press "ESC" and type in ":wq"
If you need the php-alias to work right out of your current shell-session, then you have to use the following command (that's optional, so only if u r continuing to work in your current shell):
source ~/.bashrc
That's it, no change in the /usr/bin folder needed, and u can export your bashrc file / settings to any other linux / bsd like system if needed without even touching your OS /usr/bin files. You can also add new aliases for different php versions, aliases like "php52", "php53", "php54", etc. for testing purposes or whatsoever.
Have fun,
ioCron
OS: OSX Yosemite
Just sort out a similar issue by vim ~/.bash_profile
export PATH=/usr/local/bin:$PATH
export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
Make sure the order is correct. Put export PATH=/usr/local/bin:$PATH before export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
/usr/local/bin contains the mac default php.
Hope this helps.
For brew users, you might not need php55 if you already have 54 - but regardless you should probably run these commands
$ brew install php54 php54-xdebug php54-mcrypt
you can substitute 54 with the latest number if you want. last i checked it was on 56
then based on some of the comments above you should run:
$ php --version
it will probably display:
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
if it does, then change your .bash_profile like some of the other commenters
my brew instance (i think its default) lives in
/usr/local/Cellar
/usr/local/bin
as things get installed using brew, they get put in Cellar and linked through the bin. but in this case we need to override the php
$ sudo nano ~/.bash_profile
mine kinda looks like:
PATH=$PATH:~/bin
PATH=$PATH:/usr/local/bin
PATH=$PATH:/usr/local/sbin
PATH=$PATH:/usr/local/Cellar/r/2.15.1/R.framework/Versions/Current/Resources/li$
export PATH
which means that it already has access to the /usr/local/bin - but i use the alias to specify
by adding this line, near the bottom:
alias php="/usr/local/bin/php"
save and close the terminal window, open up another one, and it should show something like:
$ php --version
PHP 5.4.27 (cli) (built: Apr 24 2014 17:16:35)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans
You should look up the path and check all directories in the path for a symlink to the wrong php version.
Then you can remove that symlink and replace it with a link to the correct version.
You can also use find to see all instances:
$ sudo find / -name "php"
I would recommend you to install php through "Mac Ports".
You have multiple versions of php installed.
With the cli command "which" you can:
DESCRIPTION
The which utility takes a list of command names and searches the path for each executable file
that would be run had these commands actually been invoked.
So read the man pages of the cli command "which".
DO this from your cli:
which php
and then you will see where is located your php executable. What i would recommend you is to replace your php executable for a symbolic link to your /usr/bin/php.
I would tring to update my php version to the version 5.5+ latest
When I'm looking via /usr/local/bin/php -v in the console I see the version 5.5+ and with php -v it tell me there is the version 5.4 installed.
How can I remove the older version 5.4 and install latest 5.5+ latest?
Typically I've used the PHP package from liip.ch
From terminal, run:
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5
It will not remove the default installation of PHP 5.4 from your system, but it does not have any impact on using PHP 5.5. They address the issue with php -v in their FAQ.
Prerequisites
Xcode and it's Command line utilities (install form preferences) will give you a gcc compiler to build the php with libjpeg, libpng etc. if you want to build with these.
OR run this command on terminal to install Command line tools
xcode-select --install
Building and installing php:
Download the distribution from www.php.net/downloads.php
Untar the archive and point your console into the location
Now, having still the original old php installed, run php -i | head in the console. What you want is to configure your new php with these flags to ensure you have proper pcre support and everything works just fine.
In the php source directory you untarred, run the ./configure script with the flags you just got
Build php by running make and sudo make install afterwards. That will actually replace the original php distributed with OS X.
Example sample run terminal commands:
Download latest version from http://php.net and after ruin below commands
tar -zxvf php_scource.tar.gz
cd php_source/
php -i | head
./configure --prefix=/usr --with-snmp # ... same flags as previous build
make
sudo make install
end of the workflow for building php and just check latest version from terminal commands
$ php -v
sample output of php -v
PHP 5.5.10 (cli) (built: Mar 27 2014 16:50:31)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
OSX Mavericks runs PHP from /usr/bin/php so in order to run the new version that you want from /usr/local/bin/php, you need to copy this file over to the /usr/bin directory. If you want to save your old version of php, first go to the /usr/bin directory
cd /usr/bin
and rename your old php file
sudo mv php php.old
Then go to the /usr/bin/local directory:
cd /usr/local/bin
and then copy the php file over to /usr/bin using sudo:
sudo cp php /usr/bin
OSX Mavericks has /usr/bin first in its environment path, so the php in /usr/bin is found before it gets to the php in /usr/local/bin. Once you copy your new php file over, when you run
php -v
Which will display something like:
PHP 5.6.23 (cli) (built: Jun 24 2016 21:08:07)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
It will have your new version that was installed at /usr/local/bin/php that you copied over to /usr/bin/php instead of version 5.4. If you're running Apache and want your webserver to also use the new php version, you'll also need to update the httpd.conf file to point at the new php_module, but that's a different question.