跳到主要内容

Mac开发环境配置

macOS系统下的完整开发环境搭建指南

准备工作

系统要求

推荐系统:macOS Monterey (12) 及以上
支持芯片:Intel / Apple Silicon (M1/M2/M3)
磁盘空间:至少20GB可用空间

检查系统信息

点击左上角 Apple 图标 →"关于本机",查看:

  • macOS 版本
  • 芯片类型(Intel 或 Apple M系列)

打开终端

方法一:Spotlight搜索
按 Command + 空格,输入 Terminal,回车

方法二:应用程序
访达 → 应用程序 → 实用工具 → 终端

安装Xcode命令行工具

为什么需要

Xcode命令行工具包含编译器、Git等基础开发工具,是Mac开发的基础。

安装步骤

# 打开终端,运行:
xcode-select --install

会弹出安装提示,点击"安装",等待完成(可能需要几分钟)。

验证安装

# 检查是否安装成功
xcode-select -p
# 输出:/Library/Developer/CommandLineTools

# 检查Git(已自动安装)
git --version
# 输出:git version 2.x.x

安装Homebrew

什么是Homebrew

Homebrew 是macOS最流行的包管理器,用一行命令安装软件。

没有Homebrew:搜索官网 → 下载 → 安装 → 配置
有了Homebrew:brew install 软件名 # 一行搞定

安装Homebrew

# 官方安装命令
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果下载慢,使用国内镜像:

# 使用清华镜像安装
/bin/bash -c "$(curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install/install.sh)"

安装后配置

Apple Silicon (M1/M2/M3) Mac:

# 添加到PATH(安装完成后会提示)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Intel Mac:

通常自动配置好,无需额外操作。

验证安装

brew --version
# 输出:Homebrew 4.x.x

配置镜像源(加速下载)

# 替换brew.git
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换homebrew-core.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换homebrew-cask.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 刷新
brew update

安装Python

为什么不用系统自带Python

macOS自带Python可能版本较旧,且与系统绑定,不建议修改。

使用Homebrew安装

# 安装Python
brew install python

# 安装完成后检查
python3 --version
# 输出:Python 3.12.x

pip3 --version
# 输出:pip 23.x.x

设置别名(可选)

# 让python命令指向python3
echo 'alias python=python3' >> ~/.zshrc
echo 'alias pip=pip3' >> ~/.zshrc
source ~/.zshrc

配置pip镜像源

# 创建配置目录
mkdir -p ~/.pip

# 创建配置文件
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

验证

# 测试安装
python3 -c "print('Hello, Python!')"

# 测试pip
pip3 install --upgrade pip

安装Node.js

方法一:Homebrew安装

# 安装Node.js LTS版本
brew install node@20

# 添加到PATH
echo 'export PATH="/opt/homebrew/opt/node@20/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

方法二:使用nvm(推荐)

nvm 是Node版本管理器,可以轻松切换不同版本。

# 安装nvm
brew install nvm

# 配置nvm
mkdir ~/.nvm
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc
echo '[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"' >> ~/.zshrc
source ~/.zshrc

# 使用nvm安装Node.js
nvm install --lts
nvm use --lts

验证安装

node --version
# 输出:v20.x.x

npm --version
# 输出:10.x.x

配置npm镜像

npm config set registry https://registry.npmmirror.com

安装VS Code

方法一:Homebrew安装(推荐)

brew install --cask visual-studio-code

方法二:官网下载

  1. 访问 https://code.visualstudio.com/
  2. 点击 "Download for Mac"
  3. 解压下载的zip文件
  4. 将 Visual Studio Code.app 拖到应用程序文件夹

安装命令行工具

  1. 打开VS Code
  2. Command +Shift+ P
  3. 输入 "Shell Command: Install 'code' command in PATH"
  4. 回车执行

现在可以在终端中使用 code 命令打开项目:

code .  # 打开当前目录
code ~/Projects/myproject # 打开指定目录

必装插件

Command +Shift+ X 打开插件市场:

插件名用途
Chinese (Simplified)中文语言包
PythonPython开发支持
PylancePython智能提示
ESLintJavaScript代码检查
Prettier代码格式化
GitLensGit增强
Material Icon Theme文件图标美化

