Claude Code 是 Anthropic 公司推出的一款 终端 AI 编程助手,基于 Claude 大语言模型构建。它可以直接在你的命令行中运行,理解你的代码库、编辑文件、执行终端命令,并自主处理完整的开发工作流。
Claude Code 基于 Node.js 运行。如果尚未安装,推荐使用 nvm(Node Version Manager)进行管理:
# 安装 nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash # 重新加载 shell 配置 source ~/.bashrc # 或 source ~/.zshrc # 安装 Node.js LTS 版本 nvm install --lts # 验证安装 node --version # 应显示 v20.x 或更高 npm --version # 应显示 v9.x 或更高
提示:如果你的发行版软件源中的 Node.js 版本较老,不推荐直接使用 `apt install nodejs`,请使用 nvm 或 NodeSource 官方源。
通过 npm 全局安装:
npm install -g @anthropic-ai/claude-code
安装完成后验证:
claude --version
Claude Code 支持多种认证方式:
# 方式一:API Key 认证(推荐,适合自动化) export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx" claude auth status # 方式二:控制台 API 认证 claude auth login --console # 方式三:企业 SSO 认证 claude auth login --sso # 方式四:浏览器 OAuth(Pro/Max 用户) claude # 首次运行会引导你完成浏览器登录
注意:使用 API Key 时,建议将其添加到 `~/.bashrc` 或 `~/.zshrc` 中以便持久化:
```bash
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"' >> ~/.bashrc
source ~/.bashrc
```
# 检查安装和自动更新器状态 claude doctor # 查看认证状态 claude auth status
直接在项目目录中运行 `claude`,进入交互式终端界面:
cd /path/to/your/project claude
进入后会看到欢迎界面,你可以直接输入自然语言指令,例如:
> 帮我重构 src/auth.py 中的认证逻辑,使用 JWT 替代 Session > 为这个函数添加单元测试 > 检查代码中的安全漏洞
适合脚本和自动化场景:
# 分析代码问题 claude -p "分析 src/ 目录下的所有 Python 文件,列出潜在的安全问题" # 管道输入 cat src/main.py | claude -p "解释这段代码的功能" # 限制执行轮次(防止无限循环) claude -p "修复 bug" --max-turns 10
在项目根目录创建 `CLAUDE.md`,让 Claude 记住项目规范:
# Project: 我的 API 项目 ## 架构 - FastAPI 后端 + SQLAlchemy ORM - PostgreSQL 数据库,Redis 缓存 - pytest 测试,覆盖率目标 90% ## 关键命令 - `make test` — 运行完整测试套件 - `make lint` — 代码检查(ruff + mypy) - `make dev` — 启动开发服务器 ## 编码规范 - 所有公开函数必须添加类型提示 - 使用 Google 风格文档字符串 - YAML 使用 2 空格缩进,Python 使用 4 空格 - 禁止使用通配符导入
通过 `--allowedTools` 限制可用工具:
# 只读模式(仅查看文件,不修改) claude -p "审查代码" --allowedTools "Read" # 允许读取和编辑 claude -p "修复 bug" --allowedTools "Read,Edit" # 允许所有操作(谨慎使用) claude --dangerously-skip-permissions
# 使用 Sonnet(平衡性能与速度) claude --model sonnet # 使用 Haiku(快速、经济,适合简单任务) claude --model haiku # 使用 Opus(最强推理,适合复杂任务) claude --model opus
MCP(Model Context Protocol)允许 Claude 连接外部服务:
# 添加 GitHub 集成 claude mcp add github -- npx @modelcontextprotocol/server-github # 添加 PostgreSQL 数据库访问 claude mcp add postgres -- npx @anthropic-ai/server-postgres --connection-string "postgresql://localhost/mydb" # 查看已配置的 MCP 服务器 claude mcp list
claude -p "列出 src/ 下所有函数" \ --output-format json \ --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}'
在隔离的工作目录中工作:
# 创建隔离的 git worktree claude -w feature-auth --tmux
创建 `.claude/agents/security-reviewer.md`:
--- name: security-reviewer description: 安全代码审查专家 model: opus tools: [Read, Bash] --- 你是一位资深安全工程师,负责审查代码中的安全问题: - SQL 注入、XSS、命令注入漏洞 - 认证/授权缺陷 - 代码中的密钥泄露 - 不安全的反序列化
使用时输入:`@security-reviewer 审查 auth 模块`
在 `.claude/settings.json` 中配置自动化钩子:
{ "hooks": { "PostToolUse": [{ "matcher": "Write(*.py)", "hooks": [{"type": "command", "command": "ruff check --fix $CLAUDE_FILE_PATHS"}] }], "PreToolUse": [{ "matcher": "Bash", "hooks": [{"type": "command", "command": "if echo \"$CLAUDE_TOOL_INPUT\" | grep -q 'rm -rf'; then echo '已拦截危险命令!' && exit 2; fi"}] }] } }
# 更新到最新版本 claude update # 或 claude upgrade # 查看当前版本 claude --version
如果使用 npm 直接更新:
npm update -g @anthropic-ai/claude-code
# 使用 --prefix 指定用户级安装目录 npm install -g @anthropic-ai/claude-code --prefix=~/.local # 或将 npm 全局目录改为用户目录 mkdir -p ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc npm install -g @anthropic-ai/claude-code
# 确认 npm 全局 bin 目录在 PATH 中 echo $PATH npm bin -g # 如果不在 PATH 中,添加到 .bashrc echo 'export PATH=$(npm bin -g):$PATH' >> ~/.bashrc source ~/.bashrc
# 检查认证状态 claude auth status # 确认 API Key 正确 echo $ANTHROPIC_API_KEY # 检查网络连接 curl -I https://api.anthropic.com
# 在会话中使用 /compact 压缩上下文 /compact # 或者清除历史重新开始 /clear
最后更新:2026年4月
>
评论区 暂无评论,快来抢沙发