如何在Debian 10上安装Node.js

介绍 (Introduction)

Node.js is a JavaScript platform for general-purpose programming that allows users to build asynchronous network applications quickly. By leveraging JavaScript on both the front and backend, Node.js can make web application development more consistent and integrated.

Node.js是一个用于通用编程JavaScript平台,允许用户快速构建异步网络应用程序。 通过在前端和后端使用JavaScript,Node.js可以使Web应用程序开发更加一致和集成。

In this guide, we’ll show you how to get started with Node.js on a Debian 10 server. We will discuss installing Node from the default Debian repository, using a more up-to-date PPA repository, and using NVM (Node Version Manager) to install and activate different versions of Node.

在本指南中,我们将向您展示如何在Debian 10服务器上开始使用Node.js。 我们将讨论从默认的Debian存储库安装Node,使用最新的PPA存储库以及使用NVM(节点版本管理器)安装和激活不同版本的Node。

Finally, we will show how to uninstall these different versions of Node.

最后,我们将展示如何卸载这些不同版本的Node。

先决条件 (Prerequisites)

This guide assumes that you are using Debian 10. Before you begin, you should have a non-root user with sudo privileges set up on your system. You can learn how to set this up by following the initial server setup for Debian 10 tutorial.

本指南假定您正在使用Debian10。开始之前,您应该在系统上设置具有sudo特权的非root用户。 您可以通过遵循Debian 10教程的初始服务器设置来学习如何进行设置。

安装官方的Debian Node.js软件包 (Installing the Official Debian Node.js Package)

Debian contains a version of Node.js in its default repositories. At the time of writing, this version is 10.15.2, which will reach end-of-life on April 1, 2021. At this date it will no longer be supported with security and bug fixes. If you would like to experiment with Node using an easy-to-install, stable, and long-term option, then installing from the Debian repo may make sense.

Debian在其默认存储库中包含一个版本的Node.js。 在撰写本文时,此版本为10.15.2,将于2021年4月1日终止使用。目前,它将不再受安全性和错误修复的支持。 如果您想使用易于安装,稳定且长期的选项尝试使用Node,那么从Debian存储库进行安装可能很有意义。

To get Node.js from the default Debian software repository, you can use the apt package manager. First, refresh your local package index:

要从默认的Debian软件存储库中获取Node.js,可以使用apt软件包管理器。 首先,刷新您的本地软件包索引:

  • sudo apt update

    sudo apt更新

Then install the Node.js package, and npm the Node Package Manager:

然后安装Node.js程序包,并安装npm Node Package Manager:

  • sudo apt install nodejs npm

    sudo apt安装nodejs npm

To verify that the install was successful, run the node command with the -v flag to get the version:

要验证安装是否成功,请运行带有-v标志的node命令以获取版本:

  • node -v

    节点-v

   
     
     
     
     
Output
v10.15.2

If you need a more recent version of Node.js than this, the next two sections will explain other installation options.

如果您需要比此版本更新的Node.js,接下来的两节将介绍其他安装选项。

使用PPA安装 (Installing Using a PPA)

