当前位置:首页>Linux>在 Windows 和 Linux 上跑通 Claude Code(附国内可用配置)

在 Windows 和 Linux 上跑通 Claude Code(附国内可用配置)

  • 2026-10-21 13:29:05
在 Windows 和 Linux 上跑通 Claude Code(附国内可用配置)

你听说过能“自己写自己”的 AI 代码工具吗?  

Claude Code 最近引起了不少开发者的兴趣,它号称有 90% 的代码是它自己生成的,背后的技术选型也几乎全部落在 Claude 模型最熟悉的领域里,这让它在实际使用时格外顺手。

在这篇文章里,我会先简单梳理它的技术栈思路,然后分别演示在 Linux 和 Windows 下的安装与配置步骤。如果你已经清楚原理,只想最快地把环境搭起来,可以直接跳到文末的 Linux 命令行省流版。

1 技术底座:分布内的模型

Claude Code 的技术选型:

  • 核心语言:TypeScript
  • 终端 UI:React + Ink 框架
  • 布局系统:Meta 开源的 Yoga
  • 构建打包:Bun

这些技术都在 AI 的“分布内”。“分布内”指的是,模型在训练阶段已经见过海量这类语言的代码,处理起来很轻松。比如 TypeScript 和 React 就是大模型很擅长的技术栈。如果选择一个冷门的语言或架构,模型需要边学边用,效果会打折扣。

用 Claude 擅长的技术栈写 Claude Code,然后用 Claude Code 写更多 Claude Code。自己写自己,就形成了今天的 Claude Code。

而在架构层面,它选择了本地运行的最底层方式。没有 Docker 容器,没有云端沙箱,直接在你的文件系统中读写。

Claude Code 本质上是一个运行在本地的 Node.js 应用程序。

2 安装逻辑

Claude Code 及配套的 Claude Code Router 工具均基于 Node.js 开发。无论是 Windows 还是 Linux 用户,都是围绕 Node.js 安装相关库。

在安装 Claude Code 之前,需要确认是否安装 nvm, node。

  • nvm:为了避免权限和版本冲突,推荐使用 nvm 管理 Node.js。
  • Node.js & npm:安装 Node.js 时,npm 会作为核心组件被自动捆绑安装。
  • Claude Code Router:国内使用必装,负责解决网络连接问题。

3 安装

3.1 Linux 命令行详解

1. 准备 Node.js 运行环境

  • nvm
     是一个管理工具,它允许在同一台机器上轻松安装、切换和使用多个版本的 Node.js。相比于直接安装系统包,使用 nvm 可以避免权限和版本问题。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
  • 安装脚本通常会修改配置文件,执行以下命令可立即在当前会话生效。
export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
  • 安装 Node.js,默认安装最新稳定版
nvm --versionnvm install node # 安装最新版本node --version

2. 安装 Claude Code 核心工具

  • 安装 Claude Code 客户端
npm install -g @anthropic-ai/claude-code
  • 安装 Claude Code Router
npm install -g @musistudio/claude-code-router

-g  参数表示全局安装,这是一个系统级命令,以便可以在任何终端路径下直接运行 ` claude

3.2 window 系统

准备 Bash 环境

Claude Code 在执行底层文件操作和命令时依赖 Bash 环境。Windows 自带的 CMD 或 PowerShell 并不够用,因此需要安装 Git,它提供了原生的 bash.exe。

  • 从 Git 官网 下载并安装 Git
  • 配置环境变量,告诉 Claude Code 你的 bash.exe 在哪里:把路径替换为你的 bash.exe 实际安装位置
set CLAUDE_CODE_GIT_BASH_PATH=D:\soft_location\git\bin\bash.exe

安装 Node. Js 与 npm

访问 Node.js 官网,下载 LTS 版本安装包并安装。会自动配置 Node. Js 和 npm 的路径。

打开 CMD 验证安装

node --versionnpm --version

安装 claude-code

npm install -g @anthropic-ai/claude-code

安装 claude-code-router 

npm install -g @musistudio/claude-code-router

4 配置路由(连接国内服务)

安装工具后,直接运行 Claude 命令会报错。须配置并启动 claude-code-router。

4.1 配置 claude-code-router

创建并编辑配置文件:在文件中填入国内服务商提供的 API 地址和密钥。

mkdir -p ~/.claude-code-routervim ~/.claude-code-router/config.json

配置文件 Demo

以下配置演示了如何接入 OpenRouter 和 DeepSeek。请将 api_key 替换为你自己的密钥。

{  "PORT": 3456,  "PROXY_URL": "http://127.0.0.1:xxx",  "APIKEY": "claude-code-test",  "API_TIMEOUT_MS": 600000,  "NON_INTERACTIVE_MODE": false,  "Providers": [    {      "name": "openrouter",      "api_base_url": "https://openrouter.ai/api/v1/chat/completions",      "api_key": "sk-or-v1-xxxx",      "models": [        "anthropic/claude-sonnet-4",        "google/gemini-2.0-flash-exp:free"      ],      "transformer": {        "use": ["openrouter"]      }    },    {      "name": "deepseek",      "api_base_url": "https://api.deepseek.com/chat/completions",      "api_key": "sk-xxxx",      "models": ["deepseek-chat", "deepseek-reasoner"],      "transformer": {        "use": ["deepseek"],        "deepseek-chat": {          "use": ["tooluse"]        }      }    }  ],  "Router": {    "default": "deepseek,deepseek-chat",    "think": "openrouter,anthropic/claude-sonnet-4",    "longContextThreshold": 60000,    "longContext": "openrouter,google/gemini-2.0-flash-exp:free",    "webSearch": "deepseek,deepseek-chat"   }  }

详细配置项可参考文末 claude-code-router 官方教程

4.2 配置 Claude Code 客户端

为了防止官方客户端在启动时卡在引导流程(如登录 Anthropic 官网),需手动指定引导完成。

vim /root/.claude.json

添加这行 {"hasCompletedOnboarding": true}

为什么添加这行,可参见:https://mp.weixin.qq.com/s/5FJIvh8FPIzHPqI4RD2vqQ

4.3 启动服务

要使用 ccr 命令启动,它会启动 Router 服务 + Claude Code。

ccr stop  # 先停止可能正在运行的旧服务进程ccr code #  启动服务

在对话框输入问题,如果可以正常返回,说明已经连接成功,可以正常使用配置的模型。类似下图的模型调用和返回界面。

5 Linux 命令行省流版

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bashexport NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"nvm --versionnvm install nodenode --versionnpm install -g @anthropic-ai/claude-codenpm install -g @musistudio/claude-code-routermkdir -p ~/.claude-code-routervim ~/.claude-code-router/config.jsonvim /root/.claude.jsonccr stopccr code

6 后记

这篇先解决让工具稳定在你电脑上跑起来的问题。如果你也在用 Claude Code 做有趣的项目,或者遇到了其他配置问题,欢迎一起交流。

7 参考链接

  • Claude Code 官方文档:https://www.google.com/url?sa=E&q=https%3A%2F%2Fcode.claude.com%2Fdocs%2Fzh-CN%2F
  • Claude Code Github项目:https://github.com/anthropics/claude-code/blob/main/README.md
  • OpenRouter Key 申请教程:OpenRouter
  • 跳过引导的原理解析:解决 Claude-Code 报错 ERR_BAD_REQUEST

最新文章

随机文章