跳转到主要内容

macOS 安装指南

系统要求

  • macOS 10.15 或更高版本
  • Terminal 或 iTerm2
  • Xcode Command Line Tools
  • 稳定的网络连接

安装步骤

1. 安装 Xcode Command Line Tools

bash
xcode-select --install

2. 安装 Node.js

使用 nvm(推荐)

nvm 让您可以轻松切换 Node.js 版本:

bash
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

# 重新加载 shell 配置(macOS 默认使用 zsh)
source ~/.zshrc

# 如果使用 Bash
source ~/.bash_profile

# 配置镜像加速下载(可选,建议中国用户使用)
nvm node_mirror https://npmmirror.com/mirrors/node/
nvm npm_mirror https://npmmirror.com/mirrors/npm/

# 安装并使用 Node.js 20
nvm install 20
nvm use 20

# 设为默认版本
nvm alias default 20

# 配置 npm 镜像加速
npm config set registry https://registry.npmmirror.com

# 验证安装
node --version
npm --version

使用 Homebrew

如果您已安装 Homebrew:

bash
# 安装 Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Node.js
brew install node

# 配置 npm 镜像加速
npm config set registry https://registry.npmmirror.com

# 验证安装
node --version
npm --version

官网下载

直接从 Node.js 官网安装:

bash
# 访问 https://nodejs.org/
# 下载 macOS 安装包 (.pkg)
# 运行安装程序并按提示完成安装

# 安装完成后配置 npm 镜像加速
npm config set registry https://registry.npmmirror.com

3. 安装 Claude Code

bash
npm install -g @anthropic-ai/claude-code

如遇权限问题:

bash
sudo npm install -g @anthropic-ai/claude-code

4. 获取 API 密钥

在配置环境变量之前,您需要先获取API密钥:

  1. 访问 AI网关
  2. 注册账户并登录
  3. 访问 令牌页面 生成您的API密钥

5. 配置环境变量

使用您在AI网关获取的API密钥来设置环境变量:

临时设置(当前会话)

bash
export ANTHROPIC_AUTH_TOKEN="你的API密钥"
export ANTHROPIC_BASE_URL="https://ai-router.plugins-world.cn"

永久设置(推荐)

bash
# 添加到 zsh 配置文件(macOS 默认)
echo 'export ANTHROPIC_AUTH_TOKEN="你的API密钥"' >> ~/.zshrc
echo 'export ANTHROPIC_BASE_URL="https://ai-router.plugins-world.cn"' >> ~/.zshrc

# 如果使用 Bash
echo 'export ANTHROPIC_AUTH_TOKEN="你的API密钥"' >> ~/.bash_profile
echo 'export ANTHROPIC_BASE_URL="https://ai-router.plugins-world.cn"' >> ~/.bash_profile

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

6. 验证安装

bash
# 检查版本
claude --version

# 启动 Claude Code
claude

常见问题

权限问题

bash
# 使用 sudo 安装
sudo npm install -g @anthropic-ai/claude-code

# 或配置 npm 用户目录
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

macOS 安全设置阻止运行

bash
# 打开"系统偏好设置" → "安全性与隐私"
# 点击"仍要打开"或"允许"

# 或在终端中运行(不推荐,会降低系统安全性)
sudo spctl --master-disable

nvm 命令不存在

bash
# 重新加载配置文件
source ~/.zshrc  # 或 ~/.bash_profile
# 或重新打开终端

npm 安装速度慢

bash
# 确认使用了镜像加速
npm config get registry

# 如果未设置,手动配置
npm config set registry https://registry.npmmirror.com

M1/M2 芯片兼容性

大多数 Node.js 包已支持 Apple Silicon,如遇问题:

bash
# 使用 Rosetta 2 运行
arch -x86_64 npm install -g @anthropic-ai/claude-code

专注于 AI 编程技术分享,助力开发者提升编程效率