SoFunction
Updated on 2025-03-10

How to upgrade PHP7.1 to PHP7.4.15 under MacOS

Recently, when writing SDK, object type prompts are required. PHPStorm smart prompts say that PHP7.2 or above is required to support this type prompt.
I checked that my native PHP was version 7.1.30, so I considered upgrading the PHP version.
First, try to upgrade using the following command:

brew update 
brew upgrade [email protected]

The first error encountered is as follows:

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

Then follow the reminder to execute the "git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow" command and will also report an error:

fatal: dumb http transport does not support shallow capabilities

It is therefore suspected that the source of homebrew-core may not be used normally due to some force majeure reasons, which leads to the failure of the fetch operation. You can consider changing to a domestic source and writing a shell as follows:

# Switch to the homebrew-core directorycd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# Set homebrew source as the domestic image of the University of Science and Technologygit remote set-url origin /
# Update homebrew-coregit -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow

After execution, you can see the following output, and then you can execute brew update smoothly.

remote: Enumerating objects: 539863, done.
remote: Counting objects: 100% (539826/539826), done.
remote: Compressing objects: 100% (194417/194417), done.
remote: Total 530481 (delta 341532), reused 521981 (delta 333211)
Receiving objects: 100% (530481/530481), 191.29 MiB | 9.18 MiB/s, done.
Resolving deltas: 100% (341532/341532), completed with 8120 local objects.

The method to install php7.4 is very simple, you can use the source code to download and compile the method:

brew install --build-from-source [email protected]

You can see a lot of output, and in the end, you can complete the installation of php7.4 without an error.

To successfully switch the previous PHP version in the terminal, you need to set environment variables in the .bash_profile and add the following statement:

export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH=/usr/local/opt/[email protected]/sbin:$PATH

Finally, execute the source ~/.bash_profile command to complete the effective operation.

In order to confirm whether it really takes effect, you can execute the following command php -v
The output is correct:

PHP 7.4.15 (cli) (built: Feb 21 2021 20:08:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
  with Zend OPcache v7.4.15, Copyright (c), by Zend Technologies

The reference information is as follows:

Homebrew domestic source replacement methodUpgraded to php7.2 to php7.4

This is the article about how to upgrade PHP7.1 to PHP7.4.15 under MacOS. For more related content on PHP7.1 to PHP7.4.15, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!