安装与环境配置
安装与环境配置
Section titled “安装与环境配置”装 Claude Code 比装一个 npm 包还快。这一页讲清楚三种装法、体检与升级、踩坑速查。
装之前先准备一件事
Section titled “装之前先准备一件事”你需要一个 Anthropic 账号。两种都行:
- Claude.ai 账号(Pro / Max / Team 订阅):按月付费,token 包在订阅里,官方推荐个人用户走这条。
- Claude Console 账号:API 预付,按 token 计费,适合脚本/CI 和企业。
没有账号?去 claude.ai 或 console.anthropic.com 注册一个,回来继续。
方式一:原生安装(官方推荐)
Section titled “方式一:原生安装(官方推荐)”这是官方文档首推的方式,二进制直接装到系统,不依赖 Node.js。
# macOS / Linux / WSLcurl -fsSL https://claude.ai/install.sh | bash# Windows PowerShellirm https://claude.ai/install.ps1 | iex:: Windows CMD(先装 curl,或用 PowerShell 走上面的方式)curl -fsSL https://claude.ai/install.ps1 -o install.ps1 && powershell -ExecutionPolicy Bypass -File install.ps1原生安装的好处:启动快、不抢你 Node 版本、官方维护、自动升级顺滑。
方式二:NPM 安装
Section titled “方式二:NPM 安装”# 需要 Node.js 18+npm install -g @anthropic-ai/claude-code适合已经被 Node 生态绑架的人,或者你团队有 npm 私服要走。注意:NPM 版本需要 Node.js 18+,老 Node 项目得先 nvm use 18。
方式三:Homebrew(macOS)
Section titled “方式三:Homebrew(macOS)”如果你已经装了 Homebrew,也可以走 brew。具体命令在不同版本有调整,以官方文档 docs.claude.com 当前的说明为准——Homebrew formula/cask 的命名偶尔会变,照官方一行抄最稳。
原生 vs NPM,怎么选
Section titled “原生 vs NPM,怎么选”| 维度 | 原生安装 | NPM 安装 |
|---|---|---|
| 依赖 | 无 | Node.js 18+ |
| 启动速度 | 快 | 稍慢(要走 Node) |
| 升级 | claude update |
npm update -g |
| 多版本 | claude install <version> |
nvm 切版本 |
| 推荐 | 大多数人 | Node 重度用户 |
简单说:没有特别理由就选原生。
claude doctor这条命令会打印当前版本、Node 版本、操作系统、配置文件路径、登录状态、有没有损坏的依赖。装完跑一遍,确认全绿。
claude update # 升到最新稳定版claude install stable # 显式装稳定版claude install 1.0.20 # 装指定版本(回退用)Claude Code 默认会自动检查更新,但你也可以手动跑。
验证:跑通第一条命令
Section titled “验证:跑通第一条命令”# 随便找个项目cd ~/projects/my-app
# 启动 Claude Codeclaude第一次启动会提示登录(下一节 账号与认证 详讲)。登录完,看到提示符 > 就说明你装好了。
退出按 Ctrl+D 或者输入 /exit。
装不上?常见问题速查
Section titled “装不上?常见问题速查”command not found: claude
Section titled “command not found: claude”PATH 没刷新。重开终端,或者:
source ~/.zshrc # macOS 默认 zshsource ~/.bashrc # Linux 默认 bashEACCES: permission denied(NPM 装法)
Section titled “EACCES: permission denied(NPM 装法)”npm 全局目录权限问题。别用 sudo 装——会埋一堆坑。正确做法是改 npm 全局路径:
mkdir ~/.npm-globalnpm config set prefix '~/.npm-global'echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrcsource ~/.zshrcnpm install -g @anthropic-ai/claude-codeWindows 安装脚本被杀毒拦
Section titled “Windows 安装脚本被杀毒拦”PowerShell 默认执行策略可能拦脚本。管理员开 PowerShell:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned再重新跑安装命令。
装完 claude doctor 报 Node 版本太低
Section titled “装完 claude doctor 报 Node 版本太低”node --version # 必须 >= 18不够就升 Node,或者改用原生安装绕开 Node。
企业网络代理拦了下载
Section titled “企业网络代理拦了下载”export HTTPS_PROXY=http://your-proxy:portcurl -fsSL https://claude.ai/install.sh | bashWSL 里启动慢
Section titled “WSL 里启动慢”WSL 文件系统跨 Windows/Linux 边界读写慢。项目放 WSL 文件系统里(~/projects),别放 /mnt/c/,能快好几倍。