如何在ubuntu20.04系统配置Claude code使用中国大模型

1. 安装claude code

  1. 安装nvm

  2. 通过nvm 安装node.js

bash 复制代码
$ nvm --version
0.39.3

$ npm --version
11.12.1

$ node --v
v22.14.0
  1. 安装 claude-code
bash 复制代码
npm install -g @anthropic-ai/claude-code

2. 安装litellm

  1. 安装python3.10

  2. 创建python虚拟环境ccx

  3. 安装litellm

bash 复制代码
#$ mkvirtualenv ccx -p /usr/bin/python3.10 
(ccx) $ pip install litellm 
(ccx) $ pip install 'litellm[proxy]'
(ccx) $ pip install httpx[socks]

3. 配置claude code

3.1 启用litellm

在~/.zshrc文件中添加以下内容:

bash 复制代码
export ZHIPU_API_KEY=<your_api_key>

运行source ~/.zshrc命令,使环境变量生效。

创建 litellm_config.yaml(与下文启动命令文件名一致),并添加以下内容。Claude Code 会通过 Anthropic 接口请求诸如 claude-sonnet-4-6 等官方模型名,因此需要一条 model_name: "*" 的通配路由,把这些名称全部转到实际使用的国内模型。

yaml 复制代码
model_list:
#   - model_name: qwen
#     litellm_params:
#       model: dashscope/qwen-plus

#   - model_name: kimi
#     litellm_params:
#       model: moonshot/moonshot-v1-8k

  - model_name: "*"
    litellm_params:
      model: zai/glm-5.1
      api_key: os.environ/ZHIPU_API_KEY

  - model_name: glm
    litellm_params:
      model: zai/glm-5.1
      api_key: os.environ/ZHIPU_API_KEY

litellm_settings:
  drop_params: true

Claude Code 会带上 Anthropic 专有字段(如 context_management),转给 GLM 时需丢弃;drop_params: true 即由 LiteLLM 自动去掉后端不支持的参数。

运行 LiteLLM 代理:

bash 复制代码
(ccx) ➜  ccx litellm --config litellm_config.yaml --port 4000
INFO:     Started server process [379725]
INFO:     Waiting for application startup.

   ██╗     ██╗████████╗███████╗██╗     ██╗     ███╗   ███╗
   ██║     ██║╚══██╔══╝██╔════╝██║     ██║     ████╗ ████║
   ██║     ██║   ██║   █████╗  ██║     ██║     ██╔████╔██║
   ██║     ██║   ██║   ██╔══╝  ██║     ██║     ██║╚██╔╝██║
   ███████╗██║   ██║   ███████╗███████╗███████╗██║ ╚═╝ ██║
   ╚══════╝╚═╝   ╚═╝   ╚══════╝╚══════╝╚══════╝╚═╝     ╚═╝


#------------------------------------------------------------#
#                                                            #
#            'This product would be better if...'             #
#        https://github.com/BerriAI/litellm/issues/new        #
#                                                            #
#------------------------------------------------------------#

 Thank you for using LiteLLM! - Krrish & Ishaan



Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new


LiteLLM: Proxy initialized with Config, Set models:
    qwen
    kimi
    glm
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:4000 (Press CTRL+C to quit)

3.2 配置claude code

  1. 在~/.zshrc文件中添加以下内容:
bash 复制代码
export ANTHROPIC_API_KEY=dummy
export ANTHROPIC_BASE_URL=http://127.0.0.1:4000
  1. 运行source ~/.zshrc命令,使环境变量生效。

  2. 运行claude命令,并输入以下内容:

bash 复制代码
$ claude
  • Choose the text style that looks best with your terminal
  • ANTHROPIC_API_KEY: sk-ant-...dummy 选择yes
  • Enter
  • yes
  1. 如果运行claude初始化配置有问题,则执行以下命令重新配置:
bash 复制代码
$ rm -rf ~/.claude*
$ claude

4. 测试claude code

  1. 运行claude命令,并输入以下内容:
bash 复制代码
$ claude
> 写一个python函数,判断质数
❯ 
相关推荐
yz_aiks3 天前
Claude Code Skills 团队怎么共享:.claude/skills 提交 git 还是建私有插件市场(2.1.299 实测 5 个坑)
人工智能·ai编程·开发工具·团队协作·claude code
木子欢儿5 天前
Ubuntu 26.04 LTS 安装Python 3.12
linux·运维·服务器·python·ubuntu
VIP_CQCRE5 天前
¥99起多模型编程套餐如何分工:Claude Code、Codex与OpenCode任务路由指南
ai编程·codex·claude code·opencode·acedatacloud
VIP_CQCRE5 天前
¥99起多模型编程套餐怎么控预算:Coding Plan缓存、Credits用量与续费指南
大模型·ai编程·codex·claude code·coding plan
2601_967979126 天前
Ubuntu部署tensorflow(CPU/GPU)方法
ubuntu·tensorflow·cpu·gpu·配置
Rabitebla7 天前
【Linux】文件篇(五):手撕简易 FILE——模拟实现 C 标准 I/O 缓冲机制
linux·服务器·c语言·笔记·学习·ubuntu
GoldenSpider.AI7 天前
讲透 Claude Code 的 Code Mode:原理、流程、用法与 24 轮实测
人工智能·ai编程·claude code
JonaHu7 天前
AceDataCloud Coding Plan 多模型编程:本地、CI 与团队仓库的 Token 隔离实战
ai编程·devops·codex·api安全·claude code
羲云网络7 天前
一份多模型编程套餐怎么控成本:AceDataCloud Coding Plan缓存、Credits与鉴权排错
api·ai编程·codex·claude code·acedatacloud
prince_zxill8 天前
把 17 个散文件装进一个文件夹:拆解 claude-plugins-official 的插件打包术(47,413 Star 实测)
python·github·agent·mcp·claude code