Skip to content

安装与配置

本节介绍如何安装Claude Code并完成初始配置。

系统要求

要求说明
Node.js18.0或更高版本
账号Claude Pro订阅或Anthropic API访问权限
Git推荐安装(Windows必需)

安装方式

方式一:Native Install(推荐)

bash
# macOS / Linux / WSL
curl -fsSL https://claude.ai/install.sh | sh
powershell
# Windows PowerShell
irm https://claude.ai/install.ps1 | iex
cmd
# Windows CMD
curl -fsSL https://claude.ai/install.bat | cmd

Windows用户

Windows需要先安装 Git for Windows

方式二:Homebrew(macOS)

bash
brew install claude

方式三:WinGet(Windows)

powershell
winget install claude

方式四:npm

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

验证安装

bash
claude --version

登录认证

首次登录

bash
claude

首次运行会自动打开浏览器进行Anthropic账号登录:

╭────────────────────────────────────────╮
│  Welcome to Claude Code                 │
│  Opening browser for authentication...  │
╭────────────────────────────────────────╯

登录方式

方式说明
Claude Pro个人订阅用户,每月$20
Anthropic API使用API Key,按使用量计费
企业账号企业订阅用户

使用API Key

如果你有Anthropic API访问权限:

bash
# 设置环境变量
export ANTHROPIC_API_KEY=your-api-key

# 或在配置文件中设置
claude config set apiKey your-api-key

使用第三方提供商

Claude Code支持第三方API提供商:

bash
# 使用OpenRouter
export ANTHROPIC_BASE_URL=https://openrouter.ai/api
export ANTHROPIC_API_KEY=your-openrouter-key

# 使用AWS Bedrock
export CLAUDE_CODE_USE_BEDROCK=1

配置文件

配置文件位置

文件位置作用域
~/.claude/settings.json用户主目录全局配置
.claude/settings.json项目目录项目配置
.claude.json项目目录MCP服务器配置

全局配置示例

json
// ~/.claude/settings.json
{
  "defaultModel": "claude-sonnet-4-20250514",
  "autoAcceptPermissions": false,
  "maxOutputTokens": 4096,
  "theme": "dark"
}

项目配置示例

json
// .claude/settings.json
{
  "projectName": "my-project",
  "testCommand": "npm test",
  "lintCommand": "npm run lint",
  "buildCommand": "npm run build"
}

查看和修改配置

bash
# 查看所有配置
claude config list

# 设置配置项
claude config set defaultModel claude-sonnet-4-20250514

# 获取配置项
claude config get defaultModel

更新

bash
# 使用安装脚本更新
curl -fsSL https://claude.ai/install.sh | sh

# 使用npm更新
npm update -g @anthropic-ai/claude-code

# 使用Homebrew更新
brew upgrade claude

卸载

bash
# npm卸载
npm uninstall -g @anthropic-ai/claude-code

# Homebrew卸载
brew uninstall claude

# 手动删除
rm -rf ~/.claude

下一步

安装完成后,继续学习 基本使用方法