Mac OSX come with pre-installed php , but many time you will find that its old version or you want to upgrade for your project specific needs then use this guide to download and install most up to date php in your mac OSX
Test your php installation
open terminal and type
you will see something like this
or if you will check with phpinfo function you will see your php installation info
Tested on mac OSX snow leopard and mountain lion ( intel )
Prerequisite knowledge and software for compiling php :
Install XCODE and XCODE command lines tools before start compiling
Test your php installation
open terminal and type
php --version
you will see something like this
<?php phpinfo() ?>
![]() |
| phpinfo |
Tested on mac OSX snow leopard and mountain lion ( intel )
Prerequisite knowledge and software for compiling php :
- Basic Unix skills (being able to operate "make" and a C compiler)
- An ANSI C compiler
- A web server
- Any module specific components (such as GD, PDF libs, etc.)
Install XCODE and XCODE command lines tools before start compiling
Steps :
1: Download PHP source codes
2 : extract source code
3 : open terminal and change directory to extracted folder
4: complie with
install dependencies first
- brew install libjpeg
- brew install pcre
- brew install libxml2
- brew install mcrypt
- brew install openssl
- brew install libpng
./configure \ --prefix=/usr \ --mandir=/usr/share/man \ --infodir=/usr/share/info \ --sysconfdir=/private/etc \ --with-apxs2=/usr/sbin/apxs \ --enable-cli \ --with-config-file-path=/etc \ --with-libxml-dir=/usr \ --with-openssl=/usr \ --with-kerberos=/usr \ --with-zlib=/usr \ --enable-bcmath \ --with-bz2=/usr \ --enable-calendar \ --with-curl=/usr \ --enable-dba \ --enable-exif \ --enable-ftp \ --with-gd \ --enable-gd-native-ttf \ --with-icu-dir=/usr \ --with-iodbc=/usr \ --with-ldap=/usr \ --with-ldap-sasl=/usr \ --with-libedit=/usr \ --enable-mbstring \ --enable-mbregex \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --without-pear \ --with-pdo-mysql=mysqlnd \ --with-mysql-sock=/var/mysql/mysql.sock \ --with-readline=/usr \ --enable-shmop \ --with-snmp=/usr \ --enable-soap \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --with-tidy \ --enable-wddx \ --with-xmlrpc \ --with-iconv-dir=/usr \ --with-xsl=/usr \ --enable-zip \ --with-imap=/usr/local/imap-2007 \ --with-kerberos \ --with-imap-ssl \ --enable-intl \ --with-pcre-regex \ --with-pgsql=/usr \ --with-pdo-pgsql=/usr \ --with-freetype-dir=/usr/X11 \ --with-jpeg-dir=/usr \ --with-png-dir=/usr/X11
Now test and install if all is good
- make test
- sudo make install
after installation again type
php --version
Settting up php mysql and apache on mac osx see this guide >>




