ubuntu 安装 claude code 失败,解决方案
npm install -g @anthropic-ai/claude-code
npm error code EACCES
npm error syscall mkdir
npm error path /usr/lib/node_modules/@anthropic-ai
npm error errno -13
npm error Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/@anthropic-ai'
npm error at async mkdir (node:internal/fs/promises:856:10)
npm error at async /usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:572:20
npm error at async Promise.allSettled (index 0)
npm error at async #reifyPackages (/usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:309:11)
npm error at async Arborist.reify (/usr/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:121:5)
npm error at async Install.exec (/usr/lib/node_modules/npm/lib/commands/install.js:151:5)
npm error at async Npm.exec (/usr/lib/node_modules/npm/lib/npm.js:208:9)
npm error at async module.exports (/usr/lib/node_modules/npm/lib/cli/entry.js:67:5) {
npm error errno: -13,
npm error code: 'EACCES',
npm error syscall: 'mkdir',
npm error path: '/usr/lib/node_modules/@anthropic-ai'
npm error }
npm error
npm error The operation was rejected by your operating system.
npm error It is likely you do not have the permissions to access this file as the current user
npm error
npm error If you believe this might be a permissions issue, please double-check the
npm error permissions of the file and its containing directories, or try running
npm error the command again as root/Administrator.
npm notice
npm notice New minor version of npm available! 11.9.0 -> 11.12.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.12.0
npm notice To update run: npm install -g npm@11.12.0
npm notice
npm error A complete log of this run can be found in: /home/zhouchenbo/.npm/_logs/2026-03-24T06_41_00_808Z-debug-0.log
解决方案:
# 1. 创建用户级全局目录
mkdir -p ~/.npm-global
# 2. 配置 npm 使用该目录
npm config set prefix '~/.npm-global'
# 3. 添加到 PATH(加到 ~/.bashrc 或 ~/.zshrc)
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
# 4. 现在无需 sudo 直接安装
npm install -g @anthropic-ai/claude-code
我选了这种解决方案,之后所有 npm install -g 都不再需要 sudo,也不会有权限问题。
浙公网安备 33010602011771号