升级背景与初始尝试
近期开发SDK过程中遇到了object类型提示符的需求,PHPStorm提示该特性需PHP 7.2及以上版本支持。检查本机环境发现当前运行的是PHP 7.1.30版本,因此决定进行版本升级。首先尝试执行标准的升级命令。
brew update
brew upgrade php@7.4
在执行升级命令后,首次遇到的错误提示如下所示:
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!
随后按照提示执行"git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow"命令,依然出现报错:
fatal: dumb http transport does not support shallow capabilities
据此推测,homebrew-core的远程仓库可能因网络不可抗力因素导致连接异常,进而致使fetch操作失败。此时可尝试更换为国内镜像源,具体Shell脚本编写如下:
# 切换到homebrew-core目录下
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# 设置homebrew源为国内的中科大镜像
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 更新homebrew-core
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
脚本执行完毕后,观察终端输出情况,确认无误后即可顺利执行brew update命令。
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.
安装PHP 7.4的过程较为简便,推荐采用源码下载并编译的方式:
brew install --build-from-source php@7.4
编译过程会产生大量日志输出,若最终未出现错误信息,则表明PHP 7.4安装成功。
为使终端中切换后的PHP版本生效,需在.bash_profile文件中配置环境变量,添加以下语句:
export PATH="/usr/local/opt/php@7.4/bin:$PATH"
export PATH=/usr/local/opt/php@7.4/sbin:$PATH
最后,运行source ~/.bash_profile命令以加载新的配置并使其立即生效。
为验证升级是否真正生效,可执行php -v命令进行检查。若得到如下输出,则说明版本切换正确:
PHP 7.4.15 (cli) (built: Feb 21 2026 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
参考的资料如下:
homebrew国内源替换办法php7.2升级到php7.4
到此这篇关于MacOS下PHP7.1升级到PHP7.4.15的方法的文章就介绍到这了,更多相关PHP7.1升级到PHP7.4.15内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!