Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Macports switch PHP CLI version

I'm trying to switch my Terminal PHP version to 5.4 because I ran into some issues with Drush while updating my Drupal core. https://drupal.stackexchange.com/questions/112090/drush-command-errors

The reason for these issues is my Terminal PHP version is different then my localhost. php -v in Terminal returns PHP 5.5.13 (cli) but my localhost is running PHP Version 5.4.29.

I started searching on how to switch my CLI version to 5.4 and found this:

sudo rm /usr/bin/php // remove /usr/bin/php first
sudo ln -s /opt/local/bin/php54 /usr/bin/php // pointing to php54
php -v // get version
PHP 5.4.9 (cli) (built: Nov 26 2012 12:40:37) 
Copyright (c) 1997-2012 The PHP Group


However, after doing this, and restarting Apache and Terminal, I'm still getting PHP 5.5.13 (cli) after php -v.

To be sure I was working in the correct folder, I checked to see if the php file was deleted after running sudo rm /usr/bin/php, and it was.

So as a test I did this: /opt/local/bin/php54 -v and got PHP 5.4.29 (cli). Which makes me think my symlink doesn't work, or I did something wrong.

Side notes:

After running sudo rm /usr/bin/php I expected php -v would give an error in Terminal, however its still giving me PHP 5.5.13 (cli).

I'm also always getting the same results when running whereis php and which php, even after the symlink.

whereis php returns /usr/bin/php and which php returns /opt/local/bin/php


#LoadModule php5_module        modules/mod_php55.so
LoadModule php5_module        modules/mod_php54.so
#LoadModule php5_module        modules/mod_php53.so
#LoadModule php5_module        modules/libphp5.so

#Include conf/extra/mod_php55.conf
Include conf/extra/mod_php54.conf
#Include conf/extra/mod_php53.conf
#Include conf/extra/mod_php.conf

Bash Profile:

# MacPorts Installer addition on 2012-10-23_at_13:41:14: adding an appropriate PATH $
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
# also include mysql binaries
export PATH=$PATH:/opt/local/lib/mysql5/bin

Any help is greatly appreciated.

like image 700
Jrn Avatar asked Mar 15 '23 22:03


1 Answers

Do not modify files in /usr/bin. That's Apple's turf, and there are always other possibilities to avoid changing things there, especially since Apple's next update will happily revert these changes again and scripts might rely on /usr/bin/php being exactly the version Apple shipped with the OS. Put the original binary back where it belongs.

Also, as you noticed, which php (or type php, which is often more accurate because it includes things as Shell aliases) returned /opt/local/bin/php, which tells you which php binary (or symlink) gets executed when you type php on the shell. Since that's /opt/local/bin/php, that's what you need to change.

MacPorts has a mechanism that allows you to adjust these preferred versions. You should use it over symlinking stuff to places manually, because in addition to the standard PHP binary, it will also symlink related stuff such as php-config, phpize and their corresponding manpages. This mechanism is called port select.

To list the versions of PHP available for selection, run port select --list php. To choose a preferred version, run sudo port select --set php $versionIdentifier. For your use case of PHP 5.4, that's likely going to be sudo port select --set php php54.

Remember to run hash -r after this command to flush any caches your shell might have on the location of the php binary.

like image 75
neverpanic Avatar answered Apr 06 '23 04:04