Linux 是 Claude Code 的原生运行环境,支持所有主流发行版。以下按包管理器分类说明,均支持 x86_64 和 ARM64 架构。
前置依赖安装
所有发行版通用检查:
# 确认 Node.js 版本(必须 18.x 或 20.x)
node -v || echo "需安装 Node.js"
# 确认基础工具
which curl && which git || echo "需安装 curl 和 git"
Ubuntu / Debian:
# 安装 Node.js 20.x(官方推荐源)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs git curl
# 验证
node -v # 应显示 v20.x.x
Fedora / RHEL / CentOS Stream:
# Fedora 38+
sudo dnf install -y nodejs20 git curl
# RHEL 9 / CentOS Stream 9
sudo dnf module enable nodejs:20
sudo dnf install -y nodejs git curl
# 旧版 CentOS 7/8 建议改用官方脚本安装 Node,或使用 npm 二进制包
Arch Linux:
# 使用 pacman 安装 Node.js(通常较新)
sudo pacman -S nodejs npm git curl
安装方式选择
方式一:官方脚本(推荐,适用于所有发行版)
# 标准安装(自动检测 /usr/local/bin 权限)
curl -fsSL https://claude.ai/install.sh | sh
# 无 sudo 权限时,安装到用户目录
curl -fsSL https://claude.ai/install.sh | sh -s -- --install-dir ~/.local/bin
# 安装后如提示未找到命令,手动添加 PATH
echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
方式二:npm 全局安装
# 确保 npm 前缀目录可写(避免使用 sudo)
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
# 安装
npm install -g @anthropic-ai/claude-code
方式三:特定发行版包管理器(社区维护)
Ubuntu/Debian(通过 npm 或脚本,暂无官方 PPA):
建议直接使用上述官方脚本,比 PPA 更新更及时。
Arch Linux(AUR):
# 使用 yay 或 paru 安装(社区维护,可能略滞后)
yay -S claude-code
# 或
paru -S claude-code
权限与路径配置
处理 "command not found":
# 查看安装位置
which claude || find ~ -name claude -type f 2>/dev/null
# 如安装在 ~/.local/bin 但不在 PATH
export PATH="$HOME/.local/bin:$PATH"
# 永久生效(根据 shell 选择)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc # Bash
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc # Zsh
桌面环境快捷启动(可选):
如在图形终端中使用,可创建桌面入口:
cat > ~/.local/share/applications/claude-code.desktop << EOF
[Desktop Entry]
Name=Claude Code
Comment=AI coding assistant
Exec=bash -c 'cd $HOME && alacritty -e claude'
Type=Application
Terminal=true
EOF
(需根据实际终端模拟器调整 Exec 行,如 gnome-terminal、konsole 等)
验证安装
# 1. 版本检查
claude --version
# 预期输出:claude 0.x.x
# 2. 帮助信息
claude --help
# 3. 进入测试项目验证 API 连通性
mkdir -p ~/test-project && cd ~/test-project
claude
# 应提示输入 API Key 或直接进入交互界面(如已配置环境变量)
常见问题
GLIBC 版本过低(常见于 CentOS 7/8 或旧 Ubuntu):
官方二进制依赖较新的 glibc,如遇 version GLIBC_2.34 not found:
- 解决方案:改用 npm 安装方式(
npm install -g @anthropic-ai/claude-code),或升级系统至 Ubuntu 22.04+/Fedora 38+
Snap 版 Node.js 冲突:
如使用 snap install node 安装的 Node,可能遇到权限隔离问题:
- 建议:卸载 snap 版 Node,改用 NodeSource 或 nvm 安装
WSL2 特定提示:
如你在 Windows WSL2 中使用 Linux 安装,请参考 2.1.4 节 关于 /mnt/c/ 跨文件系统性能优化的建议。
下一步
安装完成后,请确保已配置 1.3 节 的 ANTHROPIC_API_KEY 环境变量,然后进入 3.4 节 完成首次启动验证。如需在服务器或无图形界面环境使用,建议阅读 6.1 节 关于纯终端交互的快捷键说明。