Claude Code 终极安装与配置指南
最详细的 Claude Code 安装教程,Windows/macOS/Linux 全平台支持,包含环境配置、API设置、常见问题解答
Claude Code 终极安装与配置指南
本教程基于社区最佳实践,提供最详细的 Claude Code 安装配置指南。
前置要求
- Node.js 18+(推荐 22 LTS)
- Git
- 有效的 API Key(官方 Anthropic API 或第三方中转站)
Windows 安装(推荐方案)
Windows 用户推荐使用以下完整的环境配置方案,可获得最佳体验。
第一步:安装 Windows Terminal
Windows Terminal 是微软官方的现代终端,支持多标签、GPU 加速渲染。
安装方式:
- 打开 Microsoft Store
- 搜索 Windows Terminal
- 点击安装
或使用 WinGet(需要先安装 WinGet):
winget install Microsoft.WindowsTerminal
第二步:安装 WinGet 包管理器
WinGet 是 Windows 官方包管理器,类似于 macOS 的 Homebrew。
检查是否已安装:
winget --version
如未安装,从 Microsoft Store 安装:
- 搜索 App Installer
- 安装或更新
第三步:安装 PowerShell 7
PowerShell 7 是跨平台的现代 PowerShell,比 Windows 自带的 PowerShell 5 更强大。
winget install Microsoft.PowerShell
安装后,在 Windows Terminal 中可以选择 PowerShell 7 作为默认 Shell。
第四步:安装 Git
winget install Git.Git
安装后重启终端,验证:
git --version
第五步:安装 fnm(Node 版本管理器)
fnm (Fast Node Manager) 是用 Rust 编写的高性能 Node.js 版本管理器,比 nvm 更快。
winget install Schniz.fnm
配置 fnm 自动切换:
在 PowerShell 配置文件中添加初始化脚本:
# 打开配置文件
notepad $PROFILE
添加以下内容:
fnm env --use-on-cd --shell power-shell | Out-String | Invoke-Expression
保存后重启终端。
第六步:安装 Node.js
使用 fnm 安装 Node.js LTS 版本:
# 安装最新 LTS 版本
fnm install --lts
# 设置为默认版本
fnm default lts-latest
# 验证安装
node -v
npm -v
第七步:安装 Claude Code
npm install -g @anthropic-ai/claude-code
验证安装:
claude --version
第八步:配置 API
方式一:使用官方 API
# 设置环境变量(永久生效)
[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-ant-xxxxx", "User")
方式二:使用第三方中转站
[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "your-api-key", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://your-proxy.com/v1", "User")
方式三:跳过官方登录(使用中转站必需)
如果使用第三方 API,需要在配置文件中添加:
# 创建配置文件
$configPath = "$env:USERPROFILE\.claude.json"
$config = @{
hasCompletedOnboarding = $true
}
$config | ConvertTo-Json | Set-Content -Path $configPath -Encoding UTF8
重要:设置环境变量后,需要重新打开终端窗口才能生效。
Windows 一键安装脚本
如果觉得上述步骤太繁琐,可以使用我们的一键安装脚本:
# 以管理员身份运行 PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force
irm https://llmhu.me/scripts/install-claude-code-windows.ps1 | iex
脚本会自动安装 Chocolatey、Git、Node.js 和 Claude Code,并引导你配置 API。
macOS 安装
方式一:一键安装
curl -fsSL https://llmhu.me/scripts/install-claude-code-macos.sh | bash
方式二:手动安装
# 1. 安装 Homebrew(如未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 2. 安装 fnm(推荐)或直接安装 Node.js
brew install fnm
# 配置 fnm(添加到 ~/.zshrc)
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.zshrc
source ~/.zshrc
# 3. 安装 Node.js
fnm install --lts
fnm default lts-latest
# 4. 安装 Claude Code
npm install -g @anthropic-ai/claude-code
# 5. 验证
claude --version
配置 API
编辑 ~/.zshrc:
# 官方 API
export ANTHROPIC_API_KEY="sk-ant-xxxxx"
# 或中转站 API
export ANTHROPIC_API_KEY="your-api-key"
export ANTHROPIC_BASE_URL="https://your-proxy.com/v1"
运行 source ~/.zshrc 生效。
Linux 安装
方式一:一键安装
curl -fsSL https://llmhu.me/scripts/install-claude-code-linux.sh | bash
方式二:手动安装(Ubuntu/Debian)
# 安装 fnm
curl -fsSL https://fnm.vercel.app/install | bash
# 配置 fnm
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.bashrc
source ~/.bashrc
# 安装 Node.js
fnm install --lts
fnm default lts-latest
# 安装 Claude Code
npm install -g @anthropic-ai/claude-code
配置 API
编辑 ~/.bashrc:
export ANTHROPIC_API_KEY="your-api-key"
export ANTHROPIC_BASE_URL="https://your-proxy.com/v1" # 如使用中转站
启动 Claude Code
# 进入项目目录
cd /path/to/your/project
# 启动 Claude Code
claude
常见问题 FAQ
基础问题
Q: 提示 "command not found: claude"
解决方案:
- 重新打开终端窗口
- 检查 npm 全局路径是否在 PATH 中:
npm config get prefix - 将该路径添加到系统 PATH
Q: 如何更新 Claude Code?
npm update -g @anthropic-ai/claude-code
Q: 如何卸载?
npm uninstall -g @anthropic-ai/claude-code
API 相关
Q: 如何切换官方 API 和中转站?
修改环境变量即可:
- 使用官方 API:只设置
ANTHROPIC_API_KEY - 使用中转站:同时设置
ANTHROPIC_API_KEY和ANTHROPIC_BASE_URL
Q: 使用中转站时显示需要登录?
在 ~/.claude.json 中添加:
{
"hasCompletedOnboarding": true
}
Q: 如何使用 OpenRouter 等聚合 API?
export ANTHROPIC_API_KEY="your-openrouter-key"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1"
功能相关
Q: 如何进入 Plan Mode(规划模式)?
在 Claude Code 中输入:
/chat
Plan Mode 用于更长的、需要规划的任务,Claude 会先制定计划再执行。
Q: 如何粘贴图片?
Claude Code 支持图片输入:
- 直接粘贴剪贴板中的图片(Ctrl+V / Cmd+V)
- 使用文件路径引用:
@/path/to/image.png
Q: 如何查看 Token 消耗?
/cost
Q: 如何压缩上下文节省 Token?
/compact
Windows 特有问题
Q: Windows 上如何配置 MCP 服务器?
在 %USERPROFILE%\.claude.json 中配置:
{
"hasCompletedOnboarding": true,
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "C:/Users/YourName"]
}
}
}
注意:Windows 路径使用正斜杠 / 或双反斜杠 \
Q: PowerShell 执行策略报错?
以管理员身份运行:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Q: npm 安装很慢?
使用国内镜像:
npm config set registry https://registry.npmmirror.com
网络问题
Q: 连接超时怎么办?
- 使用第三方中转站(推荐国内用户)
- 检查网络代理设置
- 确认
ANTHROPIC_BASE_URL配置正确
Q: 如何设置代理?
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
推荐配置
编辑器美化(可选)
安装 Nerd Font 字体:
推荐使用 JetBrainsMono Nerd Font,支持 Claude Code 中的图标显示:
# Windows
winget install JetBrains.JetBrainsMono
配置 Windows Terminal 使用该字体:
- 打开 Windows Terminal 设置
- 选择 PowerShell 配置文件
- 外观 → 字体 → 选择 JetBrainsMono NF
推荐的 MCP 服务器
{
"hasCompletedOnboarding": true,
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "$HOME"]
},
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
},
"fetch": {
"command": "npx",
"args": ["-y", "@kazuph/mcp-fetch"]
}
}
}
下一步
- Claude Code 使用教程 - 深入学习 Claude Code 的使用技巧
- MCP 服务器配置指南 - 扩展 Claude Code 能力
- 一键安装脚本说明 - 了解安装脚本详情