To work with a more recent version of Node.js, you can install from a PPA (personal package archive) maintained by NodeSource. This is an alternate repository that still works with `apt, and will have more up-to-date versions of Node.js than the official Debian repositories. NodeSource has PPAs available for Node versions from 0.10 through to 12.

要使用最新版本的Node.js,可以从NodeSource维护的PPA (个人软件包归档文件)中进行安装 。 这是一个仍可与apt一起使用的备用存储库,并且比官方Debian存储库具有更多的Node.js版本。 NodeSource的PPA适用于从0.10到12的Node版本。

Let’s install the PPA now. This will add the repository to our package list and allow us to install the new packages using apt.

让我们现在安装PPA。 这会将存储库添加到我们的软件包列表中,并允许我们使用apt安装新软件包。

From your home directory, use curl to retrieve the installation script for your preferred Node.js version, making sure to replace 12.x with your preferred version string (if different):

在主目录中,使用curl来获取首选Node.js版本的安装脚本,并确保将12.x替换为首选版本字符串(如果不同):

  • cd ~

    光盘〜
  • curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh

    curl -sL https://deb.nodesource.com/setup_ 12.x -o nodesource_setup.sh

You can inspect the contents of this script with nano or your preferred text editor:

您可以使用nano或您喜欢的文本编辑器检查此脚本的内容:

  • nano nodesource_setup.sh

    纳米节点source_setup.sh

If everything looks OK, exit your text editor and run the script using sudo:

如果一切正常,请退出文本编辑器并使用sudo运行脚本:

  • sudo bash nodesource_setup.sh

    须藤bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. Now you can install the nodejs package in the same way you did in the previous step:

PPA将添加到您的配置中,并且本地软件包缓存将自动更新。 现在,您可以按照与上一步相同的方式安装nodejs软件包:

  • sudo apt install nodejs

    sudo apt安装nodejs

We don’t need to install a separate package for npm in this case, as it is included in the nodejs packae.

在这种情况下,我们不需要为npm安装单独的软件包,因为它包含在nodejs packae中。

Verify the installation by running node with the -v version option:

通过使用-v version选项运行node验证安装:

  • node -v

    节点-v

   
     
     
     
     
Output
v12.8.0

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并创建配置文件:

  • npm -v

    npm -v

   
     
     
     
     
Output
6.10.2

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软件包:

  • sudo apt install build-essential

    须藤apt install build-essential

You now have the necessary tools to work with npm packages that require compiling code from source.

现在,您具有必要的工具来处理需要从源代码编译代码的npm软件包。

使用NVM安装 (Installing Using NVM)

An alternative to installing Node.js through apt is to use a tool called nvm, which stands for “Node Version Manager”. Rather than working at the operating system level, nvm works at the level of an independent directory within your user’s home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.

通过apt安装Node.js的一种替代方法是使用一个名为nvm的工具,该工具代表“节点版本管理器”。 nvm不是在操作系统级别工作,而是在用户主目录内的独立目录级别工作。 这意味着您可以安装多个独立的Node.js版本,而不会影响整个系统。

Controlling your environment with nvm allows you to access the newest versions of Node.js while also retaining and managing previous releases. It is a different utility from apt, however, and the versions of Node.js that you manage with it are distinct from those 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/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh

    curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v 0.34.0 /install.sh -o install_nvm.sh

Inspect the installation script with nano:

使用nano检查安装脚本:

  • nano install_nvm.sh

    纳米install_nvm.sh

If the script looks OK, exit your text editor and run the script with bash:

如果脚本看起来不错,请退出文本编辑器并使用bash运行脚本:

  • bash install_nvm.sh

    bash install_nvm.sh

We don’t need sudo here because nvm is not installed into any privileged system directories. It will instead install the software into a subdirectory of your home directory at ~/.nvm. It will also add some configuration to your ~/.profile file to enable the new software.

我们这里不需要sudo ,因为nvm没有安装到任何特权系统目录中。 而是将软件安装到主目录的子目录中,该子目录位于~/.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文件,以便您的当前会话了解更改:

  • source ~/.profile

    来源〜/ .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版本的信息,请键入:

  • nvm ls-remote

    nvm ls-remote

   
     
     
     
     
Output
. . . v10.16.2 (Latest LTS: Dubnium) v11.0.0 v11.1.0 v11.2.0 v11.3.0 v11.4.0 v11.5.0 v11.6.0 v11.7.0 v11.8.0 v11.9.0 v11.10.0 v11.10.1 v11.11.0 v11.12.0 v11.13.0 v11.14.0 v11.15.0 v12.0.0 v12.1.0 v12.2.0 v12.3.0 v12.3.1 v12.4.0 v12.5.0 v12.6.0 v12.7.0 v12.8.0

As you can see, the current LTS version at the time of this writing is v10.16.2. You can install that by typing:

如您所见,在撰写本文时,当前的LTS版本是v10.16.2。 您可以通过键入以下内容进行安装:

  • nvm install 10.16.2

    nvm安装10.16.2

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 10.16.2

    nvm使用10.16.2

As always, you can verify the Node.js version currently being used by typing:

与往常一样,您可以通过键入以下命令来验证当前正在使用的Node.js版本:

  • node -v

    节点-v

   
     
     
     
     
Output
v10.16.2

If you have multiple Node.js versions, you can see what is installed by typing:

如果您有多个Node.js版本,则可以通过键入以下内容查看安装的内容:

  • nvm ls

    虚拟机

If you wish to default to one of the versions, type:

如果您希望默认使用其中一种版本,请键入:

  • nvm alias default 10.16.2

    nvm别名默认10.16.2

This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:

产生新会话时,将自动选择此版本。 您也可以通过如下别名来引用它:

  • nvm use default

    nvm使用默认

Each version of Node.js will keep track of its own packages and has npm available to manage these.

每个版本的Node.js都将跟踪自己的软件包,并提供npm来管理它们。

删除Node.js (Removing Node.js)

You can uninstall Node.js using apt or nvm, depending on the version you want to target. To remove versions installed from the Debian repository or from the PPA, you will need to work with the apt utility at the system level.

您可以使用aptnvm卸载Node.js,具体取决于要定位的版本。 要从Debian存储库或PPA中删除安装的版本,您将需要在系统级别使用apt实用程序。

To remove either of these versions, type the following:

要删除这些版本中的任何一个,请键入以下内容:

  • sudo apt remove nodejs

    sudo apt删除nodejs

This command will remove the package and the configuration files.

此命令将删除软件包和配置文件。

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版本,请首先确定您要删除的版本是否为当前活动版本:

  • nvm current

    nvm当前

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以启用更改:

  • nvm deactivate

    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 reinstallation.

现在,您可以使用上面的uninstall命令来卸载当前版本,该命令将删除与Node.js的目标版本相关的所有文件,但可以用于重新安装的缓存文件除外。

结论 (Conclusion)

There are a quite a few ways to get up and running with Node.js on your Debian 10 server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in the Debian repository is an option for experimentation, installing from a PPA and working with npm or nvm offers additional flexibility.

在Debian 10服务器上有很多方法可以启动和运行Node.js。 您的情况将决定上述哪种方法最适合您的需求。 虽然可以使用Debian存储库中的打包版本进行试验,但是从PPA安装并与npmnvm可提供更多的灵活性。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-debian-10

你可能感兴趣的:(java,linux,python,编程语言,数据库)