[TOC] #### 1. Claude Code --- Claude Code 是 Anthropic 公司推出的桌面端代码助手应用,它将强大的 Claude AI 模型与你的本地代码环境无缝集成,旨在成为程序员的智能伙伴。 简单说一句话:Claude Code = 在终端里用 AI 帮你写代码、改代码、理解代码 #### 2. 软件环境介绍 --- 本文使用的操作系统及软件介绍: | 名称 | 描述 | 官网地址 / 使用教程 | | ------------ | ------------ | ------------ | | Windows 10 专业版 | Oracle VirtualBox 的宿主机 | | | Oracle VirtualBox | 虚拟机软件(Windows 版本) | [VirtualBox 介绍及安装](https://www.itqaq.com/index/627.html) | | ubuntu-24.04.4-desktop-amd64.iso | Ubuntu24.04 LTS 桌面版镜像文件 | [官方下载地址](https://ubuntu.com/download/server) ,[我下载好的镜像](https://pan.baidu.com/s/1Pl8xU31GiKuW6m32ZaI5BA?pwd=schw) | | Ubuntu 24.04 Desktop 语言支持 | 默认系统语言为英文且无法输入中文 | [Ubuntu 24.04 中文输入法](https://www.itqaq.com/index/694.html) | | Oh My Zsh | Zsh 终端配置管理工具 | [Oh My Zsh 介绍及安装](https://www.itqaq.com/index/362.html) | | zsh-command-time | 显示命令执行消耗时长 | [Oh My Zsh 第三方插件](https://www.itqaq.com/index/568.html) | | zsh-autosuggestions | 根据历史命令自动提示并补全命令 | [Oh My Zsh 第三方插件](https://www.itqaq.com/index/568.html) | | zsh-syntax-highlighting | 检测命令是否存在,命令高亮显示 | [Oh My Zsh 第三方插件](https://www.itqaq.com/index/568.html) | | nvm | node 版本管理工具(macOS/Linux) | [nvm 安装和使用教程](https://www.itqaq.com/index/509.html) | | nvm-windows | node 版本管理工具(Windows) | [nvm-windows 安装和使用](https://www.itqaq.com/index/564.html) | | cc-switch | 多环境切换工具(快速切换模型) | [cc-switch 项目仓库](https://github.com/farion1231/cc-switch) | 这些是常用命令工具,Ubuntu Desktop 默认没有预装,可以通过以下命令安装使用: ```bash sudo apt update sudo apt install git vim curl openssh-server -y ``` #### 3. 官方脚本安装 --- 运行官方安装脚本(本文是在 Ubuntu 24.04 Desktop 测试安装): + Claude Code 官方网站:<https://claude.com/product/claude-code> + Claude Code 安装方式:<https://code.claude.com/docs/zh-CN/overview> ```bash # 适用于 macOS、Linux、WSL curl -fsSL https://claude.ai/install.sh | bash ``` 在中国大陆直接运行官方安装脚本会报错:因为目前仅在某些地区可用,在中国大陆等非支持地区会无法正常安装  解决方案:配置终端代理,让终端网络通过代理软件转发,使用支持下载 Claude 的节点,请移步:[Clash 代理软件的使用](https://www.itqaq.com/index/550.html) + 可使用的节点:新加坡[CM] + 不可用的节点:中国大陆、中国香港 配置终端代理后,重新运行官方的安装脚本(稍等一会,没有那么快): ```plaintext $ curl -fsSL https://claude.ai/install.sh | bash Setting up Claude Code... ✔ Claude Code successfully installed! Version: 2.1.104 Location: ~/.local/bin/claude Next: Run claude --help to get started ⚠ Setup notes: • Native installation exists but ~/.local/bin is not in your PATH. Run: echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc < ✅ Installation complete! ``` Setup notes:本机已安装,但是还没有配置到 `PATH`,运行以下命令完成配置 ```bash echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc ``` 查看 Claude Code 版本号 ```bash # 简写 claude -v claude --version ``` 打开新的终端窗口,运行 `claude` 命令,可能会看到以下输出内容,这说明当前所在地区 Claude Code 并不支持 + 结论:不仅仅是安装 Claude Code 需要使用终端代理,使用过程中也需要 ```plaintext $ claude ... Unable to connect to Anthropic services Failed to connect to api.anthropic.com: ERR_BAD_REQUEST Please check your internet connection and network settings. Note: Claude Code might not be available in your country. Check supported countries at https://anthropic.com/supported-countries ``` 开启终端代理后,重新运行 `claude` 命令,提示 “选择终端文本样式”,我选择的是默认的 “Dark mode”(深色模式) + 首次运行 claude 命令时出现,这是 Claude Code 的初始化向导,会引导你配置终端主题、登录方式 ```plaintext $ mkdir ~/mytodo && cd ~/mytodo $ claude ... Let's get started. Choose the text style that looks best with your terminal To change this later, run /theme ❯ 1. Dark mode ✔ 2. Light mode 3. Dark mode (colorblind-friendly) 4. Light mode (colorblind-friendly) 5. Dark mode (ANSI colors only) 6. Light mode (ANSI colors only) ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 1 function greet() { 2 - console.log("Hello, World!"); 2 + console.log("Hello, Claude!"); 3 } ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ Syntax theme: Monokai Extended (ctrl+t to disable) ``` 选择使用 Claude Code 的计费或身份验证方式: + 使用订阅:已经购买了 Claude Pro 或 Claude Max 会员的个人用户 + API 用量计费:开发者、企业用户,或者没有购买订阅但需要大量使用的用户 + 第三方平台:通过第三方云平台使用(如亚马逊 AWS Bedrock、微软 Azure Foundry 或谷歌 Vertex AI) ```plaintext $ claude ... Claude Code can be used with your Claude subscription or billed based on API usage through your Console account. Select login method: ❯ 1. Claude account with subscription · Pro, Max, Team, or Enterprise 2. Anthropic Console account · API usage billing 3. 3rd-party platform · Amazon Bedrock, Microsoft Foundry, or Vertex AI ``` 我选择 API 用量计费的方式使用,终端会给你 Claude 官网的地址,使用浏览器打开它 ```plaintext https://platform.claude.com/oauth/authorize?code=true&client_id=9d1c250a-e61b-xxx... ``` 可以使用 google 账号登录,最后会给你一个授权码(Authentication Code) ```plaintext 7Z1AnkHlHpUTaAoVotJjvFQc6HnvxJnXmwO8HmQwrrf9oSbI#qqhxstTafICXdHX92XS_UcOTw5jNJeshe_Fbxxxxxx ``` 将 Authentication Code 粘贴到终端即可登录成功,依次会有以下提示,直接按下 `Enter` 回车继续下一步 ```plaintext Logged in as guanling3077@gmail.com Login successful. Press Enter to continue… ``` ```plaintext Security notes: 1. Claude can make mistakes You should always review Claude's responses, especially when running code. 2. Due to prompt injection risks, only use it with code you trust For more details see: https://code.claude.com/docs/en/security Press Enter to continue… ``` 重新运行 `claude` 命令,可以看到以下内容。恭喜你,Claude Code 安装完成了 ! ```plaintext $ claude ╭─── Claude Code v2.1.105 ──────────────────────────────────────────────────────────────────────────────────╮ │ │ Tips for getting started │ │ Welcome back liang! │ Ask Claude to create a new app or clone a repository │ │ │ ──────────────────────────────────────────────────── │ │ ▐▛███▜▌ │ Recent activity │ │ ▝▜█████▛▘ │ No recent activity │ │ ▘▘ ▝▝ │ │ │ Sonnet 4.6 · API Usage Billing · liang‘s │ │ │ Individual Org │ │ │ ~/mytodo │ │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ──────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ ──────────────────────────────────────────────────────────────────────────────────────────────────────────── ? for shortcuts ``` #### 4. 使用 npm 安装 --- 从 2026 年 1月的版本(2.1.15+)开始,官方为了安全性和稳定性,正式停止了对 npm 安装方式的支持 + 截止目前为止(2026.04)虽然还可以使用此方式安装,但是已经不建议使用该方式了 为什么不推荐 npm 安装 ? + 安全风险:官方指出 npm 生态中存在恶意包风险,且依赖链复杂,不如原生二进制文件安全可靠 + 依赖冲突与权限问题:需要先安装 Node.js (>=18),增加了配置的复杂度,全局安装 npm 包经常遇到权限问题 Claude Code 是基于 Node.js 运行的,我们要先安装 Node 环境,`apt` 安装的版本可能比较低,推荐使用 `nvm` 安装 ```bash # 安装 nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash # 重载配置文件,立即生效 source ~/.zshrc # 按照环境要求安装 Node 稳定版本 nvm install 22 --lts ``` 加速 npm 下载(可选,但推荐):国内直接下载 npm 包可能会超时,此时可以切换到国内镜像源 ```bash npm config set registry https://registry.npmmirror.com ``` 全局安装 Claude Code: ```bash npm install -g @anthropic-ai/claude-code ``` 运行命令确认安装成功: + 我没有配置国内镜像源,8~27 秒就安装好了(相对官方脚本安装,这安装速度也太快太简单了吧) ```bash claude --version ``` 如果你现在直接运行 `claude` 命令,想要开始使用它,还是会提示错误:所在地区不支持 ```plaintext $ claude ... Unable to connect to Anthropic services Failed to connect to api.anthropic.com: ERR_BAD_REQUEST Please check your internet connection and network settings. Note: Claude Code might not be available in your country. Check supported countries at https://anthropic.com/supported-countries ``` 由于 Anthropic 官方不支持中国大陆访问且需要海外信用卡充值,推荐使用国内的中转服务商(AI 大模型 API 服务商) + 注册一个国内支持 Claude API 的平台(如:硅基流动 SiliconFlow) + 在后台获取 API Key(通常以 `sk-` 开头) + 记下该平台的 Base URL(接口地址) 配置环境变量(关键步骤): + 告诉 Claude Code 使用国内的接口,而不是去连被墙的全球官方服务器 + 本文以使用 [SiliconFlow](https://www.siliconflow.com) 为例,先去该平台获取 API 密钥,然后运行以下命令 ```bash # sk-xxxx 是在 SiliconFlow 官网获取的 API 密钥 echo 'export ANTHROPIC_AUTH_TOKEN="sk-vzanthlkmmmexxxx"' >> ~/.zshrc echo 'export ANTHROPIC_BASE_URL="https://api.siliconflow.cn/v1"' >> ~/.zshrc # 使配置立即生效 source ~/.zshrc ``` 启动 Claude Code(没有提示拒绝访问,能提示设置终端主题,说明已经成功访问国内接口了) ```plaintext $ mkdir ~/code && cd ~/code $ claude ... Let's get started. Choose the text style that looks best with your terminal To change this later, run /theme ❯ 1. Dark mode ✔ 2. Light mode 3. Dark mode (colorblind-friendly) 4. Light mode (colorblind-friendly) 5. Dark mode (ANSI colors only) 6. Light mode (ANSI colors only) ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 1 function greet() { 2 - console.log("Hello, World!"); 2 + console.log("Hello, Claude!"); 3 } ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ Syntax theme: Monokai Extended (ctrl+t to disable) ``` 如果是配置了国内 API,它通常会跳过官方登录步骤: + 官方脚本进行到此处:会提示选择使用 Claude Code 的计费或身份验证方式 + 配置了国内 API:跳过官方登录步骤,来到了 Security notes(安全注意事项) ```plaintext Security notes: 1. Claude can make mistakes You should always review Claude's responses, especially when running code. 2. Due to prompt injection risks, only use it with code you trust For more details see: https://code.claude.com/docs/en/security Press Enter to continue… ``` 提示是否信任工作文件夹(Trust this folder ?),直接回车确认即可 ```plaintext $ claude ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Accessing workspace: /home/vboxuser/code Quick safety check: Is this a project you created or one you trust? (Like your own code, a well-known open source project, or work from your team). If not, take a moment to review what's in this folder first. Claude Code'll be able to read, edit, and execute files here. Security guide ❯ 1. Yes, I trust this folder 2. No, exit Enter to confirm · Esc to cancel ``` 进入对话界面(恭喜你,首次运行引导已经结束,可以开始使用了): ```plaintext ╭─── Claude Code v2.1.107 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ Tips for getting started │ │ Welcome back! │ Run /init to create a CLAUDE.md file with instructions for Claude │ │ │ Note: You have launched claude in your home directory. For the best experience, launch it in a project director… │ │ ▐▛███▜▌ │ ──────────────────────────────────────────────────────────────────────────────────────────────────────────────── │ │ ▝▜█████▛▘ │ Recent activity │ │ ▘▘ ▝▝ │ No recent activity │ │ │ │ │ Sonnet 4.6 · API Usage Billing │ │ │ /home/vboxuser │ │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ? for shortcuts ``` #### 5. macOS 系统安装 --- 如果你已经安装 Homebrew,可以运行以下命令安装: ```bash brew install --cask claude-code ``` 实际上在没有配置国内镜像源或终端代理的情况下,下载也是很慢,我使用终端代理加速下载(耗时 1m 11s) ```plaintext $ brew install --cask claude-code ✔︎ JSON API cask.jws.json Downloaded 15.4MB/ 15.4MB ✔︎ JSON API formula.jws.json Downloaded 32.0MB/ 32.0MB ==> Fetching downloads for: claude-code ✔︎ Cask claude-code (2.1.92) Verified 198.7MB/198.7MB ==> Caveats This cask tracks the stable release channel. In-app update notifications default to the latest channel regardless. To align notifications with this cask, set the auto-update channel to "stable" via /config or in ~/.claude/settings.json: https://code.claude.com/docs/en/setup#configure-release-channel ==> Installing Cask claude-code ==> Linking Binary 'claude' to '/opt/homebrew/bin/claude' 🍺 claude-code was successfully installed! ``` #### 6. Windows 系统安装 --- ##### 使用 npm 安装 在 Windows 上安装 Claude Code 与 Linux/macOS 类似,可以使用 `npm` 的方式安装,安装方式和注意事项略有不同 + 推荐使用 nvm-windows 安装 node,方便后续切换版本和管理(软件介绍中有安装教程) + Claude Code 依赖 Git,运行 `claude` 命令时如果没有安装 Git 会提示错误(去官网自行安装) 当前你已经安装好 `nvm-windows` 和 `git`,接下来我们准备安装 Node.js(建议安装版本 v18+): ```bash # 我安装的是当前最新的 LTS 版本(长期支持版本) nvm install 24.14.1 ``` 全局安装 Claude Code: ```bash npm install -g @anthropic-ai/claude-code ``` 查看 Claude Code 版本: ```bash claude --version ``` 现在如果使用 `claude` 启动 Claude Code,会看到所在地区不支持,因为当前使用的模型还是 Claude Code 官方源 我们可以切换为国产模型,这里我们可以用到一个工具 [cc-switch](https://github.com/farion1231/cc-switch),可以用来快速切换 Claude Code 使用的 AI 模型 + 我这里使用的是 DeepSeek,去 [DeepSeek 开放平台](https://platform.deepseek.com) 获取一个 `API Key` 来使用  此时,重新运行 `claude` 命令就可以正常使用了。我们还可以在 VSCode 中使用 Claude Code: + 需要安装插件:[Claude Code for VS Code](https://marketplace.visualstudio.com/items?itemName=anthropic.claude-code)  我们来使用 Claude Code 生成一个自我介绍的个人主页,使用的提示词: 我是一名专业的前端程序员,帮我生成一个自我介绍的个人主页,让大家可以快速的认识我了解我。使用 HTML 格式,简约风格,科技感配色。页面元素要有自我介绍板块、联系方式二维码区域。  ##### 使用 PowerShell 安装 Windows PowerShell 运行以下命令安装: ```plaintext irm https://claude.ai/install.ps1 | iex ``` 在 PowerShell 中配置代理,运行以下命令(仅当前窗口有效): ```plaintext $env:HTTP_PROXY = "http://127.0.0.1:7890" $env:HTTPS_PROXY = "http://127.0.0.1:7890" ``` 检查查看代理环境变量是否设置成功,输入以下命令查看: ```plaintext $env:HTTP_PROXY $env:HTTPS_PROXY ``` 验证代理是否有效: ```plaintext curl.exe ip.sb ``` 运行 PowerShell 的安装脚本开始安装: ```plaintext PS C:\Users\Administrator> irm https://claude.ai/install.ps1 | iex Setting up Claude Code... √ Claude Code successfully installed! Version: 2.1.109 Location: C:\Users\Administrator\.local\bin\claude.exe Next: Run claude --help to get started ‼ Setup notes: • Native installation exists but C:\Users\Administrator\.local\bin is not in your PATH. Add it by opening: System Properties → Environment Variables → Edit User PATH → New → Add the path above. Then restart your terminal. ✅ Installation complete! ``` 根据提示手动配置环境变量:此电脑 -\-> -\-属性 -\-> 高级系统设置 -\-> 环境变量 -\-> 系统环境变量(Path) ```plaintext 编辑系统环境变量 Path,将 C:\Users\Administrator\.local\bin 添加进去,重启 PowerShell ```