Macos change default php version

For brew users, you might not need php55 if you already have 54 - but regardless you should probably run these commands

$ brew install php54 php54-xdebug php54-mcrypt

you can substitute 54 with the latest number if you want. last i checked it was on 56

then based on some of the comments above you should run:

$ php --version

it will probably display:

PHP 5.4.24 [cli] [built: Jan 19 2014 21:32:15] 
Copyright [c] 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright [c] 1998-2013 Zend Technologies

if it does, then change your .bash_profile like some of the other commenters my brew instance [i think its default] lives in

/usr/local/Cellar
/usr/local/bin

as things get installed using brew, they get put in Cellar and linked through the bin. but in this case we need to override the php

$ sudo nano ~/.bash_profile

mine kinda looks like:

PATH=$PATH:~/bin
PATH=$PATH:/usr/local/bin
PATH=$PATH:/usr/local/sbin
PATH=$PATH:/usr/local/Cellar/r/2.15.1/R.framework/Versions/Current/Resources/li$
export PATH

which means that it already has access to the /usr/local/bin - but i use the alias to specify by adding this line, near the bottom:

alias php="/usr/local/bin/php"

save and close the terminal window, open up another one, and it should show something like:

$ php --version
PHP 5.4.27 [cli] [built: Apr 24 2014 17:16:35] 
Copyright [c] 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright [c] 1998-2014 Zend Technologies
    with Xdebug v2.2.4, Copyright [c] 2002-2014, by Derick Rethans

Instructions on how to change preinstalled Mac OS X PHP to MAMP's PHP Installation and then install Composer Package Management

Instructions to Change PHP Installation

First, Lets find out what version of PHP we're running [To find out if it's the default version].

To do that, Within the terminal, Fire this command:

which php

This should output the path to the default PHP install which comes preinstalled by Mac OS X, by default it has to be [Assuming you've not changed it before]:

/usr/bin/php

Now, We just need to swap this over to the PHP that is installed with MAMP, which is located at /Applications/MAMP/bin/php/php5.4.10/bin [MAMP 2.1.3]

To do this, We need to edit the .bash_profile and add the MAMP version of PHP to the PATH variable.

Follow these simple steps:

  1. Within the Terminal, run vim ~/.bash_profile

  2. Type i and then paste the following at the top of the file:

     export PATH=/Applications/MAMP/bin/php/php5.4.10/bin:$PATH
    
  3. Hit ESC, Type :wq, and hit Enter

  4. In Terminal, run source ~/.bash_profile

  5. In Terminal, type in which php again and look for the updated string. If everything was successful, It should output the new path to MAMP PHP install.

  6. In case it doesn't output the correct path, try closing the terminal window [exit fully] and open again, it should apply the changes [Restart in short].

Install Composer Package Management

Now you can fire the command to install the composer globally [So you can access it from anywhere]:

$ curl -sS //getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

You can verify your installation worked by typing the following command within the Terminal:

composer

It'll show you the current version and a list of commands you can use if the installation was successful.

Homebrew makes managing packages on macOS easy. However, Apple’s new silicon is based on ARM64 architecture. This means that not every package or app can run natively [yet]. Thankfully, Rosetta 2 helps bridge the gap by “translating software” so it can run on this new architecture.

While setting up my new MacBook Pro, I ran into this error while trying to install PHP 7.4:

brew install [email protected]
Error: Cannot install under Rosetta 2 in ARM default prefix [/opt/homebrew]!
To rerun under ARM use: arch -arm64 brew install ...

Turns out, PHP 7.4 wanted nothing to do with Rosetta, so I needed to specify the architecture first:

arch -arm64 brew install [email protected]

Then I updated the PATH in my .zshconfig , so the terminal can find PHP 7.4:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

I restarted my terminal and verified the PHP version:

php -v

Now I see PHP 7.4:

PHP 7.4.27 [cli] [built: Dec 16 2021 18:02:37] [ NTS ]
Copyright [c] The PHP Group
Zend Engine v3.4.0, Copyright [c] Zend Technologies
with Zend OPcache v7.4.27, Copyright [c], by Zend Technologies

Switch Between PHP Versions

Switching between versions of PHP is the same process: install [you only have to install once] and then update the PATH. Here are some examples:

PHP 8.1

brew install php

Update the path:

echo 'export PATH="/opt/homebrew/opt/php/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php/sbin:$PATH"' >> ~/.zshrc

PHP 8.0

brew install [email protected]

Update the path:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

PHP 7.4

arch -arm64 brew install [email protected]

Update the path:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

Because PHP 7.3 and below are no longer supported, Homebrew won’t let you install:

arch -arm64 brew install [email protected]

Will throw an error:

Error: [email protected] has been disabled because it is a versioned formula!

Gotchas

You may need to unlink/link PHP version using Homebrew if you see an error like:

Error: Could not symlink include/php/TSRM/TSRM.h
Target /usr/local/include/php/TSRM/TSRM.h is a symlink belonging to php. You can unlink it: brew unlink php

You could solve for this by trying:

brew unlink php
brew link [email protected]

I hope you found this quick tip helpful, and if you have anything to add, please drop a comment below.

How do I change my default PHP on Mac?

Follow these simple steps:.
Within the Terminal, run vim ~/.bash_profile..
Type i and then paste the following at the top of the file: export PATH=/Applications/MAMP/bin/php/php5.4.10/bin:$PATH..
Hit ESC , Type :wq , and hit Enter..
In Terminal, run source ~/.bash_profile..

How do I update PHP 8 on Mac?

Upgrading with Homebrew.
Normal upgrade. brew upgrade php..
Upgrade with shivammathur/homebrew-php. brew tap shivammathur/php brew install shivammathur/php/php@8.1. To switch between versions, use the following command: brew link --overwrite --force php@8.1. ... .
Next steps. Check the current version by running php -v : php -v..

How do I run multiple PHP versions on Mac?

Follow my steps:.
Check installed php versions by brew, for sure everything good > brew list | grep php #output php56 php56-intl php56-mcrypt php71 php71-intl php71-mcrypt..
Run script > switch-php 71 # or switch-php 56 #output PHP version [71] found Switching from [php56] to [php71] ....

How do I downgrade PHP version on Mac?

“downgrade php version in mac” Code Answer's.
#Install php version 7.3..
brew install php@7.3..
#Install the required PHP to your PATH..
echo 'export PATH="/usr/local/opt/php@7.3/bin:$PATH"' >> ~/.bash_profile..
echo 'export PATH="/usr/local/opt/php@7.3/sbin:$PATH"' >> ~/.bash_profile..
source ~/.bash_profile..
#Check Version..

Chủ Đề