如何在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函数,判断质数
❯ 
相关推荐
hjjdebug18 小时前
ubuntu系统 usbmouse 驱动代码分析
linux·ubuntu·usbmouse
神秘剑客_CN18 小时前
ubuntu26.04音频转srt字幕
ubuntu·conda·whisperx
windawdaysss18 小时前
使用VMware Workstation Pro安装Ubuntu虚拟机教程
linux·运维·ubuntu
xiaobobo333018 小时前
ubuntu14.04搭建Samba服务器实现文件共享
ubuntu·samba·共享文件夹
LN花开富贵1 天前
Ubuntu aarch64 架构安装 NoMachine 远程控制 避坑与实战
linux·运维·笔记·学习·ubuntu·嵌入式
取经蜗牛1 天前
Windows 11 WSL + Ubuntu 24.04 安装指南
linux·windows·ubuntu
嘛也学不会1 天前
claude code 出现Claude Code Web Fetch问题
claude code·claude code web fetch
bush42 天前
linux开发板连接virtualbox虚拟机ubuntu的usb网卡,访问外网方法。
linux·运维·ubuntu
大明者省2 天前
完整 Ubuntu 服务器 XFCE 桌面 + XRDP 远程桌面 部署使用全流程
运维·服务器·ubuntu
Tranquil_ovo2 天前
【AI】【Claude Code】 基本使用
claude code