zoukankan      html  css  js  c++  java
  • Ubuntu中安装最新 Node.js 和 npm

    背景

    为了安装GNU QEMU Eclipse,需要安装比较新的nodejs

    $ npm install --global xpm@latest
    ▌ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
    WARN engine xpm@0.5.0: wanted: {"node":">7.7.0"} (current: {"node":"4.2.6","npm":"3.5.2"})
    
    

    参考:Ubuntu18安装最新版nodejsUbuntu16.04安装最新版nodejs

    更新ubuntu软件源

    # Ubuntu 16.04 TLS,执行以下命令:
    sudo apt-get update
    sudo apt-get install -y python-software-properties software-properties-common
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get update
    
    
    # Ubuntu 18.04 TLS,执行以下命令:
    sudo apt-get update
    sudo apt-get install -y software-properties-common
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get update
    

    安装NodeJS和NPM

    安装命令

    # Ubuntu 16.04 TLS,执行以下命令:
    sudo apt-get install nodejs
    sudo apt install nodejs-legacy
    sudo apt install npm
    
    
    # Ubuntu 18.04 TLS,执行以下命令:
    sudo apt-get install nodejs
    sudo apt install libssl1.0-dev nodejs-dev node-gyp npm
    

    更新npm的包镜像源,方便快速下载

    sudo npm config set registry https://registry.npm.taobao.org
    sudo npm config list
    

    安装n管理器(用于管理nodejs版本)

    sudo npm install n -g
    
    # 安装最新的nodejs(stable版本)
    sudo n stable
    

    验证安装

    使用下面的命令查看NodeJS和NPM的版本,若正确安装的话都可以看到相关的版本号信息。

    sudo node -v
    sudo npm -v
    

    安装完最好重启一下:sudo reboot或者重启shell

    Q & A

    验证安装查看的版本号时,加sudo和不加sudo的命令,结果不一样

    这应该是终端或远程连接终端的缓存问题,加sudo命令看到的是最新的版本号,不加sudo命令看到的是前一安装版本的版本号,关闭相关终端或远程连接终端,然后重新连接即可解决这个问题。

    apt-get update找不到文件

    // error code
    W: The repository 'http://ppa.launchpad.net/chris-lea/node.js/ubuntu xenial Release' does not have a Release file.
    N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
    N: See apt-secure(8) manpage for repository creation and user configuration details.
    E: Failed to fetch http://ppa.launchpad.net/chris-lea/node.js/ubuntu/dists/xenial/main/binary-amd64/Packages  404  Not Found
    E: Some index files failed to download. They have been ignored, or old ones used instead.
    
    

    // A: 修改node相关文件:/etc/apt/sources.list.d/chris-lea-ubuntu-node_js-xenial.list
    deb http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main
    deb-src http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main

    node -v异常

    node -v
    -bash: /usr/local/bin/node: Permission denied
    
    sudo node -v
    Segmentation fault (core dumped)
    

    // A: 重新安装nodejs
    手动移除 /usr/local/lib/node_modules整个目录
    手动移除 /usr/local/bin/node
    手动移除 /usr/local/bin/n
    手动移除 /usr/local/bin/npm

    Package 'software-properties-common' has no installation candidate异常

    sudo apt-get install -y python-software-properties software-properties-common
    
    E: Package 'python-software-properties' has no installation candidate
    

    原因详见 https://askubuntu.com/questions/422975/e-package-python-software-properties-has-no-installation-candidate?rq=1 ,调整为sudo apt-get install -y software-properties-common即可

  • 相关阅读:
    9.13 h5日记
    9.12 h5日记
    9.11 h5日记
    9.10 h5日记
    H5笔记周记
    ASP.NET-GridView之表头设计
    论执行力
    BS总结篇­
    花样年纪的记录(一)
    Nginx+ISS+Redis实现完美负载均衡
  • 原文地址:https://www.cnblogs.com/schips/p/12402412.html
Copyright © 2011-2022 走看看