Node.js is a JavaScript platform for general-purpose programming that allows users to build network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.
Node.js是用于通用编程JavaScript平台,它使用户可以快速构建网络应用程序。 通过在前端和后端同时利用JavaScript,Node.js使开发更加一致和集成。
In this guide, we’ll show you how to get started with Node.js on an Ubuntu 18.04 server.
在本指南中,我们将向您展示如何在Ubuntu 18.04服务器上开始使用Node.js。
This guide assumes that you are using Ubuntu 18.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 18.04.
本指南假定您正在使用Ubuntu 18.04。 在开始之前,您应该在系统上设置具有sudo特权的非root用户帐户。 您可以按照Ubuntu 18.04的初始服务器设置教程来学习如何执行此操作。
Ubuntu 18.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 8.10.0. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.
Ubuntu 18.04在其默认存储库中包含一个版本的Node.js,可用于在多个系统之间提供一致的体验。 在撰写本文时,存储库中的版本为8.10.0。 这不是最新版本,但应该稳定并且足以快速试用该语言。
To get this version, you can use the apt
package manager. Refresh your local package index by typing:
要获得此版本,您可以使用apt
软件包管理器。 通过输入以下内容刷新本地软件包索引:
Install Node.js from the repositories:
从存储库安装Node.js:
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 install npm
, the Node.js package manager. You can do this by typing:
如果存储库中的软件包适合您的需求,那么这就是设置Node.js所需要做的全部工作。 在大多数情况下,您还需要安装Node.js软件包管理器npm
。 您可以通过键入以下内容来执行此操作:
This will allow you to install modules and packages to use with Node.js.
这将允许您安装要与Node.js一起使用的模块和软件包。
Because of a conflict with another package, the executable from the Ubuntu repositories is called nodejs
instead of node
. Keep this in mind as you are running software.
由于与另一个软件包的冲突,Ubuntu存储库中的可执行文件称为nodejs
而不是node
。 在运行软件时,请记住这一点。
To check which version of Node.js you have installed after these initial steps, type:
要检查在完成这些初始步骤后已安装的Node.js版本,请输入:
Once you have established which version of Node.js you have installed from the Ubuntu repositories, you can decide whether or not you would like to work with different versions, package archives, or version managers. Next, we’ll discuss these elements, along with more flexible and robust methods of installation.
一旦确定了从Ubuntu存储库中安装了哪个版本的Node.js,就可以决定是否要使用其他版本,程序包归档文件或版本管理器。 接下来,我们将讨论这些元素以及更灵活,更强大的安装方法。
To get a more recent version of Node.js you can add the PPA (personal package archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official Ubuntu repositories, and will allow you to choose between Node.js v10.x (the Maintenance LTS version, supported until April of 2021), Node.js v12.x (the Active LTS version, supported until April 2022), and Node.js v14.x (the current release, supported until April 2023).
要获取最新版本的Node.js,您可以添加NodeSource维护的PPA (个人软件包存档)。 与官方的Ubuntu存储库相比,它将具有更多的Node.js版本,并允许您在Node.js v10.x(维护LTS版本,支持到2021年4月)之间进行选择。 .x(Active LTS版本,支持到2022年4月),以及Node.js v14.x(当前版本,支持到2023年4月)。
First, install the PPA in order to get access to its contents. From your home directory, use curl
to retrieve the installation script for your preferred version, making sure to replace 10.x
with your preferred version string (if different):
首先,安装PPA,以访问其内容。 从主目录中,使用curl
来获取首选版本的安装脚本,并确保将10.x
替换为首选版本字符串(如果有):
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
curl -sL https://deb.nodesource.com/setup_ 10.x -o nodesource_setup.sh
You can inspect the contents of this script with nano
(or your preferred text editor):
您可以使用nano
(或您喜欢的文本编辑器)检查此脚本的内容:
Run the script under sudo
:
在sudo
下运行脚本:
The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from Nodesource, you can install the Node.js package in the same way you did above:
PPA将添加到您的配置中,并且本地软件包缓存将自动更新。 从Nodesource运行安装脚本之后,可以按照与上面相同的方式安装Node.js软件包:
To check which version of Node.js you have installed after these initial steps, type:
要检查在完成这些初始步骤后已安装的Node.js版本,请输入:
Output
v10.22.0
The nodejs
package contains the nodejs
binary as well as npm
, so you don’t need to install npm
separately.
nodejs
软件包包含nodejs
二进制文件以及npm
,因此您不需要单独安装npm
。
npm
uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm
. Execute this command to verify that npm
is installed and to create the configuration file:
npm
使用主目录中的配置文件来跟踪更新。 它将在您第一次运行npm
时创建。 执行以下命令以验证是否已安装npm
并创建配置文件:
Output
6.14.6
In order for some npm
packages to work (those that require compiling code from source, for example), you will need to install the build-essential
package:
为了使某些npm
软件包(例如那些需要从源代码编译代码的软件包)起作用,您将需要安装build-essential
软件包:
You now have the necessary tools to work with npm
packages that require compiling code from source.
现在,您具有必要的工具来处理需要从源代码编译代码的npm
软件包。
An alternative to installing Node.js with apt
is to use a tool called nvm
, which stands for “Node.js Version Manager”. Rather than working at the operating system level, nvm
works at the level of an independent directory within your home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.
使用apt
安装Node.js的替代方法是使用一个名为nvm
的工具,该工具代表“ Node.js版本管理器”。 nvm
不是在操作系统级别工作, nvm
在主目录内的独立目录级别工作。 这意味着您可以安装多个独立的Node.js版本,而不会影响整个系统。
Controlling your environment with nvm
allows you to access the newest versions of Node.js and retain and manage previous releases. It is a different utility from apt
, however, and the versions of Node.js that you manage with it are distinct from the versions you manage with apt
.
使用nvm
控制环境可让您访问Node.js的最新版本,并保留和管理以前的版本。 但是,它与apt
是不同的实用程序,并且您管理的Node.js版本与apt
管理的版本不同。
To download the nvm
installation script from the project’s GitHub page, you can use curl
. Note that the version number may differ from what is highlighted here:
要从项目的GitHub页面下载nvm
安装脚本,可以使用curl
。 请注意,版本号可能与此处突出显示的版本号不同:
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o install_nvm.sh
curl -sL https://raw.githubusercontent.com/creationix/nvm/ v0.35.3 /install.sh -o install_nvm.sh
Inspect the installation script with nano
:
使用nano
检查安装脚本:
Run the script with bash
:
使用bash
运行脚本:
It will install the software into a subdirectory of your home directory at ~/.nvm
. It will also add the necessary lines to your ~/.profile
file to use the file.
它将把软件安装到您的主目录的子目录~/.nvm
。 还将在~/.profile
文件中添加必要的行以使用该文件。
To gain access to the nvm
functionality, you’ll need to either log out and log back in again or source the ~/.profile
file so that your current session knows about the changes:
要获得对nvm
功能的访问权限,您需要注销并再次登录,或获取~/.profile
文件,以便您的当前会话了解更改:
With nvm
installed, you can install isolated Node.js versions. For information about the versions of Node.js that are available, type:
安装nvm
,您可以安装隔离的Node.js版本。 有关可用的Node.js版本的信息,请键入:
Output
...
v12.18.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
v14.3.0
v14.4.0
v14.5.0
v14.6.0
v14.7.0
As you can see, the current LTS version at the time of this writing is v12.18.3. You can install that by typing:
如您所见,在撰写本文时,当前的LTS版本是v12.18.3。 您可以通过键入以下内容进行安装:
nvm install 12.18.3
NVM安装12.18.3
Usually, nvm
will switch to use the most recently installed version. You can tell nvm
to use the version you just downloaded by typing:
通常, nvm
会切换为使用最新安装的版本。 您可以通过键入以下命令告诉nvm
使用刚刚下载的版本:
nvm use 12.18.3
NVM使用12.18.3
When you install Node.js using nvm
, the executable is called node
. You can see the version currently being used by the shell by typing:
使用nvm
安装Node.js时,可执行文件称为node
。 您可以通过键入以下命令查看外壳当前正在使用的版本:
Output
v12.18.3
If you have multiple Node.js versions, you can see what is installed by typing:
如果您有多个Node.js版本,则可以通过键入以下内容查看安装的内容:
If you wish to default one of the versions, type:
如果您希望默认使用其中一个版本,请键入:
nvm alias default 12.18.3
NVM别名默认12.18.3
This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:
产生新会话时,将自动选择此版本。 您也可以通过如下别名来引用它:
Each version of Node.js will keep track of its own packages and has npm
available to manage these.
每个版本的Node.js都将跟踪自己的软件包,并提供npm
来管理它们。
You can also have npm
install packages to the Node.js project’s ./node_modules
directory. Use the following syntax to install the express
module:
您还可以将npm
install软件包安装到Node.js项目的./node_modules
目录中。 使用以下语法来安装express
模块:
npm install express
npm安装快递
If you’d like to install the module globally, making it available to other projects using the same version of Node.js, you can add the -g
flag:
如果您想在全球范围内安装该模块,以使该模块可用于使用相同版本的Node.js的其他项目,则可以添加-g
标志:
npm install -g express
npm install -g 快递
This will install the package in:
这会将软件包安装在:
~/.nvm/versions/node/12.18.3/lib/node_modules/express
Installing the module globally will let you run commands from the command line, but you’ll have to link the package into your local sphere to require it from within a program:
在全局安装模块将使您可以从命令行运行命令,但是必须将程序包链接到本地范围,才能在程序中要求它:
npm link express
npm链接快递
You can learn more about the options available to you with nvm
by typing:
您可以通过键入以下内容来了解有关nvm
可用选项的更多信息:
You can uninstall Node.js using apt
or nvm
, depending on the version you want to target. To remove the distro-stable version, you will need to work with the apt
utility at the system level.
您可以使用apt
或nvm
卸载Node.js,具体取决于要定位的版本。 要删除发行版不稳定的版本,您将需要在系统级别使用apt
实用程序。
To remove the distro-stable version, type the following:
要删除发行版稳定的版本,请键入以下内容:
This command will remove the package and retain the configuration files. These may be of use to you if you intend to install the package again at a later point. If you don’t want to save the configuration files for later use, then run the following:
此命令将删除软件包并保留配置文件。 如果您打算以后再安装该软件包,这些可能对您有用。 如果您不想保存配置文件以备后用,请运行以下命令:
This will uninstall the package and remove the configuration files associated with it.
这将卸载软件包并删除与之关联的配置文件。
As a final step, you can remove any unused packages that were automatically installed with the removed package:
最后,您可以删除与删除的软件包一起自动安装的所有未使用的软件包:
To uninstall a version of Node.js that you have enabled using nvm
, first determine whether or not the version you would like to remove is the current active version:
要卸载使用nvm
启用的Node.js版本,请首先确定您要删除的版本是否为当前活动版本:
If the version you are targeting is not the current active version, you can run:
如果您定位的版本不是当前的活动版本,则可以运行:
nvm uninstall node_version
nvm卸载node_version
This command will uninstall the selected version of Node.js.
此命令将卸载选定版本的Node.js。
If the version you would like to remove is the current active version, you must first deactivate nvm
to enable your changes:
如果要删除的版本是当前活动版本,则必须首先停用nvm
以启用更改:
You can now uninstall the current version using the uninstall
command above, which will remove all files associated with the targeted version of Node.js except the cached files that can be used for reinstallment.
现在,您可以使用上面的uninstall
命令来卸载当前版本,该命令将删除与Node.js目标版本关联的所有文件,但可以用于重新安装的缓存文件除外。
There are quite a few ways to get up and running with Node.js on your Ubuntu 18.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
offers additional flexibility.
在Ubuntu 18.04服务器上有很多方法可以启动并运行Node.js。 您的情况将决定上述哪种方法最适合您的需求。 虽然在Ubuntu的存储库中使用打包版本是最简单的方法,但使用nvm
可以提供更多的灵活性。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04