102

I feel this is an awfully obtuse question to ask, but strangely, this problem is poorly documented.

I would like to upgrade PHP, but there are several problems:

  • There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.
  • Running locate php indicates there are probably many dependencies.
  • I don't know HOW php was installed, as it was included with the OS, so I don't know whether I should install from source or download binaries. I also don't know the proper way to uninstall the previous version without breaking dependencies.

I am running on Leopard. I have a feeling Apple doesn't want you to upgrade. Would buying Snow Leopard and upgrade solve this problem (and future ones like it)?

hakre
  • 193,403
  • 52
  • 435
  • 836
thebossman
  • 4,598
  • 11
  • 34
  • 45

13 Answers13

91

You may want to check out Marc Liyanage's PHP package. It comes in a nice Mac OS X installer package that you can double-click. He keeps it pretty up to date.

http://php-osx.liip.ch/

Also, although upgrading to Snow Leopard won't help you do PHP updates in the future, it will probably give you a newer version of PHP. I'm running OS X 10.6.2 and it has PHP 5.3.0.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • 1
    Thank you!! I was nervous this process was going to be more difficult. – Gavin Apr 06 '14 at 15:33
  • 10
    Note that this package **does not override** the original PHP version on the Mac, so it doesn't work out of the box, you need add the new directory to the `PATH`. Instructions to make it work are on that page (I was stupid enough to miss that the first time). – Coded Monkey Apr 10 '14 at 18:52
  • 1
    Wow! After spending hours trying to get the installation process via Homebrew to work, I tried this. Super fast, super easy and it actually worked straight out of the box! Thanks! – Magnus Aug 17 '14 at 16:29
  • Definitely the best option. So painless (as long as you remember to make sure your `$PATH` is correct as pointed out by @CodedMonkey) – Darragh Enright Jun 28 '15 at 21:19
  • 2
    Also, it is worth mentioning that despite installing in a separate directory, the installer configures Apache so that all your web applications use the newly installed php version. So, the PATH trick is useful only if you run php from the command line. If you want to run web applications the installer takes care of everything. – fiacobelli Jul 30 '15 at 18:14
  • I guess we are fewer and fewer as time passes, but if you still have a 32-bit mac computer, this will not work. – Félix Adriyel Gagnon-Grenier Sep 05 '16 at 19:11
  • i dont think it's the good way for upgrade php version on mac i had tried to upgrade php version 5.5 to 5.6, the command line from php-ox.liip.ch auto install apache to my Mac although my Mac is already installed Nginx. – J Ha Dec 20 '16 at 03:36
51

I use this: https://github.com/Homebrew/homebrew-php

The command is:

$ xcode-select --install

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php

$ brew options php56
$ brew install php56

Then config in your .bash_profile or .bashrc

# Homebrew PHP CLI
export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
wangchi
  • 2,945
  • 1
  • 16
  • 11
  • Note that this won't update the embedded PHP, but install a new one. I ran into serious trouble having two versions installed, despite the confusion... – Julian F. Weinert Nov 21 '16 at 13:00
  • I tried this and also had a lot of issues, like bash crashing on launch, I had to reinstall it. Be careful when you try these commands and watch out to what Brew suggests you should do while processing these lines. – Brac Dec 28 '16 at 23:21
  • Note that homebrew/dupes and homebrew/versions are now deprecated. – medinasod Apr 19 '17 at 15:59
15

I think one simple way to do it, is:

1 - Check you where is your current PHP:

$ which php
$ /usr/local/bin/php

You see? Usually, our commands that we run is a link in /usr/local/bin so...

2 - Unlink this current link of PHP

unlink /usr/local/bin/php

If you prefere, before unlink it, check the path and then remove php files (do ls -al /usr/local/bin | grep php and then rm -rf into desired path)

3 - Install PHP 7.1

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.1

4 - Create new link (using php 7.1 bin that you have installed)

ln /usr/local/php5-7.1.9-20170914-100859/bin/php /usr/local/bin/php

Like I said, its a simple way I think.

Paulo Victor
  • 3,814
  • 2
  • 26
  • 29
8

There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.

You could still install it with MacPorts. sudo port install php52 (or whichever version you want) will install PHP.

