背景
作为一名使用 Windows 11 + RTX 5090 的开发者,我在尝试安装 Claude Code 时遇到了系统不兼容的问题。经过探索,我成功通过 WSL2 方案实现了 Claude Code 与 PyCharm 的完美集成。以下是我的详细实践经验。
初始问题
在 Windows 环境下直接安装 Claude Code 时遇到错误:
bash
npm install -g @anthropic-ai/claude-code
# 错误输出:
# Error: Claude Code is not supported on Windows.
# Claude Code requires macOS or Linux to run properly.
解决方案:WSL2 + Claude Code + PyCharm 集成
步骤 1:安装和配置 WSL2
1.1 检查系统版本
winver
成功标志:显示 Windows 11 版本 2004 或更高
1.2 启用 WSL 功能(管理员 PowerShell)
bash
# 启用 WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
# 启用虚拟机平台
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
成功标志:显示 "操作成功完成"
重要:必须重启计算机!
1.3 安装 WSL2 和 Ubuntu
csharp
# 设置默认版本
wsl --set-default-version 2
# 更新 WSL(可能遇到 403 错误)
wsl --update
常见问题 :如果 wsl --update
返回 403 错误,使用 Microsoft Store 安装:
arduino
Start-Process "ms-windows-store://pdp/?productid=9P9TQF7MRM4R"
1.4 安装 Ubuntu
css
wsl --install -d Ubuntu-22.04
成功标志:自动打开 Ubuntu 终端,要求创建用户名和密码
步骤 2:将 WSL 迁移到其他磁盘(可选但推荐)
为了节省 C 盘空间,我将 WSL 迁移到了 E 盘:
php
# 创建目标目录
New-Item -ItemType Directory -Path "E:\WSL" -Force
New-Item -ItemType Directory -Path "E:\WSL\Ubuntu-22.04" -Force
New-Item -ItemType Directory -Path "E:\WSL\backups" -Force
# 导出现有安装
wsl --shutdown
wsl --export Ubuntu-22.04 E:\WSL\backups\ubuntu-22.04-backup.tar
# 注销原安装
wsl --unregister Ubuntu-22.04
# 导入到新位置
wsl --import Ubuntu-22.04 E:\WSL\Ubuntu-22.04 E:\WSL\backups\ubuntu-22.04-backup.tar --version 2
# 设置默认用户
wsl -d Ubuntu-22.04 -u root bash -c "echo '[user]' > /etc/wsl.conf"
wsl -d Ubuntu-22.04 -u root bash -c "echo 'default=yourusername' >> /etc/wsl.conf"
wsl --shutdown
成功标志 :wsl --list -v
显示 Ubuntu-22.04 位于新位置
步骤 3:在 WSL 中配置开发环境
3.1 更新系统
sql
sudo apt update && sudo apt upgrade -y
3.2 安装开发工具
bash
# 基础工具
sudo apt install -y build-essential git curl wget software-properties-common
# Python
sudo apt install -y python3 python3-pip python3-venv
3.3 安装 Node.js(用于 Claude Code)
arduino
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
成功标志:
bash
node --version # 显示 v22.16.0 或更高
npm --version # 显示 10.9.2 或更高
步骤 4:安装 Claude Code
bash
# 全局安装
sudo npm install -g @anthropic-ai/claude-code
# 验证安装
claude --version
成功标志 :显示版本号如 1.0.31 (Claude Code)
配置 Claude Code
bash
# 运行 claude 开始配置
claude
配置流程:
- 选择主题(我选择了 Dark mode)
- 选择登录方式(选择 Anthropic Console account)
- 浏览器会自动打开,完成授权
- 显示 "Login successful"
重要:Claude Code 有目录访问限制,必须在项目目录中启动!
步骤 5:配置 PyCharm 集成
5.1 创建项目虚拟环境
bash
cd /mnt/e/PycharmProjects/py_system
python3 -m venv venv
source venv/bin/activate
5.2 PyCharm 配置 WSL Python 解释器
- File → Settings → Project → Python Interpreter
- 点击齿轮 → Add → WSL
- 选择 Ubuntu-22.04
- 环境位置:
/mnt/e/PycharmProjects/py_system/venv
成功标志:PyCharm 显示 "Python 3.10.12 WSL (Ubuntu-22.04)"
5.3 配置终端使用 WSL
File → Settings → Tools → Terminal
- Shell path:
wsl.exe -d Ubuntu-22.04
5.4 添加 Claude Code 外部工具
File → Settings → Tools → External Tools
工具 1:Claude Interactive
- Name:
Claude Code - Interactive
- Program:
wsl.exe
- Arguments:
-d Ubuntu-22.04 bash -c "cd /mnt/e/PycharmProjects/py_system && claude"
- Working directory:
$ProjectFileDir$
工具 2:Claude Analyze File
- Name:
Claude Code - Analyze File
- Program:
wsl.exe
- Arguments:
-d Ubuntu-22.04 bash -c "cd /mnt/e/PycharmProjects/py_system && echo 'analyze $FileNameWithoutExtension$.$FileExt$ and suggest improvements' | claude"
工具 3:Claude Fix Errors
- Name:
Claude Code - Fix Errors
- Program:
wsl.exe
- Arguments:
-d Ubuntu-22.04 bash -c "cd /mnt/e/PycharmProjects/py_system && echo 'fix errors in $FileNameWithoutExtension$.$FileExt$' | claude"
5.5 设置快捷键
File → Settings → Keymap,搜索 "Claude Code":
- Claude Interactive:
Ctrl+Alt+C
- Analyze File:
Ctrl+Alt+A
- Fix Errors:
Ctrl+Alt+F
步骤 6:处理项目依赖
常见依赖冲突问题
在安装 requirements.txt 时可能遇到版本冲突:
ini
ERROR: Cannot install langchain-community==0.2.0 because these package versions have conflicting dependencies
解决方案:使用不指定版本的方式安装
bash
# 创建最小依赖文件
cat > requirements_minimal.txt << 'EOF'
langchain
langchain-community
langchain-experimental
# ... 其他包不指定版本
EOF
pip install -r requirements_minimal.txt
RTX 5090 PyTorch 配置
对于 RTX 5090,需要安装支持 CUDA 12.8 的 PyTorch:
perl
# 卸载默认版本
pip uninstall torch torchvision torchaudio -y
# 安装 CUDA 12.8 版本
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
验证 CUDA:
python
import torch
print(f"GPU: {torch.cuda.get_device_name(0)}") # 应显示 NVIDIA GeForce RTX 5090
print(f"CUDA: {torch.cuda.is_available()}") # 应显示 True
步骤 7:使用 Claude Code
在项目目录中启动:
bash
cd /mnt/e/PycharmProjects/py_system
claude
初始化项目:
bash
/init # 创建 .claude.md 文件
成功标志:Claude Code 成功分析项目结构并创建配置文件
经验总结
- WSL2 是 Windows 上使用 Claude Code 的最佳方案
- 始终在项目目录中启动 Claude Code,避免权限问题
- 使用灵活的包版本管理,让 pip 自动解决依赖冲突
- 保存成功的环境配置 :
pip freeze > requirements_working.txt
- PyCharm 集成让工作流程更顺畅,快捷键大幅提升效率
通过这个方案,我成功在 Windows 环境下获得了接近原生 Linux 的 Claude Code 体验,同时保留了 Windows 的便利性和 PyCharm 的强大功能。