Claude Code 命令行版 + DeepSeek API 的全平台安装教程

#Claude Code#DeepSeek#教程#代理#终端#开发环境
11 min read

Claude Code 官方支持 macOS 13+、Windows 10 1809+、Windows Server 2019+、Ubuntu 20.04+、Debian 10+、Alpine Linux 3.19+,硬件要求为 4GB+ RAM、x64 或 ARM64。(Claude Code)


一、Windows 安装 Claude Code

1. PowerShell 安装

打开 PowerShell,执行:

powershell
irm https://claude.ai/install.ps1 | iex

官方 Windows PowerShell 安装命令就是这个。(Claude Code)

安装完成后验证:

powershell
claude --version

2. 修复 PATH

安装后看到类似提示:

text
C:\Users\xxxx\.local\bin is not in your PATH

执行:

powershell
$claudeBin = "$env:USERPROFILE\.local\bin"
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
 
if ($userPath -notlike "*$claudeBin*") {
    [Environment]::SetEnvironmentVariable("Path", "$userPath;$claudeBin", "User")
}

关闭 PowerShell,重新打开,再验证:

powershell
claude --version

二、Windows WinGet 安装方式

Claude Code 官方也提供 WinGet 安装方式。(Claude Code)

powershell
winget install Anthropic.ClaudeCode

验证:

powershell
claude --version

手动更新:

powershell
winget upgrade Anthropic.ClaudeCode

WinGet 安装不会自动更新,需要手动升级。(Claude Code)


三、macOS 安装 Claude Code

1. 官方原生安装

打开 Terminal:

bash
curl -fsSL https://claude.ai/install.sh | bash

将 Claude 写进环境变量:

bash
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc

验证:

bash
claude --version

macOS / Linux / WSL 的官方原生安装命令就是上面这条。(Claude Code)

2. Homebrew 安装

bash
brew install --cask claude-code

验证:

bash
claude --version

更新:

bash
brew upgrade claude-code

Homebrew 版不会自动更新,需要手动 brew upgrade。(Claude Code)


四、Linux 安装 Claude Code

1. 官方原生安装

bash
curl -fsSL https://claude.ai/install.sh | bash

刷新 shell:

bash
source ~/.bashrc

验证:

bash
claude --version

五、WSL 安装 Claude Code

WSL 里按 Linux 方式安装:

bash
curl -fsSL https://claude.ai/install.sh | bash

刷新配置:

bash
source ~/.bashrc

验证:

bash
claude --version

WSL 不需要安装 Git for Windows;原生 Windows 使用 Claude Code 时,官方建议安装 Git for Windows,方便 Claude Code 使用 Bash 工具。(Claude Code)


六、npm 通用安装方式

DeepSeek 官方的 Claude Code 集成文档也提供 npm 安装方式,要求 Node.js 18+,Windows 用户还需要 Git for Windows。(DeepSeek API Docs)

1. 检查 Node.js

bash
node -v
npm -v

2. 安装 Claude Code

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

3. 验证

bash
claude --version

七、配置 Claude Code 接 DeepSeek API

DeepSeek 的 Anthropic 兼容 API 地址是:

text
https://api.deepseek.com/anthropic

DeepSeek 官方文档明确说明其 Anthropic API base_url 使用这个地址。(DeepSeek API Docs)

Claude Code 支持通过环境变量控制认证、模型选择、请求路由等行为,也支持通过 settings.json 配置这些环境变量。(Claude Code)


方案 A:推荐,全平台写入 settings.json

Windows

创建目录:

powershell
mkdir "$env:USERPROFILE\.claude" -Force

编辑配置:

powershell
vim "$env:USERPROFILE\.claude\settings.json"

写入:

json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
    "ANTHROPIC_AUTH_TOKEN": "你的DeepSeek_API_Key",
    "ANTHROPIC_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-v4-flash",
    "CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-v4-flash",
    "CLAUDE_CODE_EFFORT_LEVEL": "max"
  }
}

⚠️ 注意将 ANTHROPIC_AUTH_TOKEN 替换为你的 DeepSeek API Key。


