国内安装claude code

避坑指南:在 Linux 上安全安装 Claude Code CLI

最近想体验 Anthropic 官方的命令行工具 claude-code,结果在安装过程中踩了两个典型的坑。如果你也在 Linux 上遇到了 Region UnavailableEACCES permission denied 错误,这篇博客能帮你快速解决。

❌ 第一坑:官方脚本被地区限制拦截

按照官网文档,

https://code.claude.com/docs/en/quickstart

第一步通常是运行:

bash 复制代码
curl -fsSL https://claude.ai/install.sh | bash

结果报错:

报错里面含有 App unavailable in region

复制代码
bash: line 1: syntax error near unexpected token `<'
bash: line 1: `<!DOCTYPE html><!-- Last Published: Wed Mar 18 2026 22:03:54 GMT+0000 (Coordinated Universal Time) --><html data-wf-domain="websitemain.claude.com" data-wf-page="68bd5cf2687bfe3893fd2b7f" data-wf-site="6889473510b50328dbb70ae6" data-wf-intellimize-customer-id="117902971" lang="en-US"><head><meta charset="utf-8"/><title>App unavailable in region | Claude</title><link rel="alternate" hrefLang="x-default" href="https://claude.com/app-unavailable-in-region"/><link rel="alternate" hrefLang="en-US" href="https://claude.com/app-unavailable-in-region"/><link rel="alternate" hrefLang="ja-JP" href="https://claude.com/ja-jp/app-unavailable-in-region"/><link rel="alternate" hrefLang="de-DE" href="https://claude.com/de-de/app-unavailable-in-region"/><link rel="alternate" hrefLang="fr-FR" href="https://claude.com/fr-fr/app-unavailable-in-region"/><link rel="alternate" hrefLang="ko-KR" href="https://claude.com/ko-kr/app-unavailable-in-region"/><meta content="Unfortunately, Claude isn't available here." name="description"/><meta content="App unavailable in region | Claude" property="og:title"/><meta content="Unfortunately, Claude isn't available here." property="og:description"/><meta content="https://cdn.prod.website-files.com/6889473510b50328dbb70ae6/68c469d23594abeb9ab6ee48_og-claude-generic.jpg" property="og:image"/><meta content="App unavailable in region | Claude" property="twitter:title"/><meta content="Unfortunately, Claude isn't available here." property="twitter:description"/><meta property="og:type" content="website"/><meta content="summary_large_image" name="twitter:card"/><meta content="width=device-width, initial-scale=1" name="viewport"/><meta content="tPMMBQMBzgZlNmCBal5cMPAx3nhO2iyM4rT9nxuRcdk" name="google-site-verification"/><link href="https://cdn.prod.website-files.com/6889473510b50328dbb70ae6/css/claude-brand.shared.e1c3addac.min.css" rel="stylesheet" type="text/css" integrity="sha384-4cOt2sOCB8umzY27E8CaMtj5IHLMtcRrdxJWvOpV3Q6MjS2oKhU131waB+p1gTXi" crossorigin="anonymous"/><script type="text/javascript">!function(o,c){var n=c.documentElement,t=" w-mod-";n.className+=t+"js",("ontouchstart"in o||o.DocumentTouch&&c instanceof DocumentTouch)&&(n.className+=t+"touch")}(window,document);</script><link href="https://cdn.prod.website-files.com/6889473510b50328dbb70ae6/689f4a9aff1f63fde75cf733_favicon.png" rel="shortcut icon" type="image/x-icon"/><link href="https://cdn.prod.website-files.com/6889473510b50328dbb70ae6/68c33859cc6cd903686c66a2_apple-touch-icon.png" rel="apple-touch-icon"/><link href="https://claude.com/app-unavailable-in-region" rel="canonical"/><style>.anti-flicker, .anti-flicker * {visibility: hidden !important; opacity: 0 !important;}</style><style>[data-wf-hidden-variation], [data-wf-hidden-variation] * {'
curl: (23) Failure writing output to destination

原因分析:

在国内网络环境下,直接访问该脚本会被 Anthropic 服务器拦截,返回一个"此区域不可用"的 HTML 页面,而不是 Shell 脚本。Bash 试图执行 HTML 代码,自然语法报错。

✅ 解决方案:改用 npm 安装

绕过下载脚本,直接通过 Node.js 包管理器安装:

bash 复制代码
npm install -g @anthropic-ai/claude-code

(提示:如果下载慢,可先切换淘宝镜像:npm config set registry https://registry.npmmirror.com)


❌ 第二坑:权限不足 (EACCES)

运行 npm 命令后,新的错误出现了:

text 复制代码
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied...

原因分析:

默认情况下,npm 试图将全局包安装到系统目录 /usr/local/lib/node_modules。普通用户没有写入该目录的权限。

⚠️ 常见误区:不要直接用 sudo

这个教程

https://github.com/claude-code-chinese/claude-code-guide

不建议加 sudo 说有安全风险:

bash 复制代码
sudo npm install -g ... # 不推荐!

✅ 最佳实践:配置用户级全局路径

我们将 npm 的全局安装目录修改为用户自己的目录,既安全又无需 sudo。

🛠️ 操作步骤

1. 创建专属目录

bash 复制代码
mkdir -p ~/.npm-global

2. 配置 npm 使用该目录

bash 复制代码
npm config set prefix '~/.npm-global'

3. 添加环境变量

编辑你的 shell 配置文件(~/.bashrc~/.zshrc):

bash 复制代码
nano ~/.bashrc  # 或者 nano ~/.zshrc

在文件末尾添加一行,将新目录加入 PATH

bash 复制代码
export PATH="$HOME/.npm-global/bin:$PATH"

保存退出(Ctrl+O, Enter, Ctrl+X)。

4. 刷新配置

bash 复制代码
source ~/.bashrc  # 或者 source ~/.zshrc

5. 重新安装(无需 sudo)

bash 复制代码
npm install -g @anthropic-ai/claude-code

🎉 验证与额外配置

安装完成后,输入以下命令验证:

bash 复制代码
claude --version

如果显示版本号,恭喜安装成功!

相关推荐
DigitalOcean1 小时前
DeepSeek 推理接近 4 倍提速,这家 AI 推理云是怎么做到的?
gpt·claude·deepseek
Resistance丶未来1 小时前
GPT-5.5 深度评测:性能边界与实战价值全解析
gpt·大模型·api·claude·gemini·api key·gpt5.5
西索斯1 小时前
MiniMax M2.7 实测:和 Claude Sonnet 4.6、GPT-5.5 放一起跑,结果有点意外
aigc·claude
雷欧力2 小时前
如何使用 Claude API?3 种接入方案实测,附完整代码(2026)
python·claude
西索斯3 小时前
Claude API 报 529 Overloaded 怎么办?3 种方案实测,最后一种最省心
python·claude
拾贰_C3 小时前
【OpenClaw | openai | QQ】 配置QQ qot机器人
运维·人工智能·ubuntu·面试·prompt
郝亚军4 小时前
ubuntu 22.04如何安装libmodbus
运维·服务器·ubuntu
乌恩大侠5 小时前
【AI-RAN】在空ubuntu服务器安装环境和生成TV,高达430G文件
服务器·人工智能·ubuntu·fpga开发·o-ru
Hello.Reader5 小时前
Ubuntu 上正确安装 Kali 虚拟机、Docker 与 kail 工具指南
linux·ubuntu·docker
一袋米扛几楼986 小时前
【报错问题】解决 Vercel 部署报错:Express 类型失效与 TypeScript 2349/2339/2769 错误排查
ubuntu·typescript·express