note that make install by default installs files to the /usr/local folder. If you are like me, the php that came with mac is actually in the /usr directory, not the /usr/local one. In this case, the old version will still be on your system along with the newly installed one. Installing it adds /usr/local/bin to your $PATH, but after /usr/bin, so your system will still be using the old php version. To fix this, simply edit /etc/paths and put /usr/local/bin before /usr/bin.
ReplyDeleteor set the directory prefix
ReplyDelete./configure --prefix=/usr/local/
thanks nice info ! @3783c5ef9a033114207a1ccec4c3e9c5:disqus
I am trying to upgrade my php from 5.3.15 to 5.4.8 on Mac OSX 10.8.2. I've manually download source codes of php 5.4.8 and executed:
ReplyDelete./configure --prefix=/usr/local
make
sudo make install
I also add "export PATH=/usr/local/bin:$PATH" to my /etc/profile file.
After that, php -v shows 5.4.8, while still shows 5.3.15. Any ideas on how to get to point to the new installed php version?
remove old php installation
ReplyDeleteand check terminal start as login shell
include php path in .bash_login or bashrc file
then quite and start terminal again
or another easy option use brew taps
ReplyDeletehttps://github.com/josegonzalez/homebrew-php
First of all, thank you for responding. Much appreciated!Two questions:
ReplyDelete(1) Is the old installation in /usr/bin and the new in /usr/local/bin?
(2) How do you check the terminal start as login shell? I am not a UNIX guru.
First :
ReplyDeleteset terminal as login shell
if you are using osx default terminal then click on preferences and make sure its checked as default login sheell in startup tab
open ~/.bash_login in text editor
export PATH="$HOME/bin:$PATH"
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
save and quite terminal and start again
second :
you can find about php
$php -v
$which php
Hi Alok,
ReplyDelete* My terminal was set to default login shell.
* I did not have ~/.bash_login file created, so created it has supplied to export PATH information.
* Restarted the terminal and also my system. Same result. php -v shows 5.4.8, while phpinfo( ) shows 5.3.15.
* "$ which php" has the path "/usr/local/bin/php"
* There is another php in /usr/bin/php, which I believe is the original php 5.3.15 that shipped with OSX
So,
would you recommend to remove the php in /usr/bin/php? If so, what is
the safest method to do so? OR should I try another method to install
php?
- Rob
safest way
ReplyDeleterename old php and create a symlink of new php
$sudo mv /usr/binphp /usr/bin/php.bak
$sudo ln -s /usr/local/bin/php /usr/bin/php
try that method you can revert back whenever you want just rename again and remove bak extension
or you can .. change bash_login or bash profile
ReplyDeleteand put /usr/local/bin first then /usr/bin
(Y) give a shot
Moved /usr/bin/php to /usr/bin/php.bak and created the link. Still no luck. phpinfo still shows 5.3.15 and php --version shows 5.4.8 (see attached screen captures). Also, /usr/local/bin is listed first in bash_login. Any other ideas?
ReplyDeletecheck Apache is loading newer php5 version module
ReplyDeleteIn my httpd.conf file, my load module line is "LoadModule php5_module libexec/apache2/libphp5.so". Did a search for libphp*.* on my system and I only have one file that matches and it's /usr/libexec/apache2/libphp5.so.
ReplyDeleteMy load module line in https.conf is "LoadModule php5_module libexec/apache2/libphp5.so". Did a search for libphp*.* and only /usr/libexec/apache2/libphp5.so is found.
ReplyDeleteMy load module line in https.conf is "LoadModule php5_module libexec/apache2/libphp5.so". Did a search for libphp* and only /usr/libexec/apache2/libphp5.so is found.
ReplyDeleteMy load module line in https.conf is "LoadModule php5_module libexec/apache2/libphp5.so". Did a search for libphp*.* and only /usr/libexec/apache2/libphp5.so is found.
ReplyDeleteMy load module line in httpd.conf is LoadModule php5_module libexec/apache2/libphp5.so Only have one file found in /usr/libexec/apache2
ReplyDeleteMy load module line in https.conf is "LoadModule php5_module libexec/apache2/libphp5.so". Did a search for libphp*.* and only /usr/libexec/apache2/libphp5.so is found.
ReplyDeleteMy httpd.conf line is LoadModule php5_module libexec/apache2/libphp5.so. I only have one of these in /usr/libexec/apache2
ReplyDeleteMy load module in my httpd.conf file is "LoadModule php_module libexec/apache2/libphp5.so". I did a search for libphp* and only this one file appears in libexec/apache2. Wouldn't I have two of these files when compiling the new php version? Hence, point Apache to the new .so version.
ReplyDeleteMy httpd.conf file has a load module line as "LoadModule php5_module libexec/apache2/libphp5.so". Did a search on libphp* and I only have one of these files that matches which is in /libexec/apache2.
ReplyDeleteAlso the same problem after your manual - 5.4 only in terminal
ReplyDeleteMy httpd.conf file has a load module line as "LoadModule php5_module libexec/apache2/libphp5.so". Did a search on libphp* and I only have one of these files that matches which is in /libexec/apache2.
ReplyDeletephpinfo ();
ReplyDeleteand
$php = PHP_BINDIR;
echo $php;
As the same problem i also tried and it is a bit strange - PHP_BINDIR = /usr/bin and phpversion() = 5.3.15, but in console
ReplyDelete192-168-0-100:$ /usr/bin/php -v
PHP 5.4.8 (cli) (built: Nov 8 2012 09:30:58)
try to remove all php related installed files from every location you can find
ReplyDeleteand try to re-install
if dont work for you then simply switch to brew php dupes ( search in google php-dupe brew )
I am afraid that after that I lost php
ReplyDeleteuse brew php taps
ReplyDeletehttps://github.com/josegonzalez/homebrew-php
because my hdd is corrupted so mac is not booting