macOS / Linux / WSL

创建目录:

bash
mkdir -p ~/.claude

编辑配置:

bash
vim ~/.claude/settings.json

写入:

json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
    "ANTHROPIC_AUTH_TOKEN": "你的DeepSeek_API_Key",
    "ANTHROPIC_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-v4-pro[1m]",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-v4-flash",
    "CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-v4-flash",
    "CLAUDE_CODE_EFFORT_LEVEL": "max"
  }
}

⚠️ 注意将 ANTHROPIC_AUTH_TOKEN 替换为你的 DeepSeek API Key。

Claude Code 官方说明,用户级配置文件是 ~/.claude/settings.json,Windows 下 ~/.claude 对应 %USERPROFILE%\.claude。(Claude Code)


方案 B:临时环境变量配置

Windows PowerShell

powershell
$env:ANTHROPIC_BASE_URL="https://api.deepseek.com/anthropic"
$env:ANTHROPIC_AUTH_TOKEN="你的DeepSeek_API_Key"
$env:ANTHROPIC_MODEL="deepseek-v4-pro[1m]"
$env:ANTHROPIC_DEFAULT_OPUS_MODEL="deepseek-v4-pro[1m]"
$env:ANTHROPIC_DEFAULT_SONNET_MODEL="deepseek-v4-pro[1m]"
$env:ANTHROPIC_DEFAULT_HAIKU_MODEL="deepseek-v4-flash"
$env:CLAUDE_CODE_SUBAGENT_MODEL="deepseek-v4-flash"
$env:CLAUDE_CODE_EFFORT_LEVEL="max"

启动:

powershell
claude

DeepSeek 官方 Windows 配置变量就是这一组。(DeepSeek API Docs)


macOS / Linux / WSL

bash
export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
export ANTHROPIC_AUTH_TOKEN=你的DeepSeek_API_Key
export ANTHROPIC_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_OPUS_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_HAIKU_MODEL=deepseek-v4-flash
export CLAUDE_CODE_SUBAGENT_MODEL=deepseek-v4-flash
export CLAUDE_CODE_EFFORT_LEVEL=max

启动:

bash
claude

DeepSeek 官方 Linux / Mac 配置变量也是这一组。(DeepSeek API Docs)


八、进入项目启动 Claude Code

进入项目目录:

bash
cd /path/to/my-project
claude

Windows 示例:

powershell
cd E:\your-project
claude

进入后检查状态:

text
/status

九、常用基础命令

进入 Claude Code 后常用:

text
/status
/model
/permissions
/help

让它先读项目结构:

text
解释当前项目的目录结构、技术栈、启动命令和主要入口文件。不要修改任何文件。

让它只做计划:

text
先不要修改文件。请阅读代码后给出一个简短修复计划,包含问题定位、涉及文件、修改步骤和验证方式。

十、推荐最终配置结构

Windows

text
C:\Users\你的用户名\.claude\settings.json

macOS / Linux / WSL

text
~/.claude/settings.json

项目级 Claude 指令文件

放在项目根目录:

text
CLAUDE.md

示例:

markdown
# Project Instructions
 
- 先阅读项目结构再执行修改。
- 修改前先说明将改哪些文件。
- 一次只完成一个明确任务。
- 不要主动重构无关代码。
- 修改后给出验证命令。
- 涉及依赖变更时,说明新增依赖的用途。

十一、更新 Claude Code

Windows 原生安装

原生安装版通常会自动更新。(Claude Code)

Windows WinGet

powershell
winget upgrade Anthropic.ClaudeCode

macOS Homebrew

bash
brew upgrade claude-code

npm

bash
npm update -g @anthropic-ai/claude-code

十二、卸载

Windows 原生安装

删除安装目录:

powershell
Remove-Item "$env:USERPROFILE\.local\bin\claude.exe" -Force

删除配置:

powershell
Remove-Item "$env:USERPROFILE\.claude" -Recurse -Force

macOS / Linux / WSL 原生安装

bash
rm -f ~/.local/bin/claude
rm -rf ~/.claude

