103

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)?

1

13 Answers 13

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you!! I was nervous this process was going to be more difficult.
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).
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!
Definitely the best option. So painless (as long as you remember to make sure your $PATH is correct as pointed out by @CodedMonkey)
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.
|
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" 

3 Comments

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...
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.
Note that homebrew/dupes and homebrew/versions are now deprecated.
16

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.

Comments

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.

1 Comment

how do you figure out the php version package name?
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.

1 Comment

You mean this? 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
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.

1 Comment

This comment saved me a lot of time!
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.

2 Comments

Apple's Security Update 2010-002 updates Leopard's PHP to 5.2.12.
PHP 5.3.1 comes with Snow Leopard.
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 

Comments

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

Comments

0

Use this Command:

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

Comments

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 

Comments

-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

2 Comments

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.
And yes...the link is broken! :-D
-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.