How to update php in mac

The solutions above did not work for me on Big Sur I kept on getting:

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-[to be removed in future macOS] [cli] [built: Dec 21 2020 21:33:25] [ NTS ]
Copyright [c] 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright [c] 1998-2018 Zend Technologies

To fix this I used the following steps:

Step 1 | Tap into another repository of formulae

brew tap shivammathur/php

Step 2 | Install the desired PHP version

brew install shivammathur/php/

Other options are:







 
 or known as just PHP

Step 3 | Link the PHP Version

brew link --overwrite --force 

Step 4 | Restart Terminal

Step 5 | Check PHP version

php -v

You should now see the new version.

PHP 7.4.25 [cli] [built: Oct 21 2021 00:29:22] [ NTS ]
Copyright [c] The PHP Group
Zend Engine v3.4.0, Copyright [c] Zend Technologies
    with Zend OPcache v7.4.25, Copyright [c], by Zend Technologies

This also solved my issues with PHP syntax in Visual Studio Code.

This was written in 2018. Now, all the brew taps in this blog have been deprecated.

Photo by Caspar Rubin on Unsplash

Using Homebrew

First, make sure that your homebrew is up to date

brew update && brew upgrade

If you didn’t have homebrew, install it right away!

Using Brew Tap command allows Homebrew to tap into another repository of formula.

brew tap homebrew/dupes
These formulae were those that duplicated software provided by macOS.

brew tap homebrew/versions
These formulae provided multiple versions of existing packages or newer versions of packages that were too incompatible to go in homebrew/core.

brew tap homebrew/homebrew-php
A centralized repository for PHP-related brews.

Checking php version using the following commands

php -v
[same as php --version]

To unlink the last version

brew unlink php
[depends on the current version on your computer]

For instance, your current php version is 5.5
brew unlink php55

To install the new version of php

brew install php
[If you need another version just change the version number]

If you need php 7.1
brew install php71

then check php version again

php -v

If everything looks fine then congratulation! you’ve finished the php updated.

But If you find the following errors
Error:Could not symlink file: /urs/local/Cellar/php...

you need to run the following commands

sudo chown -R$[whoami] /usr/local/*
brew list | while read f; do brew unlink f; brew link $f; done
brew unlink php
brew link --overwrite php

check php version again

php -v

linked to the old version?

So open you .profile or .bash_profile
like nano or vim

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

Source your file with source ~/.bash_profile or source ~/.profile
[same as close and reopen your terminal].

Using one line install

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

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

# Upgrading with Homebrew

Start by making sure brew is up-to-date:

brew update

Next, upgrade PHP. You can either use the built-in php recipe, but I recommend to use the shivammathur/homebrew-php tap.

# Normal upgrade

brew upgrade php

# Upgrade with shivammathur/homebrew-php

brew tap shivammathur/php
brew install shivammathur/php/

To switch between versions, use the following command:

brew link --overwrite --force 

You can read more in the repository.

# Next steps

Check the current version by running php -v:

php -v

Restart Nginx or Apache, if you're using Laravel Valet you can skip to the next section; you need some extra steps in order for the web server to properly work.

sudo nginx -s reload
sudo apachectl restart

And make sure that your local web server also uses PHP 8.1 by visiting this script:

# index.php, accessible to your web server

phpinfo[];

The version should show 8.1.x.

Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:

# Valet

If you're using Laravel Valet, you should do the following steps to upgrade it:

composer global update

You can use valet use to switch between PHP versions:

valet use 
valet use 

# Extensions

PHP extensions are installed using pecl. I personally use Redis and Xdebug. They can be installed like so:

pecl install redis
pecl install xdebug

You can run pecl list to see which extensions are installed:

pecl list

# Installed packages, channel pecl.php.net:
# =========================================
# Package Version State
# redis   5.3.4   stable
# xdebug  3.1.1   stable

You can search for other extensions using pecl search:

pecl search pdf

# Retrieving data...0%
# ..
# Matched packages, channel pecl.php.net:
# =======================================
# Package Stable/[Latest] Local
# pdflib  4.1.4 [stable]        Creating PDF on the fly with the PDFlib library

Make sure to restart your web server after installing new packages:

sudo nginx -s reload
sudo apachectl restart
valet restart

Make sure all extensions are correctly installed and loaded by checking both your PHP webserver and CLI installs:

php -i | grep redis
var_dump[extension_loaded['redis']];

If extensions aren't properly loaded, there are two easy fixes.

First, make sure the extensions are added in the correct ini file. You can run php --ini to know which file is loaded:

Configuration File [php.ini] Path: /opt/homebrew/etc/php/8.1
Loaded Configuration File:         /opt/homebrew/etc/php/8.1/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.1/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.1/conf.d/error_log.ini,
/opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini,
/opt/homebrew/etc/php/8.1/conf.d/php-memory-limits.ini

Now check the ini file:

extension="redis.so"
zend_extension="xdebug.so"

Note that if you're testing installed extensions via the CLI, you don't need to restart nginx, apache or Valet when making changes to ini settings.

The second thing you can do, if you're updating from an older PHP version which also used pecl to install extension; is to reinstall every extension individually.

pecl uninstall redis
pecl install redis

# Last step

Finally you should test and upgrade your projects for PHP 8 compatibility.

Footnotes

The Road to PHP 8.1

New in PHP 8.1 — A comprehensive list of all things new in PHP 8.1

PHP 8.1 benchmarks

Enums in PHP 8.1

Readonly properties in PHP 8.1

New in initializers in PHP 8.1

Fibers with a grain of salt

How do I upgrade PHP to 8.0 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 update PHP to 7.4 on Mac?

6 Answers.
brew install php@7.4..
brew link --force --overwrite php@7.4..
brew services start php@7.4..
export PATH="/usr/local/opt/php@7.4/bin:$PATH".
export PATH="/usr/local/opt/php@7.4/sbin:$PATH".

How do I update my version of PHP?

How do I change the PHP version?.
Log in to your one.com control panel..
Scroll down to the Advanced settings tile and select PHP and database settings..
Scroll down to Update PHP version..
Select the PHP version you want to switch to and click Update..

How do I check my PHP version on Mac?

2 Answers.
Go to File > Preferences > User Settings > Settings.json..
Change the value of php. validate. executablePath according to the installed directory of php7. "php.validate.executablePath": "/Applications/MAMP/bin/php/php7.0.14/bin/php".
Relaunch VM Code..

Chủ Đề