Homebrew

bash
brew uninstall --cask claude-code

npm

bash
npm uninstall -g @anthropic-ai/claude-code

最短安装路径

Windows + DeepSeek

powershell
$env:HTTP_PROXY="http://127.0.0.1:10808"
$env:HTTPS_PROXY="http://127.0.0.1:10808"
$env:ALL_PROXY="http://127.0.0.1:10808"
 
irm https://claude.ai/install.ps1 | iex
 
mkdir "$env:USERPROFILE\.claude" -Force
vim "$env:USERPROFILE\.claude\settings.json"
 
claude --version
claude

macOS / Linux / WSL + DeepSeek

bash
curl -fsSL https://claude.ai/install.sh | bash
 
mkdir -p ~/.claude
vim ~/.claude/settings.json
 
claude --version
claude

十三、终端自动代理配置

目标:

text
代理软件开启 → 终端自动使用代理
代理软件关闭 → 终端自动清除代理
代理软件重启 → 终端自动恢复代理

默认代理端口:

text
127.0.0.1:10808

13.1 Windows PowerShell 自动代理

适用于:

  • Windows 10 / Windows 11
  • PowerShell
  • Windows Terminal
  • Tabby Windows 端
  • v2rayN

1. 创建 PowerShell 配置文件

powershell
if (!(Test-Path $PROFILE)) {
    New-Item -ItemType File -Force $PROFILE | Out-Null
}

2. 编辑 PowerShell 配置文件

powershell
vim $PROFILE

3. 追加下面内容

powershell
# ===== Auto Proxy for PowerShell =====
$Global:AutoProxyHost = "127.0.0.1"
$Global:AutoProxyPort = 10808
$Global:AutoProxyUrl = "http://${Global:AutoProxyHost}:${Global:AutoProxyPort}"
$Global:AutoProxyEnabled = $false
 
function proxy_on {
    $env:HTTP_PROXY = $Global:AutoProxyUrl
    $env:HTTPS_PROXY = $Global:AutoProxyUrl
    $env:ALL_PROXY = $Global:AutoProxyUrl
 
    $env:http_proxy = $Global:AutoProxyUrl
    $env:https_proxy = $Global:AutoProxyUrl
    $env:all_proxy = $Global:AutoProxyUrl
 
    $Global:AutoProxyEnabled = $true
}
 
function proxy_off {
    Remove-Item Env:HTTP_PROXY -ErrorAction SilentlyContinue
    Remove-Item Env:HTTPS_PROXY -ErrorAction SilentlyContinue
    Remove-Item Env:ALL_PROXY -ErrorAction SilentlyContinue
 
    Remove-Item Env:http_proxy -ErrorAction SilentlyContinue
    Remove-Item Env:https_proxy -ErrorAction SilentlyContinue
    Remove-Item Env:all_proxy -ErrorAction SilentlyContinue
 
    $Global:AutoProxyEnabled = $false
}
 
function proxy_auto {
    $proxyOnline = Test-NetConnection $Global:AutoProxyHost -Port $Global:AutoProxyPort -InformationLevel Quiet
 
    if ($proxyOnline) {
        if (-not $Global:AutoProxyEnabled) {
            proxy_on
        }
    } else {
        if ($Global:AutoProxyEnabled) {
            proxy_off
        }
    }
}
 
function proxy_status {
    $proxyOnline = Test-NetConnection $Global:AutoProxyHost -Port $Global:AutoProxyPort -InformationLevel Quiet
 
    if ($proxyOnline) {
        Write-Host "Proxy port is online: $Global:AutoProxyUrl"
    } else {
        Write-Host "Proxy port is offline"
    }
 
    Get-ChildItem Env: | Where-Object { $_.Name -match "proxy" }
}
 
function prompt {
    proxy_auto
    "PS $($executionContext.SessionState.Path.CurrentLocation)> "
}
 
proxy_auto
# ===== Auto Proxy End =====

4. 重新加载配置

powershell
. $PROFILE

5. 查看状态

powershell
proxy_status

6. 测试 Claude 安装源

