如何在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函数,判断质数
❯ 
相关推荐
且从容.16 小时前
Ubuntu安装flume
linux·ubuntu·flume
承渊政道17 小时前
Linux网络学习【Reactor反应堆模式】
linux·网络·学习·ubuntu·reactor
且从容.20 小时前
ubuntu安装flink
linux·ubuntu·flink
Linux-lucky1 天前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
禁默1 天前
启元 Q1 二次开发实践:Ubuntu 部署与仿真验证,从 PrimeConsole 到 ROS 2 Humble + RViz
linux·运维·ubuntu
skywalk81631 天前
把 去也网站 从 Ubuntu 搬到 FreeBSD:一次 Service Jail + nginx 的部署实录
linux·运维·nginx·ubuntu·freebsd
David猪大卫2 天前
【Linux修炼】环境变量、进程地址空间
linux·c语言·经验分享·笔记·学习·ubuntu·centos
且从容.3 天前
Ubuntu 安装 Docker
linux·ubuntu·docker
承渊政道4 天前
Linux网络学习【深入理解多路转接select、poll的代码改写】
linux·ubuntu·select·多路转接·poll
yz_aiks4 天前
Postman 自动给 Claude Code、Codex、Cursor 装插件怎么查和卸载:本机实测 5 处痕迹(Postman 12.34)
安全·postman·ai编程·开发工具·claude code