You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04卸载Node.js后无法安装Node.js 18的问题求助

Ubuntu 22.04 升级 Node.js 18 时 dpkg 覆盖文件错误的解决办法

问题场景

Ubuntu 22.04系统中,原通过以下命令安装Node.js 12:

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

尝试升级到Node.js 18时,先执行sudo apt-get remove nodejs卸载旧版本,再配置Node 18源:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

执行sudo apt-get install -y nodejs时触发dpkg错误,错误信息:

After this operation, 184 MB of additional disk space will be used. Get:1 https://deb.nodesource.com/node_18.x jammy/main amd64 nodejs amd64 18.13.0-deb-1nodesource1 [28.4 MB] Fetched 28.4 MB in 44s (641 kB/s)                                               (Reading database ... 213677 files and directories currently installed.) Preparing to unpack .../nodejs_18.13.0-deb-1nodesource1_amd64.deb ... Unpacking nodejs (18.13.0-deb-1nodesource1) ... dpkg: error processing archive /var/cache/apt/archives/nodejs_18.13.0-deb-1nodesource1_amd64.deb (--unpack): trying to overwrite '/usr/include/node/common.gypi', which is also in package libnode-dev 12.22.9~dfsg-1ubuntu3 dpkg-deb: error: paste subprocess was killed by signal (Broken pipe) Errors were encountered while processing: /var/cache/apt/archives/nodejs_18.13.0-deb-1nodesource1_amd64.deb E: Sub-process /usr/bin/dpkg returned an error code (1)

错误原因

旧版本的libnode-dev包未完全卸载,其包含的/usr/include/node/common.gypi文件与Node.js 18安装包中的同名文件冲突,导致dpkg拒绝覆盖文件,安装失败。

解决步骤

  • 彻底卸载所有旧版Node.js相关包(包括开发依赖):
    sudo apt-get purge -y nodejs libnode-dev libnode72
    
  • 清理系统残留的依赖和apt缓存:
    sudo apt-get autoremove -y && sudo apt-get clean
    
  • 重新配置Node.js 18的软件源:
    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    
  • 安装Node.js 18:
    sudo apt-get install -y nodejs
    
  • 验证安装结果:
    node -v
    npm -v
    

内容的提问来源于stack exchange,提问作者bottles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 16:31:16