powershell
irm https://downloads.claude.ai/claude-code-releases/latest

13.2 macOS zsh 自动代理

适用于:

  • macOS
  • Terminal
  • iTerm2
  • Tabby macOS 端
  • Clash
  • v2rayN / v2rayU / V2rayXS
  • sing-box

1. 备份 zsh 配置

bash
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d-%H%M%S) 2>/dev/null || touch ~/.zshrc

2. 编辑 zsh 配置

bash
vim ~/.zshrc

3. 追加下面内容

bash
# ===== Auto Proxy for macOS zsh =====
__AUTO_PROXY_HOST="127.0.0.1"
__AUTO_PROXY_PORT="10808"
__AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
__AUTO_PROXY_ENABLED="0"
 
proxy_on() {
  export HTTP_PROXY="$__AUTO_PROXY_URL"
  export HTTPS_PROXY="$__AUTO_PROXY_URL"
  export ALL_PROXY="$__AUTO_PROXY_URL"
 
  export http_proxy="$__AUTO_PROXY_URL"
  export https_proxy="$__AUTO_PROXY_URL"
  export all_proxy="$__AUTO_PROXY_URL"
 
  __AUTO_PROXY_ENABLED="1"
}
 
proxy_off() {
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
 
  unset http_proxy
  unset https_proxy
  unset all_proxy
 
  __AUTO_PROXY_ENABLED="0"
}
 
proxy_auto() {
  if nc -z -G 1 "$__AUTO_PROXY_HOST" "$__AUTO_PROXY_PORT" >/dev/null 2>&1; then
    if [ "$__AUTO_PROXY_ENABLED" != "1" ]; then
      proxy_on
    fi
  else
    if [ "$__AUTO_PROXY_ENABLED" != "0" ]; then
      proxy_off
    fi
  fi
}
 
proxy_status() {
  if nc -z -G 1 "$__AUTO_PROXY_HOST" "$__AUTO_PROXY_PORT" >/dev/null 2>&1; then
    echo "Proxy port is online: $__AUTO_PROXY_URL"
  else
    echo "Proxy port is offline"
  fi
 
  env | grep -i proxy
}
 
autoload -Uz add-zsh-hook
add-zsh-hook precmd proxy_auto
 
proxy_auto
# ===== Auto Proxy End =====

4. 重新加载配置

bash
source ~/.zshrc

5. 查看状态

bash
proxy_status

6. 测试 Claude 安装脚本

bash
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh

正常开头应为 shell 脚本内容,不应出现:

html
<!DOCTYPE html>

13.3 Linux bash 自动代理

适用于:

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • Linux bash
  • Tabby Linux 端

1. 备份 bash 配置

bash
cp ~/.bashrc ~/.bashrc.backup.$(date +%Y%m%d-%H%M%S) 2>/dev/null || touch ~/.bashrc

2. 编辑 bash 配置

bash
vim ~/.bashrc

3. 追加下面内容

bash
# ===== Auto Proxy for Linux bash =====
__AUTO_PROXY_HOST="127.0.0.1"
__AUTO_PROXY_PORT="10808"
__AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
__AUTO_PROXY_ENABLED="0"
 
proxy_on() {
  export HTTP_PROXY="$__AUTO_PROXY_URL"
  export HTTPS_PROXY="$__AUTO_PROXY_URL"
  export ALL_PROXY="$__AUTO_PROXY_URL"
 
  export http_proxy="$__AUTO_PROXY_URL"
  export https_proxy="$__AUTO_PROXY_URL"
  export all_proxy="$__AUTO_PROXY_URL"
 
  __AUTO_PROXY_ENABLED="1"
}
 
proxy_off() {
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
 
  unset http_proxy
  unset https_proxy
  unset all_proxy
 
  __AUTO_PROXY_ENABLED="0"
}
 
proxy_auto() {
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    if [ "$__AUTO_PROXY_ENABLED" != "1" ]; then
      proxy_on
    fi
  else
    if [ "$__AUTO_PROXY_ENABLED" != "0" ]; then
      proxy_off
    fi
  fi
}
 
