macOS 安装 Claude Code 完整教程
本文适配 macOS 12+(Monterey 及以上)系统,支持 Intel/Apple Silicon(M1/M2/M3)全机型,涵盖 Claude Code CLI 命令行、VS Code 插件两种核心使用方式的安装、配置与验证流程。
一、前置准备(必做)
1.1 系统与环境要求
- 系统版本:macOS 12.0+(Monterey、Ventura、Sonoma 均支持)
- 硬件兼容:Intel 芯片、Apple Silicon(M1/M2/M3)芯片全机型
- 基础依赖:Python 3.8+(CLI 方式必需)、网络环境(需可访问 Anthropic 服务器)
1.2 关键准备工作
- 安装 Homebrew(推荐,简化依赖安装)
打开「终端」(访达 → 应用程序 → 实用工具 → 终端),执行以下命令:
安装过程中按提示输入电脑密码,等待完成即可。无 Homebrew 可跳过,后续采用手动安装方式。/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - 获取 Claude API Key
- 访问 Anthropic 官网控制台,注册/登录账号;
- 进入「API Keys」页面,点击「Create API Key」创建密钥;
- 复制生成的 API Key 并保存(后续配置必需,请勿泄露)。
二、安装方式一:Claude Code CLI(命令行,推荐)
通过 pip 安装官方 CLI 工具,支持终端直接执行编程交互、代码生成等操作。
2.1 安装 Python 依赖
# 1. 用 Homebrew 安装最新版 Python 3(推荐)
brew install python3
# 2. 验证 Python 版本(需 ≥3.8)
python3 --version
# 3. 升级 pip(避免安装失败)
pip3 install --upgrade pip
2.2 安装 Claude Code CLI
# 全局安装 anthropic-cli(Claude 官方 CLI 包)
pip3 install anthropic-cli
# 验证安装(输出版本号即成功)
anthropic --version
2.3 解决「command not found: anthropic」问题
若终端提示命令未识别,需配置 Python 二进制文件路径到系统环境变量:
# 1. 查看 Python 包安装路径
pip3 show anthropic-cli | grep Location
# 2. 复制路径(示例:/Users/你的用户名/Library/Python/3.11/lib/python/site-packages)
# 3. 将路径添加到环境变量(根据终端类型选择命令)
# 若终端是 zsh(macOS 默认)
echo 'export PATH="$HOME/Library/Python/3.11/bin:$PATH"' >> ~/.zshrc
~/.zshrc
>> ~/.bash_profile
~/.bash_profile

