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