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

php --version

you will see something like this

or if you will check with phpinfo function you will see your php installation info

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 

now customise and configure php installation according to your need

./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
  if above method dont work for you use
  http://php-osx.liip.ch/

Settting up php mysql and apache on mac osx see this guide >> 

Share →
  • ydk2

    I use php-brew to install 5.4.13 is fully automatic, after it I dont need any zend-server and other only Mac http share. :)

  • http://www.computersnyou.com/ Alok Yadav

    use brew php taps
    https://github.com/josegonzalez/homebrew-php

    because my hdd is corrupted so mac is not booting

  • http://www.facebook.com/kvasilij Кирилл Пчёлкин

    I am afraid that after that I lost php

  • http://www.computersnyou.com/ Alok Yadav

    try to remove all php related installed files from every location you can find
    and try to re-install

    if dont work for you then simply switch to brew php dupes ( search in google php-dupe brew )

  • http://www.facebook.com/kvasilij Кирилл Пчёлкин

    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

    192-168-0-100:$ /usr/bin/php -v

    PHP 5.4.8 (cli) (built: Nov 8 2012 09:30:58)

  • http://www.computersnyou.com/ Alok Yadav

    phpinfo ();

    and

    $php = PHP_BINDIR;
    echo $php;

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • nonamez

    Also the same problem after your manual – 5.4 only in terminal

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My httpd.conf line is LoadModule php5_module libexec/apache2/libphp5.so. I only have one of these in /usr/libexec/apache2

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My load module line in httpd.conf is LoadModule php5_module libexec/apache2/libphp5.so Only have one file found in /usr/libexec/apache2

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    My 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.

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    In 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.

  • http://www.computersnyou.com/ Alok Yadav

    check Apache is loading newer php5 version module

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    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?

  • http://www.computersnyou.com/ Alok Yadav

    or you can .. change bash_login or bash profile

    and put /usr/local/bin first then /usr/bin
    (Y) give a shot

  • http://www.computersnyou.com/ Alok Yadav

    safest way

    rename 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

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    Hi Alok,

    * 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

  • http://www.computersnyou.com/ Alok Yadav

    First :
    set 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

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    First of all, thank you for responding. Much appreciated!Two questions:
    (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.

  • http://www.computersnyou.com/ Alok Yadav

    or another easy option use brew taps
    https://github.com/josegonzalez/homebrew-php

  • http://www.computersnyou.com/ Alok Yadav

    remove old php installation

    and check terminal start as login shell
    include php path in .bash_login or bashrc file
    then quite and start terminal again

  • http://www.facebook.com/rob.hoth.5 Rob Hoth

    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:

    ./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?

  • http://www.computersnyou.com/ Alok Yadav

    or set the directory prefix
    ./configure –prefix=/usr/local/

    thanks nice info ! @3783c5ef9a033114207a1ccec4c3e9c5:disqus

  • pixelshaded

    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.