Claude Code CLI 完整教程

从零开始学习安装、配置和使用 Claude Code 命令行工具

最后更新: 2026-04-26 作者: 云上开发组

安装 Claude Code CLI

1. 前置准备

在安装之前,你需要准备好以下内容:

准备工作
  • API 端点:https://ai.cloudplugins.cn
  • API 密钥:从平台获取你的 API Key
  • 命令行工具:
    • Mac/Linux:系统自带的 Terminal(终端)
    • Windows:PowerShell 或 WSL(Windows Subsystem for Linux)

2. 使用官方脚本安装(推荐)

根据你的系统选择对应的安装命令:

Windows 安装
# Windows PowerShell
irm https://claude.ai/install.ps1 | iex

# Windows CMD
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd

# WinGet
winget install Anthropic.ClaudeCode
macOS/Linux 安装
# macOS、Linux、WSL
curl -fsSL https://claude.ai/install.sh | bash

# Homebrew (macOS)
brew install --cask claude-code

安装完成后,验证是否成功:

验证安装
claude --version

# 输出示例:
# 2.1.81 (Claude Code)

3. 使用 npm 安装(不推荐)

官方已不再推荐 npm 安装方式,但如果需要:

npm 安装
# 先确认 Node.js 已安装
node --version

# 安装 Claude Code
npm install -g @anthropic-ai/claude-code

# 如果下载慢,使用国内镜像源
npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com
常见问题
  • npm command not found:需要安装 Node.js(前往 nodejs.org)
  • permission denied:Mac/Linux 在命令前加 sudo;Windows 以管理员身份运行 PowerShell
  • 安装速度慢:添加 --registry=https://registry.npmmirror.com 使用国内镜像

配置 API 访问

1. 创建配置文件

Claude Code 的配置文件位于 C:\Users\Administrator\.claude\settings.json。如果文件不存在,需要手动创建。

创建配置文件
# Windows PowerShell - 创建目录和文件
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.claude"
New-Item -ItemType File -Force -Path "$env:USERPROFILE\.claude\settings.json"

2. 配置 API 密钥和端点

编辑 settings.json 文件,添加以下内容:

settings.json 配置
{
  "env": {
    "ANTHROPIC_API_KEY": "your-api-key-here",
    "ANTHROPIC_BASE_URL": "https://ai.cloudplugins.cn"
  },
  "autoUpdatesChannel": "stable"
}
重要提示
  • your-api-key-here 替换为你的实际 API 密钥
  • API 端点已设置为:https://ai.cloudplugins.cn
  • 不要将此文件提交到 Git 仓库,避免泄露 API 密钥

更新与卸载

1. 更新 Claude Code

更新命令
# 方式一:使用内置命令(推荐)
claude update
# 或
claude install

# 方式二:通过包管理器更新
# macOS Homebrew
brew upgrade claude-code

# Windows WinGet
winget upgrade Anthropic.ClaudeCode

2. 卸载 Claude Code

卸载命令
# 官方脚本安装(Windows PowerShell)
Remove-Item -Path "$env:USERPROFILE\.local\bin\claude.exe" -Force
Remove-Item -Path "$env:USERPROFILE\.local\share\claude" -Recurse -Force

# WinGet 安装
winget uninstall Anthropic.ClaudeCode

# npm 安装
npm uninstall -g @anthropic-ai/claude-code

登录与启动

1. 启动 Claude Code

在项目目录中启动 Claude Code,这样它才能读取你的项目文件:

启动命令
# 切换到项目目录
cd C:\path\to\your\project

# 启动 Claude Code
claude
配置完成

只要 settings.json 中的 API 密钥和端点配置正确,Claude Code 启动后即可直接使用,无需执行 /login 命令。

基本使用

1. 理解项目

进入项目目录启动 Claude Code 后,可以先让它分析你的代码库:

项目分析
# 询问项目概况
what does this project do?

# 询问技术栈
what technologies does this project use?

# 询问文件夹结构
explain the folder structure

2. 命令行直接查询

命令行使用
# 执行一次性任务后退出
claude "fix the build error"

# 执行单次查询(适合脚本集成)
claude -p "explain this function"

# 继续最近一次对话
claude -c

# 查看版本号
claude --version

常见问题与故障排除

⚠️ Claude Code 无法连接到 Anthropic 服务

如果你在使用 npm 安装完 Claude Code 后,在命令行输入 claude 遇到如下错误:

错误信息示例:
Unable to connect to Anthropic services
Failed to connect to api.anthropic.com: ERR BAD REQUEST
Please check your internet connection and network settings.
Note: Claude Code might not be available in your country...
注意事项

如果你是首次配置并根据链接跳转到这一步,请直接按照下面的教程运行命令即可。

解决方案:修复连接错误

1. 打开命令行工具
按下键盘 Win + R 键,输入 cmd 后回车,打开命令行程序。

2. 运行以下修复命令

修复无法连接错误(Windows)
powershell -Command "$f='%USERPROFILE%\.claude.json';$j=Get-Content $f|ConvertFrom-Json;$j|Add-Member -NotePropertyName 'hasCompletedOnboarding' -NotePropertyValue $true -Force;$j|ConvertTo-Json|Set-Content $f"

3. 重启 Claude Code

重启 CLI
cd 你的项目路径
claude
成功标志

如果一切正常,Claude Code 将不再提示连接错误,并直接进入交互式对话模式。

其他常见问题

  • Q: 配置完 API 端点后仍然提示 API Key 无效?
    A: 请检查 ANTHROPIC_API_KEY 是否复制完整且没有多余空格,同时确认 API 密钥有效。
  • Q: Windows 上运行修复命令提示 "File not found"?
    A: 可以先运行一次 claude --version 让其自动生成配置文件,然后再执行修复命令。
  • Q: 如何确认 Claude Code 使用了自定义端点?
    A: 检查 settings.json 中的 ANTHROPIC_BASE_URL 是否设置为 https://ai.cloudplugins.cn