proxy_status() {
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    echo "Proxy port is online: $__AUTO_PROXY_URL"
  else
    echo "Proxy port is offline"
  fi
 
  env | grep -i proxy
}
 
PROMPT_COMMAND="proxy_auto${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
 
proxy_auto
# ===== Auto Proxy End =====

4. 重新加载配置

bash
source ~/.bashrc

5. 查看状态

bash
proxy_status

6. 测试 Claude 安装脚本

bash
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh

13.4 Linux zsh 自动代理

适用于:

  • Linux zsh
  • Oh My Zsh
  • Tabby Linux 端

1. 备份 zsh 配置

bash
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d-%H%M%S) 2>/dev/null || touch ~/.zshrc

2. 编辑 zsh 配置

bash
vim ~/.zshrc

3. 追加下面内容

bash
# ===== Auto Proxy for Linux zsh =====
__AUTO_PROXY_HOST="127.0.0.1"
__AUTO_PROXY_PORT="10808"
__AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
__AUTO_PROXY_ENABLED="0"
 
proxy_on() {
  export HTTP_PROXY="$__AUTO_PROXY_URL"
  export HTTPS_PROXY="$__AUTO_PROXY_URL"
  export ALL_PROXY="$__AUTO_PROXY_URL"
 
  export http_proxy="$__AUTO_PROXY_URL"
  export https_proxy="$__AUTO_PROXY_URL"
  export all_proxy="$__AUTO_PROXY_URL"
 
  __AUTO_PROXY_ENABLED="1"
}
 
proxy_off() {
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
 
  unset http_proxy
  unset https_proxy
  unset all_proxy
 
  __AUTO_PROXY_ENABLED="0"
}
 
proxy_auto() {
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    if [ "$__AUTO_PROXY_ENABLED" != "1" ]; then
      proxy_on
    fi
  else
    if [ "$__AUTO_PROXY_ENABLED" != "0" ]; then
      proxy_off
    fi
  fi
}
 
proxy_status() {
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    echo "Proxy port is online: $__AUTO_PROXY_URL"
  else
    echo "Proxy port is offline"
  fi
 
  env | grep -i proxy
}
 
autoload -Uz add-zsh-hook
add-zsh-hook precmd proxy_auto
 
proxy_auto
# ===== Auto Proxy End =====

4. 重新加载配置

bash
source ~/.zshrc

5. 查看状态

bash
proxy_status

13.5 WSL 自动代理

适用于:

  • Windows 11
  • WSL Ubuntu
  • WSL Debian
  • Windows 主机运行 v2rayN
  • WSL 内安装 Claude Code

WSL 里访问 Windows 主机代理,推荐使用 Windows 主机 IP,而不是固定写 127.0.0.1

1. 备份 WSL bash 配置

bash
cp ~/.bashrc ~/.bashrc.backup.$(date +%Y%m%d-%H%M%S) 2>/dev/null || touch ~/.bashrc

2. 编辑 WSL bash 配置

bash
vim ~/.bashrc

3. 追加下面内容

bash
# ===== Auto Proxy for WSL =====
__AUTO_PROXY_PORT="10808"
__AUTO_PROXY_ENABLED="0"
 
get_windows_host_ip() {
  grep nameserver /etc/resolv.conf | awk '{print $2}'
}
 
proxy_on() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
  __AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
 
  export HTTP_PROXY="$__AUTO_PROXY_URL"
  export HTTPS_PROXY="$__AUTO_PROXY_URL"
  export ALL_PROXY="$__AUTO_PROXY_URL"
 
  export http_proxy="$__AUTO_PROXY_URL"
  export https_proxy="$__AUTO_PROXY_URL"
  export all_proxy="$__AUTO_PROXY_URL"
 
  __AUTO_PROXY_ENABLED="1"
}
 
proxy_off() {
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
 
  unset http_proxy
  unset https_proxy
  unset all_proxy
 
  __AUTO_PROXY_ENABLED="0"
}
 
