【2021/12/14】 Intel CPU mac 安装 homebrew 和 brew 安装php74

参考

  1. MacOS M1使用Homebrew搭建PHP运行环境
  2. Mac入门--通过Homebrew安装PHP(新)
  3. Homebrew / Linuxbrew 镜像使用帮助-清华大学开源软件镜像站 HOME EVENTS BLOG RSS PODCAST MIRRORS

原因

因为最新版本的mac os 不再包含php,需要进行手动安装。最简单的就是通过brew 包管理器进行安装了。

但是搜索了很多教程安装brew,虽然安装成功,但是都搜索不到php;并且还出现访问404之类的问题。

本文主要参考 清华大学开源软件镜像站 的帮助教程,然后一些需要注意的地方参考的 1、2 文章。

安装brew步骤

  1. 在终端输入以下几行命令设置环境变量:
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
  1. 从镜像下载安装脚本并安装 Homebrew / Linuxbrew
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install
  1. 替换现有仓库上游
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
for tap in core cask{,-fonts,-drivers,-versions} command-not-found; do
    brew tap --custom-remote --force-auto-update "homebrew/${tap}" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done
brew update
  1. 推荐用户将这两个环境变量设置加入 shell 的 profile 设置中。
test -r ~/.bash_profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.bash_profile  # bash
test -r ~/.bash_profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.bash_profile
  1. 镜像迁移说明:Linuxbrew 核心仓库(linuxbrew-core)自 2021 年 10 月 25 日(brew 版本 3.3.0 起)被弃用,Linuxbrew 用户应迁移至 homebrew-core。Linuxbrew 用户请依新版镜像说明重新设置镜像。注意迁移前请先运行 brew update 将 brew 更新至 3.3.0 或以上版本。迁移过程中若出现任何问题,可使用如下命令重新安装 homebrew-core:
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
rm -rf "$(brew --repo homebrew/core)"
brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

安装php步骤

  1. 终端搜索php
brew search php
# 输出
brew-php-switcher   php@7.2             phplint             pcp
php                 php@7.3             phpmd               pup
php-code-sniffer    php@7.4 ✔           phpmyadmin
php-cs-fixer        php@8.0             phpstan
php-cs-fixer@2      phpbrew             phpunit
  1. 安装
brew install php@7.4
  1. 安装完毕后php不能使用,还需要创建链接
brew link php@7.4