Change php cli version mac
I've got a brand new Mac yesterday and noted that it states PHP will be removed from future Mac OS versions by default. Show
I'm pretty surprised they go this way. By default, it comes with PHP 7.3, and I needed 7.4 for my project so let me guide you through setting up PHP on your Mac! Mac's warning looks like this:
However, don't be scared. It's pretty easy to install PHP and even install multiple versions if you like. Installing HomebrewWhen it comes to installing software on your Mac, we need only one package manager, and it's Homebrew. It can install any package or software you want and even install specific versions. Read more on Homebrew - Package manager for Mac. Quick guide: Run the following command in your terminal:
Installing PHP with Homebrew on MacTo install PHP, we can run the following command:
This will install the latest stable version of PHP (At the moment of writing, this is PHP 8.0). Before running any brew commands, it's a good habit to run the following commands first. These will check if Homebrew is all up to date and running the latest versions.
Installing PHP 7.4 with HomebrewIn my case, I wanted to install PHP 7.4 since it's the version our server is running. To install a specific version, we can use the @ notation.
This will run the installer, and it should end with a success notice in your terminal. However, even though this installed PHP, it didn't change our running instance yet. So if we run the To fix this, we need to link the correct PHP version. Switching PHP Versions with Homebrew on MacNow that we installed versions, we can easily switch between them using the First, check which version of PHP is currently running:
Then we can unlink that version by using:
The next step is to link the version we want:
It will tell you to run a script to add the path:
This will make sure the right PHP version is loaded,
now if you run
And there we go. We switched to the PHP version. Php -v is still showing the wrong versionI had the issue when upgrading from 7.4 to 8.0 for my demo that I kept seeing 7.4 when running
Remove the line that points to your old instance of PHP.
Thank you for reading, and let's connect!Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter Homebrew, the popular package manager for macOS, allows you to install more than one version of PHP. Now, let us assume you have installed PHP 7.4 and PHP 8.1 with HomeBrew. If you want to switch from PHP 7.4 to PHP 8.1, you need to run the following commands:
and
How about this instead?
The following shell script (added to
💡 macOS ships with Run
to install the latest version of With this script in place, when I open a new terminal and run
I can see that the script created aliases as expected.
To switch from any version of PHP to PHP 8.1, I now only need to run
I hope it helps! How do I change PHP version in Terminal Mac?Switch Between PHP Versions. 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]. ... . PHP 7.4 arch -arm64 brew install [email protected].. 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 change my PHP version?Changing the PHP version. Navigate to the Manage Websites page.. Click the Manage button to the right of your domain.. Under the PHP section, click the Modify icon.. Choose a version of PHP from the dropdown menu.. Scroll down and click the Change PHP Version button to save your changes.. Where is my PHP Path Mac?Show activity on this post.. Find .ini location path. Open Terminal and run command php --ini.. copy this path /usr/local/etc/php/7.4/php.ini and open it by command nano /usr/local/etc/php/7.4/php.ini.. Make changes and Quit with the keyboard combination Ctrl+X to exit nano.. run apachectl restart after finish.. |