proxy_auto() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
 
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    if [ "$__AUTO_PROXY_ENABLED" != "1" ]; then
      proxy_on
    fi
  else
    if [ "$__AUTO_PROXY_ENABLED" != "0" ]; then
      proxy_off
    fi
  fi
}
 
proxy_status() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
  __AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
 
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    echo "Proxy port is online: $__AUTO_PROXY_URL"
  else
    echo "Proxy port is offline"
  fi
 
  env | grep -i proxy
}
 
PROMPT_COMMAND="proxy_auto${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
 
proxy_auto
# ===== Auto Proxy End =====

4. 重新加载配置

bash
source ~/.bashrc

5. 查看状态

bash
proxy_status

6. 测试 Claude 安装脚本

bash
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh

13.6 WSL zsh 自动代理

1. 备份 zsh 配置

bash
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d-%H%M%S) 2>/dev/null || touch ~/.zshrc

2. 编辑配置

bash
vim ~/.zshrc

3. 追加下面内容

bash
# ===== Auto Proxy for WSL zsh =====
__AUTO_PROXY_PORT="10808"
__AUTO_PROXY_ENABLED="0"
 
get_windows_host_ip() {
  grep nameserver /etc/resolv.conf | awk '{print $2}'
}
 
proxy_on() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
  __AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
 
  export HTTP_PROXY="$__AUTO_PROXY_URL"
  export HTTPS_PROXY="$__AUTO_PROXY_URL"
  export ALL_PROXY="$__AUTO_PROXY_URL"
 
  export http_proxy="$__AUTO_PROXY_URL"
  export https_proxy="$__AUTO_PROXY_URL"
  export all_proxy="$__AUTO_PROXY_URL"
 
  __AUTO_PROXY_ENABLED="1"
}
 
proxy_off() {
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset ALL_PROXY
 
  unset http_proxy
  unset https_proxy
  unset all_proxy
 
  __AUTO_PROXY_ENABLED="0"
}
 
proxy_auto() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
 
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    if [ "$__AUTO_PROXY_ENABLED" != "1" ]; then
      proxy_on
    fi
  else
    if [ "$__AUTO_PROXY_ENABLED" != "0" ]; then
      proxy_off
    fi
  fi
}
 
proxy_status() {
  __AUTO_PROXY_HOST="$(get_windows_host_ip)"
  __AUTO_PROXY_URL="http://${__AUTO_PROXY_HOST}:${__AUTO_PROXY_PORT}"
 
  timeout 1 bash -c "cat < /dev/null > /dev/tcp/${__AUTO_PROXY_HOST}/${__AUTO_PROXY_PORT}" >/dev/null 2>&1
 
  if [ "$?" = "0" ]; then
    echo "Proxy port is online: $__AUTO_PROXY_URL"
  else
    echo "Proxy port is offline"
  fi
 
  env | grep -i proxy
}
 
autoload -Uz add-zsh-hook
add-zsh-hook precmd proxy_auto
 
proxy_auto
# ===== Auto Proxy End =====

4. 生效

bash
source ~/.zshrc

5. 查看状态

bash
proxy_status

十四、代理配置后的 Claude Code 安装测试

Windows

powershell
proxy_status
irm https://downloads.claude.ai/claude-code-releases/latest
irm https://claude.ai/install.ps1 | iex

macOS

bash
proxy_status
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh
bash /tmp/claude-install.sh

Linux

bash
proxy_status
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh
bash /tmp/claude-install.sh

WSL

bash
proxy_status
curl -fsSL https://claude.ai/install.sh -o /tmp/claude-install.sh
head -n 5 /tmp/claude-install.sh
bash /tmp/claude-install.sh

十五、代理端口修改

当前教程统一使用:

text
10808

端口改动时,只修改对应配置里的端口变量。

Windows PowerShell

powershell
$Global:AutoProxyPort = 10808

macOS / Linux

bash
__AUTO_PROXY_PORT="10808"

WSL

bash
__AUTO_PROXY_PORT="10808"

修改后重新加载配置。

Windows

powershell
. $PROFILE

macOS / Linux / WSL

bash
source ~/.zshrc

或:

bash
source ~/.bashrc

Comments