如何将nodeJS更新到最新版本?

我在Ubuntu上安装了nodeJS,代码如下

sudo apt-get install nodejs 

由于我是ubuntu的新用户,我也运行了这个代码

 sudo apt-get install npm 

现在当我输入

  nodejs --version 

表明

 v0.6.19 

我检查并看到最新的nodeJS版本是0.10.26

如何将0.10.26版本更新为0.10.26

我试过了

  sudo apt-get install  sudo apt-get install --only-upgrade  

但没有运气。

从npm使用n模块以升级节点

 sudo npm cache clean -f sudo npm install -gn sudo n stable 

要升级到最新版本(而不是当前稳定版),您可以使用

 sudo n latest 

撤销:

 sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/node sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -gn 

发现于David Walsh博客http://davidwalsh.name/upgrade-nodejs

Nodesource已经在此处上传了完整的安装说明。 它复制在下面供您参考。 更新到最新版本的说明相同。

Node.js v10.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 6.x的信息。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_10.x | bash - apt-get install -y nodejs 

Node.js v9.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 6.x的信息。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_9.x | bash - apt-get install -y nodejs 

Node.js v8.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 6.x的信息。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_8.x | bash - apt-get install -y nodejs 

Node.js v7.x

注意:此版本提供Debian Wheezy和Ubuntu Precise软件包。 请参考在较旧的发行版上运行Node.js> = 4.x.

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_7.x | bash - apt-get install -y nodejs 

Node.js v6.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 4.x的信息 。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_6.x | bash - apt-get install -y nodejs 

Node.js v5.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 4.x的信息 。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_5.x | bash - apt-get install -y nodejs 

Node.js v4.x

注意:如果您使用的是Ubuntu Precise或Debian Wheezy,您可能需要阅读有关在较旧的发行版上运行Node.js> = 4.x的信息 。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_4.x | bash - apt-get install -y nodejs 

Node.js v0.12

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_0.12 | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_0.12 | bash - apt-get install -y nodejs 

Node.js v0.10

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_0.10 | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_0.10 | bash - apt-get install -y nodejs 

io.js v3.x

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_iojs_3.x | sudo -E bash - sudo apt-get install -y iojs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_iojs_3.x | bash - apt-get install -y iojs 

io.js v2.x

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_iojs_2.x | sudo -E bash - sudo apt-get install -y iojs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_iojs_2.x | bash - apt-get install -y iojs 

io.js v1.x

注意:io.js的这个分支没有主动维护,不建议用于生产。

 # Using Ubuntu curl -sL https://deb.nodesource.com/setup_iojs_1.x | sudo -E bash - sudo apt-get install -y iojs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_iojs_1.x | bash - apt-get install -y iojs 

该PPA已过期,不再维护; 你应该考虑其他答案而不是这个答案。

您可以从PPA安装最新版本:

 sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs 

我使用NVM来处理我的Node版本。 设置简单,易于使用。

 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm 

要改为全局安装NVM,请使用以下curl命令而不是上面的命令(并且可能不使用第二个命令但使用第三个命令)

 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | NVM_DIR=/usr/local/nvm bash 

然后使用nvm install stable (或插入版本号而不是stable )来获取最新的/特定版本的Node。 使用nvm use stable (或特定版本号)来使用该Node版本。 使用nvm ls查看已安装的Node版本和nvm uninstall stable (或特定版本号)以删除特定版本的Node。

来源: 安装 , 使用

我还建议使用nvm,并删除已安装的版本以避免终端中的冲突

 sudo apt purge nodejs npm 

然后安装nvm并使用它

video说明

 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash 

下载并安装nvm

 nvm install node 

应该下载并安装最新版本的节点。

稍后再更新节点

 nvm install node nvm alias default node 

我在我的ubuntu 14.04上尝试了相同的列表命令,但它仍然抛出错误。

我执行的命令是:

 sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs 

和我得到的错误:

 Invalid version 0.12.2 Line 299: curl not found in /bin/n 