配置Git

安装(已通过Xcode工具安装)

git --version

基本配置

# 设置用户名和邮箱
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"

# 设置默认分支名
git config --global init.defaultBranch main

# 设置VS Code为默认编辑器
git config --global core.editor "code --wait"

# 查看配置
git config --list

配置SSH Key(用于GitHub)

# 生成SSH Key
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
# 一路回车使用默认设置

# 启动ssh-agent
eval "$(ssh-agent -s)"

# 添加SSH Key
ssh-add ~/.ssh/id_ed25519

# 复制公钥
pbcopy < ~/.ssh/id_ed25519.pub

然后:

  1. 打开 GitHub → Settings → SSH and GPG keys
  2. 点击 "New SSH key"
  3. 粘贴公钥,保存

验证:

ssh -T git@github.com
# 输出:Hi username! You've successfully authenticated...

安装iTerm2(可选但推荐)

为什么用iTerm2

iTerm2 是macOS最强大的终端替代品,功能丰富、可定制性强。

安装

brew install --cask iterm2

基本配置

  1. 打开 iTerm2 → Preferences (Command + ,)
  2. Appearance → Theme: Minimal
  3. Profiles → Colors → Color Presets: Solarized Dark
  4. Profiles → Text → Font: 选择等宽字体,建议安装 JetBrains Mono

安装Oh My Zsh(可选)

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

配置主题:

# 编辑配置
code ~/.zshrc

# 修改主题(推荐agnoster或robbyrussell)
ZSH_THEME="robbyrussell"

其他常用工具

推荐安装

# 代码搜索工具
brew install ripgrep

# JSON处理工具
brew install jq

# HTTP客户端
brew install httpie

# 目录跳转
brew install autojump
echo '[ -f /opt/homebrew/etc/profile.d/autojump.sh ] && . /opt/homebrew/etc/profile.d/autojump.sh' >> ~/.zshrc

# 文件预览增强
brew install bat # cat的替代品

# 查看磁盘占用
brew install ncdu

Homebrew常用命令

brew search 关键词   # 搜索软件
brew install 软件名 # 安装软件
brew uninstall 软件 # 卸载软件
brew list # 列出已安装
brew update # 更新Homebrew
brew upgrade # 升级所有软件
brew cleanup # 清理旧版本
brew doctor # 诊断问题

开发环境验证

完整检查

# 创建检查脚本
cat > ~/check_env.sh << 'EOF'
#!/bin/bash
echo "=== macOS 开发环境检查 ==="

echo -e "\n[Homebrew]"
brew --version

echo -e "\n[Python]"
python3 --version
pip3 --version

echo -e "\n[Node.js]"
node --version
npm --version

echo -e "\n[Git]"
git --version

echo -e "\n[VS Code]"
code --version

echo -e "\n=== 检查完成 ==="
EOF

chmod +x ~/check_env.sh
~/check_env.sh

创建第一个Python项目

# 创建项目目录
mkdir -p ~/Projects/hello-python
cd ~/Projects/hello-python

# 创建虚拟环境
python3 -m venv venv

# 激活虚拟环境
source venv/bin/activate

# 用VS Code打开
code .

创建 hello.py

print("Hello, Python!")
print("Mac开发环境配置成功!")

运行:

python hello.py

常见问题解决

Homebrew安装慢

# 使用镜像源,见上文"配置镜像源"部分

zsh: command not found

# 检查PATH
echo $PATH

# 重新加载配置
source ~/.zshrc

权限问题

# 修复Homebrew权限
sudo chown -R $(whoami) /opt/homebrew

# 修复npm权限
sudo chown -R $(whoami) ~/.npm

Apple Silicon兼容性

# 检查软件是否原生支持
file $(which python3)
# 如果显示arm64,说明是原生支持

本章小结

  1. Xcode工具:Mac开发必装,包含Git
  2. Homebrew:Mac必备包管理器
  3. Python:通过Homebrew安装最新版
  4. Node.js:推荐用nvm管理版本
  5. VS Code:安装命令行工具
  6. iTerm2:可选,更强大的终端

下一步

环境配置完成!接下来深入了解代码编辑器的使用。

→ 继续阅读:10-代码编辑器选择与配置