It won't overwrite the Apple-supplied version. It'll install it under /opt/local. You can add /opt/local to the beginning of your $PATH, and use the MacPorts version in your Apache config.

mipadi
  • 398,885
  • 90
  • 523
  • 479
6

Option #1

As recommended here, this site provides a convenient, up-to-date one liner.

This doesn't overwrite the base version of PHP on your system, but instead installs it cleanly in /usr/local/php5.

Option #2

My preferred method is to just install via Homebrew.

Community
  • 1
  • 1
user456584
  • 86,427
  • 15
  • 75
  • 107
  • 1
    You mean [this](https://github.com/josegonzalez/homebrew-php)? Ideally you would sum up everything here, you know, for redundancy in case link breaks and for simplicity! Also worth noting it won't replace the apple php either. ;P – cregox Sep 11 '15 at 23:41
3

Before I go on, I have the latest version (v5.0.15) of OS X Server (yes, horrible, I know...however, the web server seems to work A-OK). I searched high and low for days trying to update (or at least get Apache to point to) a new version of PHP. My mcrypt did not work, along with other extensions and I installed and reinstalled PHP countless times from http://php-osx.liip.ch/ and other tutorials until I finally noticed a tid-bit of information written in a comment in one of the many different .conf files OS X Server keeps which was that OS X Server loads it's own custom .conf file before it loads the Apache httpd.conf (located at /etc/apache2/httpd.conf). The server file is located:

/Library/Server/Web/Config/apache2/httpd_server_app.conf

When you open this file, you have to comment out this line like so:

#LoadModule php5_module libexec/apache2/libphp5.so

Then add in the correct path (which should already be installed if you have installed via the http://php-osx.liip.ch/ link):

LoadModule php5_module /usr/local/php5/libphp5.so

After this modification, my PHP finally loaded the correct PHP installation. That being said, if things go wonky, it may be because OS X is made to work off the native installation of PHP at the time of OS X installation. To revert, just undo the change above.

Anyway, hopefully this is helpful for anyone else spending countless hours on this.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
2

Upgrading to Snow Leopard won't solve the your primary problem of keeping PHP up to date. Apple doesn't always keep the third party software that it bundles up to date with OS updates. And relying on Apple to get you the bug fix / security update you need is asking for trouble.

Additionally, I would recommend installing through MacPorts (and doing the config necessary to use it instead of Apple's PHP) rather than try to upgrade the Apple supplied PHP in place. Anything you do to /usr/bin risks being overwritten by some future Apple update.

Dave Bacher
  • 15,652
  • 3
  • 63
  • 86
2

Saving on keystrokes, this worked on MacOS Sierra:

$ brew install homebrew/php/php71

$ /usr/local/opt/php71/bin/php -v
PHP 7.1.4 (cli) (built: Apr 14 2017 15:02:16) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
2

Check your current php version in terminal with the following command,

$ php -v

You see current php version in terminal, and next command run in terminal if you want to upgrade your php version with php concat with version liked as,

$ brew install homebrew/php/php71

Please restart terminal if you finished php version upgrade installed and run the command.

$ php -v

Now you see the current php version in terminal....thank

yekyawaung
  • 201
  • 2
  • 7
0

Use this Command:

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.0
Aghaie
  • 44
  • 7
0

You can use curl to update php version.

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.3

Last Step:

export PATH=/usr/local/php5/bin:$PATH

Check the upgraded version

php -v
Shuvo Habib
  • 2,035
  • 1
  • 20
  • 25
-1

best way to upgrade is compile it from source

see this tutorial that may be helful for you

http://www.computersnyou.com/2012/09/how-to-upgrade-php-in-mac-osx-compiling.html

riyush
  • 7
  • 1
  • 21
    You should avoid simply linking to a resource without summarizing it's contents here. When the link breaks your answer will no longer be relevant. – Scott Oct 05 '12 at 09:53
  • 1
    And yes...the link is broken! :-D – emale Mar 21 '19 at 15:09
-2

to upgrade php7 to latest stable version brew upgrade php7 or for php5.X to latest stable version

brew upgrade php56

use brew list to check installed version

Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78