Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
Node.js是用于服务器端编程JavaScript运行时。 它允许开发人员使用JavaScript创建可扩展的后端功能,JavaScript是许多基于浏览器的Web开发已经熟悉的语言。
In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:
在本指南中,我们将向您展示在Ubuntu 20.04服务器上安装Node.js的三种不同方式:
using apt
to install the nodejs
package from Ubuntu’s default software repository
使用apt
从Ubuntu的默认软件存储库安装nodejs
软件包
using apt
with an alternate PPA software repository to install specific versions of the nodejs
package
结合使用apt
和其他PPA软件存储库来安装nodejs
软件包的特定版本
installing nvm
, the Node Version Manager, and using it to install and manage multiple versions of Node.js
安装nvm
,节点版本管理器,并使用它来安装和管理Node.js的多个版本
For many users, using apt
with the default repo will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node
versions frequently, choose the nvm
method.
对于许多用户而言,将apt
与默认存储库配合使用就足够了。 如果需要特定的较新(或旧版)的Node版本,则应使用PPA存储库。 如果您正在积极开发Node应用程序,并且需要经常在node
版本之间切换,请选择nvm
方法。
This guide assumes that you are using Ubuntu 20.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the initial server setup tutorial for Ubuntu 20.04.
本指南假定您正在使用Ubuntu 20.04。 在开始之前,您应该在系统上设置具有sudo特权的非root用户帐户。 您可以通过遵循Ubuntu 20.04的初始服务器设置教程来学习如何执行此操作。
Ubuntu 20.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 10.19. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.
Ubuntu 20.04在其默认存储库中包含一个版本的Node.js,可用于在多个系统之间提供一致的体验。 在撰写本文时,存储库中的版本为10.19。 这不是最新版本,但应该稳定并且足以快速试用该语言。
To get this version, you can use the apt
package manager. Refresh your local package index first by typing:
要获得此版本,您可以使用apt
软件包管理器。 首先输入以下内容刷新本地软件包索引:
Then install Node.js:
然后安装Node.js:
Check that the install was successful by querying node
for its version number:
通过查询node
的版本号来检查安装是否成功:
Output
v10.19.0
If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to also install npm
, the Node.js package manager. You can do this by installing the npm
package with apt
:
如果存储库中的软件包适合您的需求,那么这就是设置Node.js所需要做的全部工作。 在大多数情况下,您还需要安装Node.js软件包管理器npm
。 您可以通过使用apt
安装npm
软件包来做到这一点:
This will allow you to install modules and packages to use with Node.js.
这将允许您安装要与Node.js一起使用的模块和软件包。
At this point you have successfully installed Node.js and npm
using apt
and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.
至此,您已经使用apt
和默认的Ubuntu软件存储库成功安装了Node.js和npm
。 下一节将显示如何使用备用存储库来安装不同版本的Node.js。
To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v10, v12, v13, and v14 are available as of the time of writing.
要安装其他版本的Node.js,可以使用NodeSource维护的PPA (个人软件包存档)。 这些PPA具有比官方Ubuntu存储库更多的Node.js版本。 在撰写本文时,Node.js v10,v12,v13和v14可用。
First, we will install the PPA in order to get access to its packages. From your home directory, use curl
to retrieve the installation script for your preferred version, making sure to replace 14.x
with your preferred version string (if different).
首先,我们将安装PPA以便访问其软件包。 从主目录中,使用curl
来获取首选版本的安装脚本,并确保将14.x
替换为首选版本字符串(如果有)。
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
curl -sL https://deb.nodesource.com/setup_ 14.x -o nodesource_setup.sh
Refer to the NodeSource documentation for more information on the available versions.
有关可用版本的更多信息,请参考NodeSource文档 。
Inspect the contents of the downloaded script with nano
(or your preferred text editor):
使用nano
(或您喜欢的文本编辑器)检查下载脚本的内容:
When you are satisfied that the script is safe to run, exit your editor, then run the script with sudo
:
如果您满意脚本可以安全运行,请退出编辑器,然后使用sudo
运行脚本:
The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section:
PPA将添加到您的配置中,并且本地软件包缓存将自动更新。 现在,您可以按照上一节中的相同方式安装Node.js软件包:
Verify that you’ve installed the new version by running node
with the -v
version flag:
通过运行带有-v
version标志的node
验证是否已安装新版本:
Output
v14.2.0
The NodeSource nodejs
package contains both the node
binary and npm
, so you don’t need to install npm
separately.
NodeSource nodejs
软件包同时包含node
二进制文件和npm
,因此您无需单独安装npm
。
At this point you have successfully installed Node.js and npm
using apt
and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.
至此,您已经使用apt
和NodeSource PPA成功安装了Node.js和npm
。 下一节将显示如何使用节点版本管理器来安装和管理Node.js的多个版本。
Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.
安装Node.js的另一种特别灵活的方法是使用nvm(节点版本管理器)。 通过该软件,您可以同时安装和维护许多不同的独立版本的Node.js及其关联的Node软件包。
To install NVM on your Ubuntu 20.04 machine, visit the project’s GitHub page. Copy the curl
command from the README file that displays on the main page. This will get you the most recent version of the installation script.
要在Ubuntu 20.04计算机上安装NVM,请访问项目的GitHub页面 。 从主页上显示的README文件中复制curl
命令。 这将为您提供最新版本的安装脚本。
Before piping the command through to bash
, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash
segment at the end of the curl
command:
在将命令传递到bash
,最好审核一下脚本以确保它没有执行您不同意的任何事情。 您可以通过删除| bash
curl
命令末尾的| bash
段:
Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash
appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:
看一看,确保对它所做的更改感到满意。 如果满意,请再次使用| bash
运行命令| bash
| bash
附加在末尾。 您使用的URL将根据nvm的最新版本而变化,但是从现在开始,可以通过键入以下内容来下载和执行脚本:
This will install the nvm
script to your user account. To use it, you must first source your .bashrc
file:
这会将nvm
脚本安装到您的用户帐户。 要使用它,必须首先获取.bashrc
文件:
Now, you can ask NVM which versions of Node are available:
现在,您可以询问NVM哪些版本的Node可用:
Output
. . .
v12.13.0 (LTS: Erbium)
v12.13.1 (LTS: Erbium)
v12.14.0 (LTS: Erbium)
v12.14.1 (LTS: Erbium)
v12.15.0 (LTS: Erbium)
v12.16.0 (LTS: Erbium)
v12.16.1 (LTS: Erbium)
v12.16.2 (LTS: Erbium)
v12.16.3 (Latest LTS: Erbium)
v13.0.0
v13.0.1
v13.1.0
v13.2.0
v13.3.0
v13.4.0
v13.5.0
v13.6.0
v13.7.0
v13.8.0
v13.9.0
v13.10.0
v13.10.1
v13.11.0
v13.12.0
v13.13.0
v13.14.0
v14.0.0
v14.1.0
v14.2.0
It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v13.6.0, you can type:
这是一个很长的清单! 您可以通过键入看到的任何发行版本来安装Node版本。 例如,要获取版本v13.6.0,可以输入:
You can see the different versions you have installed by typing:
您可以通过键入以下命令查看已安装的不同版本:
nvm list
Output
-> v13.6.0
default -> v13.6.0
node -> stable (-> v13.6.0) (default)
stable -> 13.6 (-> v13.6.0) (default)
. . .
This shows the currently active version on the first line (-> v13.6.0
), followed by some named aliases and the versions that those aliases point to.
这会在第一行( -> v13.6.0
)上显示当前处于活动状态的版本,然后是一些已命名的别名以及这些别名指向的版本。
Note: if you also have a version of Node.js installed through apt
, you may see a system
entry here. You can always activate the system-installed version of Node using nvm use system
.
注意:如果您还通过apt
安装了Node.js版本,则可能会在此处看到system
条目。 您始终可以使用nvm use system
激活系统安装的Node版本。
Additionally, you’ll see aliases for the various long-term support (or LTS) releases of Node:
此外,您还将看到Node的各种长期支持(或LTS)版本的别名:
Output
. . .
lts/* -> lts/erbium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.20.1 (-> N/A)
lts/erbium -> v12.16.3 (-> N/A)
We can install a release based on these aliases as well. For instance, to install the latest long-term support version, erbium
, run the following:
我们也可以基于这些别名安装发行版。 例如,要安装最新的长期支持版本erbium
,请运行以下命令:
Output
Downloading and installing node v12.16.3...
. . .
Now using node v12.16.3 (npm v6.14.4)
You can switch between installed versions with nvm use
:
您可以使用nvm use
在已安装的版本之间切换:
nvm use v13.6.0
Now using node v13.6.0 (npm v6.13.4)
You can verify that the install was successful using the same technique from the other sections, by typing:
您可以通过输入以下内容使用其他部分中的相同技术来验证安装是否成功:
Output
v13.6.0
The correct version of Node is installed on our machine as we expected. A compatible version of npm
is also available.
如我们所料,正确版本的Node已安装在我们的计算机上。 还提供了兼容版本的npm
。
There are a quite a few ways to get up and running with Node.js on your Ubuntu 20.04 server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Ubuntu’s repository is the easiest method, using nvm
or a NodeSource PPA offers additional flexibility.
在Ubuntu 20.04服务器上有很多方法可以启动并运行Node.js。 您的情况将决定上述哪种方法最适合您的需求。 虽然在Ubuntu的存储库中使用打包版本是最简单的方法,但使用nvm
或NodeSource PPA可以提供更多的灵活性。
For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.
有关使用Node.js进行编程的更多信息,请参阅我们的教程系列如何在Node.js中进行编码 。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04