Skip to content

About

一条命令,完成 Claude Code 环境搭建,支持Linux/Mac/Windows

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

27 Commits

Folders and files

Repository files navigation

Anthropic

Claude Code Bootstrap

一条命令,完成 Claude Code 环境搭建

Platform Shell Node.js License


一行命令安装

Linux / macOS

curl -fsSL https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/install.sh | bash

Windows

iex ((irm https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/install.ps1).TrimStart([char]0xFEFF))

Linux / macOS:管道方式无法修改父 shell 环境,完成后 source ~/.bashrc 或重新打开终端。

Windows:命令会移除 Windows PowerShell 5.1 从网络脚本保留的 UTF-8 BOM;若提示 "无法加载",先运行 Set-ExecutionPolicy -Scope CurrentUser RemoteSigned,或使用 powershell -ExecutionPolicy Bypass 启动。


一行命令卸载

Linux / macOS

curl -fsSL https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/uninstall.sh | bash

Windows

iex ((irm https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/uninstall.ps1).TrimStart([char]0xFEFF))

卸载菜单提供以下模式:

  • 仅撤销配置:清理 bootstrap 环境文件、wrapper、启动配置块和仍由脚本管理的 settings.json 字段,保留 Claude Code npm 包。
  • 恢复安装前状态:在安装状态可确认且当前值未被修改时,恢复原 npm 版本、npm prefix 和默认 Node 设置。
  • 彻底卸载 Claude Code:同时卸载 npm 包,默认保留 Node.js、nvm/fnm 和 Claude 用户数据。
  • 高级清理:可继续清理确认由脚本安装的运行时,或删除全部 ~/.claude 用户数据;危险操作需要额外输入确认文本。

未知来源的文件、用户修改过的配置以及其他渠道安装的 claude 命令不会被自动删除。运行时清理还会比对安装完成时的目录内容哈希;发现新增 Node 版本、全局 npm 包、目录修改或无法确认的文件时会保留运行时和状态文件。Node 安装阶段中断时也会保留 pending 状态和既有配置,供重试或诊断。命令行执行可使用 --preview、--config-only、--restore 或 --full;--runtime、--purge-data 必须与 --full 一起使用并保留交互确认。PowerShell 文件参数对应为 -Preview、-ConfigOnly、-Restore、-Full、-Runtime 和 -PurgeData。


这是什么?

在一台没有任何开发环境的机器上,运行上面那一行命令即可自动完成:

  • ✅ 系统依赖安装(curl、git、tar、xz / winget)
  • ✅ Node.js 22 环境搭建(nvm / fnm)
  • ✅ Claude Code 全局安装(npm)
  • ✅ API 网关配置(BASE_URL + API Key / Auth Token)
  • ✅ 动态模型列表获取
  • ✅ Shell / PowerShell 环境自动注入
  • ✅ Claude Code settings.json 同步

脚本会自动识别操作系统和包管理器(apt、dnf、yum、apk、pacman、zypper、winget),无需手动干预。


带配置的一行安装

如果你已经知道网关地址,可以跳过交互式输入:

Linux / macOS

BOOTSTRAP_BASE_URL="https://api.example.com" bash -c "$(curl -fsSL https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/install.sh)"

Windows

