第 02 期 | 环境搭建与授权:Windows/Mac/Linux 极速安装指南
🎯 学习目标
学完本期你将能够:
- 在 Mac / Windows / Linux 上成功安装 Claude Code
- 完成终端认证授权
- 在第一个项目中启动 Claude Code 会话
📖 核心概念讲解
2.1 安装方式总览
Claude Code 提供多种安装方式,适配不同操作系统和场景:
| 安装方式 | 平台 | 适用场景 |
|---|---|---|
| Native Install(推荐) | Mac / Linux / Windows | 自动更新,最稳定 |
| Homebrew | Mac | Homebrew 用户偏好 |
| WinGet | Windows | Windows 包管理器 |
| VS Code 扩展 | 全平台 | IDE 内集成 |
| Desktop App | Mac / Windows | GUI 界面偏好 |
| Web 版 | 全平台 | 无需安装 |
2.2 订阅计划
使用 Claude Code 需要付费订阅:
| 计划 | 价格 | 适用人群 |
|---|---|---|
| Pro | $20/月 | 日常开发,轻度使用 |
| Max 5x | $100/月 | 每日密集开发 |
| Max 20x | $200/月 | 专业团队/重度用户 |
| API (Console) | 按量计费 | 集成到自动化管道 |
💻 代码演示:三步安装
Mac / Linux 安装
# 步骤 1:一键安装(推荐方式)
curl -fsSL https://claude.ai/install.sh | bash
# 步骤 2:验证安装
claude --version
# 输出: Claude Code v2.3.0
# 步骤 3:认证登录
claude auth login
Windows 安装(PowerShell)
# 步骤 1:一键安装
irm https://claude.ai/install.ps1 | iex
# 步骤 2:验证
claude --version
# 步骤 3:认证
claude auth login
Homebrew 安装(Mac 替代方案)
# 安装
brew install --cask claude-code
# 升级
brew upgrade claude-code
💻 模拟 Claude TUI 交互:首次启动
$ cd ~/my-project
$ claude
╭──────────────────────────────────────────────────────╮
│ │
│ Welcome to Claude Code v2.3.0! │
│ │
│ You need to sign in to continue. │
│ Press Enter to open the browser for authentication │
│ │
╰──────────────────────────────────────────────────────╯
↵ Enter
✓ Browser opened. Waiting for authentication...
✓ Successfully authenticated as [email protected]
✓ Plan: Max (5x)
╭──────────────────────────────────────────────────────╮
│ │
│ Claude Code is ready! │
│ │
│ 📁 Working directory: ~/my-project │
│ 📄 Found: package.json, tsconfig.json │
│ 📝 No CLAUDE.md found (run /init to create one) │
│ │
│ What can I help you with? │
│ │
│ Tips: │
│ • Press Shift+Tab to switch modes │
│ • Type /help for available commands │
│ • Type /init to set up project memory │
│ │
╰──────────────────────────────────────────────────────╯
> /help
╭─ Available Commands ────────────────────────────────╮
│ │
│ /help 显示帮助信息 │
│ /init 初始化 CLAUDE.md 项目记忆 │
│ /clear 清空当前上下文 │
│ /compact 压缩长会话为摘要 │
│ /memory 查看/编辑自动记忆 │
│ /permissions 管理工具权限 │
│ /agents 查看可用子代理 │
│ /schedule 创建定时任务 │
│ /desktop 切换到桌面应用 │
│ /loop 重复执行 prompt │
│ /teleport 从 Web 端拉取会话 │
│ │
╰──────────────────────────────────────────────────────╯
💻 代码演示:认证状态检查
# 检查当前认证状态
claude auth status
# 输出示例:
# ✓ Authenticated
# Account: [email protected]
# Plan: Max (5x usage)
# Organization: Personal
# 登出(切换账号时使用)
claude auth logout
# 使用 API Key 认证(CI/CD 场景)
claude auth login --console
🔧 涉及的 CLI 命令
| 命令 | 作用 | 官方文档 |
|---|---|---|
claude |
启动交互会话 | CLI Reference |
claude auth login |
登录认证 | 支持 --email, --sso, --console 参数 |
claude auth status |
查看认证状态 | |
claude auth logout |
登出 | |
claude --version |
查看版本 | |
claude update |
更新到最新版 |
⚠️ 常见问题排查
问题 1:安装后 claude 命令找不到
# Mac/Linux:将 Claude Code 加入 PATH
export PATH="$HOME/.claude/bin:$PATH"
# 永久生效
echo 'export PATH="$HOME/.claude/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
问题 2:认证窗口无法打开
# 使用 Console 方式手动认证
claude auth login --console
# 它会输出一个 URL,手动在浏览器中打开
问题 3:Windows PowerShell 报 && 错误
# Windows CMD 和 PowerShell 语法不同
# 如果出错,使用 Git Bash 或 WSL 来运行安装脚本
📝 本期要点回顾
- 推荐使用 Native Install 方式安装(自动更新)
- 首次使用需要在浏览器中完成 OAuth 认证
- 使用
claude auth status随时检查认证状态 - CI/CD 环境使用
claude auth login --console进行 API Key 认证 - 记住常用斜杠命令:
/help,/init,/clear,/compact