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

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

sudo apt-get install nodejs

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

sudo apt-get install npm

现在当我打字的时候

 nodejs --version

它显示

v0.6.19

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

如何将我的nodeJS版本更新为 0.10.26?

我试过了

 sudo apt-get install <packagename> sudo apt-get install --only-upgrade <packagename>

但运气不好。

使用方法 来自npm的n模块 为了升级节点

sudo npm cache clean -fsudo npm install -g nsudo 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 -g n

您可能需要重新启动终端才能看到更新的节点版本。

发现于 大卫*沃尔什博客

完整的安装说明已上载 这里 通过Nodesource。 它复制在下面供您参考。 更新到最新版本的说明相同。

如果你不喜欢 curl ... | sudo bash - 然后你可以按照 手册说明,它告诉你如何添加节点。js仓库到 /etc/apt/sources.list.d/ 你自己。

注意:如果您使用的是Ubuntu精确或Debian Wheezy,您可能需要阅读 运行节点。js>=6.旧发行版上的x

节点。js当前:

# Using Ubuntucurl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -sudo apt-get install -y nodejs# Using Debian, as rootcurl -fsSL https://deb.nodesource.com/setup_current.x | bash -apt-get install -y nodejs

节点。js v13.x:

# Using Ubuntucurl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -sudo apt-get install -y nodejs# Using Debian, as rootcurl -sL https://deb.nodesource.com/setup_13.x | bash -apt-get install -y nodejs

节点。js v12.x:

# Using Ubuntucurl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -sudo apt-get install -y nodejs# Using Debian, as rootcurl -sL https://deb.nodesource.com/setup_12.x | bash -apt-get install -y nodejs

节点。js v11.x:

# Using Ubuntucurl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -sudo apt-get install -y nodejs# Using Debian, as rootcurl -sL https://deb.nodesource.com/setup_11.x | bash -apt-get install -y nodejs

节点。js v10.x:

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

节点。js v9.x:

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

节点。js v8.x:

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

节点。js v7.x:

注意:Debian Wheezy和Ubuntu精确软件包是 不是 可用于此版本。 请参考 运行节点。js>=4.旧发行版上的x

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

节点。js v6.x:

注意:如果您使用的是Ubuntu精确或Debian Wheezy,您可能需要阅读 运行节点。js>=4.旧发行版上的x.

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

节点。js v5.x:

注意:如果您使用的是Ubuntu精确或Debian Wheezy,您可能需要阅读 运行节点。js>=4.旧发行版上的x.

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

节点。js v4.x:

注意:如果您使用的是Ubuntu精确或Debian Wheezy,您可能需要阅读 运行节点。js>=4.旧发行版上的x.

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

节点。js v0.12:

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

节点。js v0.10:

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

伊俄js v3.x:

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

伊俄js v2.x:

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

伊俄js v1.x:

注意:io的这个分支。js没有积极维护,不建议用于生产使用。

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

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

sudo apt purge nodejs npm

然后安装nvm并使用它

视频讲解

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

下载并安装nvm

nvm install node

应下载并安装最新版本的node。

稍后更新节点只需

nvm install nodenvm alias default node

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

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

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

我使用NVM来处理我的节点版本。 非常简单的设置和易于使用。

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bashexport 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)以获取最新/特定版本的节点。 使用方法 nvm use stable (或特定版本号)来使用该节点版本。 使用方法 nvm ls 要查看已安装的节点版本和 nvm uninstall stable(或特定版本号)来移除特定版本的节点。

资料来源:安装/安装, 使用方法

注意事项

如果您正在努力更新 npm 到最新的稳定版本,因为你被困在一个特定的版本 npm 每次你更新它不起作用,那么你可以使用这种方法来更新 npm ""好吧。 只要您安装最新的稳定版本 node, npm 将自动更新到其最新的稳定版本。

Nvm(节点版本管理器)与 --lts

NVM在: https://askubuntu.com/a/441527/52975 但这里有一个更完整的用法示例,包括sane --lts 版本,它安装最新的长期支持节点版本,如果您没有更具体的要求,它可能是您想要的版本。

安装NVM和最新的节点LTS版本:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | shsource ~/.nvm/nvm.shnvm install --ltsnvm use --ltsnpm --version

现在用一个虚拟包测试它:

npm install --global vacavaca

由于采购必须为每个新的shell完成,因此安装脚本黑客在您的 .barshrc. 这有效,但我更喜欢删除自动添加的一个并添加我自己的:

f="$HOME/.nvm/nvm.sh"if [ -r "$f" ]; then  . "$f" &>'/dev/null'  nvm use --lts &>'/dev/null'fi

优势:

  • 允许您使用多个版本的节点和没有sudo

  • 类似于Ruby RVM和Python Virtualenv,在Ruby和Python社区中被广泛认为是最佳实践

  • 在可能的情况下下载预编译的二进制文件,如果没有,它会下载源代码并为您编译一个

我们可以很容易地切换节点版本:

nvm install 0.9.0nvm install 0.9.9nvm use 0.9.0node --version#v0.9.0nvm use 0.9.9node --version#v0.9.9

然后,您可以使用git跟踪 .nvmrc 用于指示给定项目所需的节点版本的文件: https://stackoverflow.com/questions/24869959/how-do-i-specify-a-local-version-of-node-for-a-project/54503474#54503474

有了这个设置,你会得到例如:

which node

给予:

/home/ciro/.nvm/versions/node/v0.9.0/bin/node

和:

which vaca

给予:

/home/ciro/.nvm/versions/node/v0.9.0/bin/vaca

如果我们想使用全局安装的模块:

npm link vacanode -e 'console.log(require.resolve("vaca"))'

给予:

/home/ciro/.nvm/versions/node/v0.9.0/lib/node_modules/vaca/index.js

所以我们看到所有的东西都完全包含在特定的节点版本中。

在Ubuntu17.10中测试。

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

我执行的命令是:

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

我得到的错误:

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

我发现curl实用程序没有安装在我的操作系统上。

我执行了命令:

apt-get install curl

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

然后重复回答中建议的步骤,它就工作了;)

这一个安装最新的节点v0.12。*来自nodesource。

sudo apt-get install -y curlcurl -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

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

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

进一步的说明是 这里.

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

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

然后安装nodejs:

nvm install node

现在,您可以轻松切换node的版本。

最好使用nvm安装nodejs。 它代表"节点版本管理器"。 您可以非常轻松地安装多个版本的nodejs,在它们之间切换并设置默认值。