/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装完成后,将 Homebrew 添加到 PATH:
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
brew install git
brew install php
安装特定版本 (如 PHP 8.2):
brew install php@8.2
brew unlink php && brew link php@8.2 --force
brew install composer
pecl install xdebug
pecl install redis
brew install python
安装特定版本 (如 Python 3.9):
brew install python@3.9
brew install pyenv
添加到 shell 配置 (~/.zshrc 或 ~/.bashrc):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
安装特定 Python 版本:
pyenv install 3.9.13
pyenv global 3.9.13
Python 3.4+ 已自带 pip,如需升级:
python -m pip install --upgrade pip
推荐使用 virtualenv 或 Python 内置 venv:
python -m venv myenv
source myenv/bin/activate
brew install mysql
brew services start mysql
brew install postgresql
brew services start postgresql
brew install redis
brew services start redis
sudo apachectl start
brew install nginx
brew services start nginx
php -v
composer --version
python --version
pip --version
mysql --version
psql --version
redis-cli ping
权限问题:
sudo
执行需要管理员权限的命令端口冲突:
lsof -i :80
kill -9 <PID>
环境变量不生效:
source ~/.zshrc
或重新打开终端多版本切换问题:
brew link
和 brew unlink
按照以上步骤,您应该能够在 Mac OS 上搭建一个完善的 PHP 和 Python 开发环境。