我想到的是我的操作系统上没有安装curl实用程序。

我执行了命令:

 apt-get install curl 

(如果你不是su使用sudo作为前缀)

然后重复步骤建议在答案中,它的工作;)

这个从nodesource安装最新的节点v0.12。*。

 sudo apt-get install -y curl curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash - sudo apt-get install -y nodejs 

对于节点v4.x

 curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs 

我建议您首先删除所有nodejs安装,然后在使用所需的nodejs版本及其安装父目录进行编辑后执行以下脚本

查看所有可用的nodejs版本

https://nodejs.org/dist/

下面的脚本将允许您在Linux或OSX上安装任何这些nodejs版本

 #!/bin/bash # usage : # # edit two vars in below : NODE_VER and CODE_PARENT_DIR # # ... execute this script as yourself unless you choose a root owned value for var CODE_PARENT_DIR # whichever id you execute this as determines the id you will issue npm commands as : npm install -g foo-bar # # NOTE - nodejs comes bundled with npm ... so no need to do separate npm install # this scripts runs fine on linux or OSX # ... copy all the lines starting here .. top of copy .... and ending ... end of copy ... # and paste into your ~/.bashrc file so proper env vars get set # ............... top of copy ........................ install_node.sh # export NODE_VER=v7.2.0 # see available versions at https://nodejs.org/dist/ # export NODE_VER=v8.5.0 # edit this line next time you need to update nodejs export NODE_VER=v9.3.0 # edit this line next time you need to update nodejs # ... pick parent dir of nodejs install ... comment out or remove ONE of below # export CODE_PARENT_DIR=/opt/code # root owned dir ... requires you to sudo prior to npm install going forward export CODE_PARENT_DIR=${HOME} # RECOMMENDED execute as yourself including npm install # ......... following env vars are OK no edits needed ... only ever need to edit above vars curr_OS=$( uname ) echo curr_OS $curr_OS if [[ "${curr_OS}" == "Darwin" ]]; then OS_ARCH=darwin-x64 elif [[ "${curr_OS}" == "Linux" ]]; then OS_ARCH=linux-x64 else echo "ERROR - failed to recognize OS $curr_OS" exit 5 fi if [[ -z ${CODE_PARENT_DIR} ]]; then echo "ERROR - failed to see env var CODE_PARENT_DIR" exit 5 fi export NODE_CODEDIR=${CODE_PARENT_DIR}/nodejs export COMSUFFIX=tar.gz export NODE_NAME=node-${NODE_VER} export NODE_PARENT=${NODE_CODEDIR}/${NODE_NAME}-${OS_ARCH} export PATH=${NODE_PARENT}/bin:${PATH} export NODE_PATH=${NODE_PARENT}/lib/node_modules # ............... end of copy ........................ install_node.sh # copy and paste above from ... top of copy ... to here into your file ~/.bashrc echo echo "NODE_CODEDIR $NODE_CODEDIR<--" echo echo "mkdir -p ${NODE_CODEDIR}" echo mkdir -p ${NODE_CODEDIR} echo echo "cd ${NODE_CODEDIR}" cd ${NODE_CODEDIR} echo # this is compiled code NOT source [ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove echo "wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}" wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} echo echo "tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}" tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} echo [ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove # ........... done ........... # which node node --version # .... bottom of file install_node.sh 

如果使用n不起作用,则可以安装运行以下命令的最新版本的nodejs (即版本8):

 curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs 

更多说明在这里 。

如果你在代理后面,也许你在运行’sudo n stable’时会遇到这个错误:

 Error: invalid version 

你必须像这样设置env变量:

 export HTTP_PROXY=http://your-proxy-url:port export HTTPS_PROXY=http://your-proxy-url:port 

然后运行命令将这些变量传递给root用户:

 sudo -E n stable 

使用nvm是首选方法。 首先安装nvm:

 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash 

然后安装nodejs:

 nvm install node 

现在,您可以轻松切换节点版本。