$env:BOOTSTRAP_BASE_URL="https://api.example.com"; iex ((irm https://raw.githubusercontent.com/4iKZ/claude-bootstrap/main/install.ps1).TrimStart([char]0xFEFF))

配置选项

环境变量 默认值 说明
BOOTSTRAP_BASE_URL — API 网关地址
DEFAULT_AUTH_MODE auth_token 认证模式:auth_token 或 api_key
CREATE_CLAUDE_WRAPPER 1 是否创建 wrapper 脚本
ENABLE_GATEWAY_MODEL_DISCOVERY 1 是否从网关动态拉取模型列表
CLAUDE_CODE_SUBPROCESS_ENV_SCRUB_DEFAULT 0 子进程环境清理策略
REQUIRED_NODE_MAJOR 22 目标 Node.js 主版本号
NODE_DIST_MIRROR_FALLBACK npmmirror Node.js 镜像 官方 Node.js 源不可用时的回退地址
NVM_NODEJS_ORG_MIRROR — Linux/macOS 自定义 Node.js 下载源
FNM_NODE_DIST_MIRROR — Windows 自定义 Node.js 下载源

Claude Code npm 包固定为 @anthropic-ai/claude-code@2.1.142。脚本会写入 DISABLE_UPDATES=1,避免固定版本被 Claude Code 自更新覆盖。


执行流程

Linux / macOS (install.sh)

检测平台(Linux / macOS + 架构)
        │
        ▼
检查内存(建议 ≥ 4 GB)
        │
        ▼
检测 sudo 权限
        │
        ▼
安装基础依赖(curl、git、tar、xz)
  ├─ Debian/Ubuntu → apt-get
  ├─ Fedora/RHEL   → dnf / yum
  ├─ Alpine        → apk
  ├─ Arch          → pacman
  └─ openSUSE      → zypper
        │
        ▼
安装 Node.js 22(通过 nvm)
  ├─ nvm 使用官方 script 模式,不执行 Git Clone
  └─ Node.js 官方源不可用时回退 npmmirror
        │
        ▼
npm install -g @anthropic-ai/claude-code@2.1.142
        │
        ▼
交互式配置 → 写入配置 → 连通性验证 → 打印摘要

Windows (install.ps1)

检测平台(Windows + 架构)
        │
        ▼
检查内存(建议 ≥ 4 GB)
        │
        ▼
检测管理员权限
        │
        ▼
安装基础依赖(git,通过 winget)
        │
        ▼
安装 fnm + Node.js 22
  └─ Node.js 官方源不可用时回退 npmmirror
        │
        ▼
npm install -g @anthropic-ai/claude-code@2.1.142
        │
        ▼
交互式配置 → 写入配置 → 连通性验证 → 打印摘要

生成的文件

路径 说明
~/.claude-code/env 环境变量文件(Linux/macOS)
~/.claude-code/bin/claude 包装脚本(Linux/macOS)
~/.claude-code/install-state.json 无密钥安装状态(Linux/macOS)
~/.claude-bootstrap/env.ps1 环境变量文件(Windows PowerShell)
~/.claude-bootstrap/env.cmd 环境变量文件(Windows cmd)
~/.claude-bootstrap/bin/claude.cmd 包装脚本(Windows)
~/.claude-bootstrap/install-state.json 无密钥安装状态(Windows)
~/.claude/settings.json Claude Code 官方配置

支持的平台

操作系统 架构 脚本 状态
Ubuntu 20.04+ x64 / ARM64 install.sh ✅
Debian 11+ x64 / ARM64 install.sh ✅
Fedora 38+ x64 / ARM64 install.sh ✅
RHEL 7/8/9 x64 install.sh ✅
Alpine 3.17+ x64 / ARM64 install.sh ✅(需先装 bash)
Arch Linux x64 install.sh ✅
openSUSE x64 install.sh ✅
macOS 12+ x64 / ARM64 install.sh ✅
Windows 10+ x64 / ARM64 install.ps1 ✅
Windows Server 2019+ x64 install.ps1 ✅

故障排查

Alpine Linux:command not found: bash
apk add bash
nvm install 失败:tar 无法解压 .tar.xz

脚本已自动处理,若跳过了包安装阶段:

  • Debian/Ubuntu: sudo apt install xz-utils
  • Fedora/RHEL: sudo dnf install xz
  • Alpine: sudo apk add xz
nvm 安装时无法连接 github.com

脚本使用 nvm 官方 METHOD=script 模式,通过 raw.githubusercontent.com 下载 nvm 文件,不再执行 git clone github.com/nvm-sh/nvm.git。安装 Node.js 时会先检测官方源,连接失败则自动回退 npmmirror;也可以通过 NVM_NODEJS_ORG_MIRROR 或 FNM_NODE_DIST_MIRROR 显式指定下载源。

Windows:运行脚本提示 "无法加载文件"

PowerShell 默认禁止执行脚本,使用 -ExecutionPolicy Bypass 运行:

powershell -ExecutionPolicy Bypass -File install.ps1
Windows:winget 找不到

Windows 10 早期版本可能未安装 winget。从 Microsoft Store 安装"应用安装程序"(App Installer),或手动安装 git 和 fnm 后重新运行。

Claude Code 启动时提示 bubblewrap 错误

脚本默认设置 CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=0,避免缺少 bubblewrap 时启动失败。

重新配置

直接重新运行脚本。检测到已有配置后,可选择保留现有配置、仅切换模型或重新配置全部:

bash install.sh        # Linux / macOS
powershell -ExecutionPolicy Bypass -File install.ps1   # Windows

安全说明

  • API Key / Auth Token 写入文件时仅所有者可读写
  • Linux/macOS 的 ~/.claude-code/ 和 Windows 的 ~/.claude-bootstrap/ 用于保存 bootstrap 文件
  • install-state.json 只保存密钥哈希,不保存 API Key / Auth Token 明文
  • 脚本不通过命令行参数传递密钥,全部采用交互式输入(隐藏回显)或环境变量
  • 建议生产环境中通过环境变量预注密钥,避免交互式输入被日志记录

License

MIT


Built with ❤️ for Claude Code users

About

一条命令,完成 Claude Code 环境搭建,支持Linux/Mac/Windows

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages