如何在CentOS 8上安装Node.js

介绍 (Introduction)

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 a CentOS 8 server:

在本指南中,我们将向您展示在CentOS 8服务器上安装Node.js的三种不同方式:

  • using dnf to install the nodejs package from CentOS’s default AppStream repository

    使用dnf从CentOS的默认AppStream存储库安装nodejs软件包

  • installing nvm, the Node Version Manager, and using it to install and manage multiple versions of node

    安装nvm ,节点版本管理器,并使用它来安装和管理node多个版本

  • building and installing node from source

    从源代码构建和安装node

Most users should use dnf to install the built-in pre-packaged versions of Node. If you’re a developer or otherwise need to manage multiple installed versions of Node, use the nvm method. Building from source is rarely necessary for most users.

大多数用户应使用dnf来安装Node的内置预打包版本。 如果您是开发人员,或者需要管理多个已安装的Node版本,请使用nvm方法。 对于大多数用户而言,很少需要从源代码构建。

先决条件 (Prerequisites)

To complete this tutorial, you will need a server running CentOS 8. We will assume you are logged into this server as a non-root, sudo-enabled user. To set this up, see our Initial Server Setup for CentOS 8 guide.

要完成本教程,您将需要一台运行CentOS 8的服务器。我们假定您以非root用户 ,启用了sudo用户身份登录到该服务器。 要进行设置,请参阅我们的CentOS 8初始服务器设置指南。

选项1 —从CentOS AppStream存储库安装节点 (Option 1 — Installing Node from the CentOS AppStream Repository)

Node.js is available from CentOS 8’s default AppStream software repository. There are multiple versions available, and you can choose between them by enabling the appropriate module stream. First list out the available streams for the nodejs module using the dnf command:

可从CentOS 8的默认AppStream软件存储库中获取Node.js。 有多种版本,您可以通过启用适当的模块流在它们之间进行选择。 首先使用dnf命令nodejs模块的可用流:

  • sudo dnf module list nodejs

    sudo dnf模块列表nodejs

   
   
Output
Name Stream Profiles Summary nodejs 10 [d] common [d], development, minimal, s2i Javascript runtime nodejs 12 common, development, minimal, s2i Javascript runtime

Two streams are available, 10 and 12. The [d] indicates that version 10 is the default stream. If you’d prefer to install Node.js 12, switch module streams now:

有两个流, 1012[d]表示版本10是默认流。 如果您想安装Node.js 12,请立即切换模块流:

  • sudo dnf module enable nodejs:12

    sudo dnf模块启用nodejs:12

You will be prompted to confirm your decision. Afterwards the version 12 stream will be enabled and we can continue with the installation. For more information on working with module streams, see the official CentOS AppStream documentation.

系统将提示您确认您的决定。 之后,将启用版本12流,我们可以继续进行安装。 有关使用模块流的更多信息,请参见正式的CentOS AppStream文档

Install the nodejs package with dnf:

使用dnf安装nodejs软件包:

  • sudo dnf install nodejs

    须藤dnf安装nodejs

Again, dnf will ask you to confirm the actions it will take. Press y then ENTER to do so, and the software will install.

再次, dnf将要求您确认将要执行的操作。 依次按y ENTER ,然后将安装软件。

Check that the install was successful by querying node for its version number:

通过查询node的版本号来检查安装是否成功:

  • node --version

    节点-版本

   
   
Output
v12.13.1

Your --version output will be different if you installed Node.js 10 instead.

如果安装了Node.js 10,则--version输出将有所不同。

Note: both available versions of Node.js are long-term support releases, meaning they have a longer guaranteed window of maintenance. See the official Node.js releases page for more lifecycle information.

注意: Node.js的两个可用版本都是长期支持版本,这意味着它们具有更长的保证维护期。 请参阅官方的Node.js版本页面以获取更多生命周期信息。

Installing the nodejs package should also install the npm Node Package Manager utility as a dependency. Verify that it was installed properly as well:

安装nodejs软件包还应将npm Node Package Manager实用程序作为依赖项进行安装。 验证它是否也正确安装:

  • npm --version

    npm --version

   
   
Output
6.12.1

At this point you have successfully instlled Node.js and npm using the CentOS software repositories. The next section will show how to use the Node Version Manager to do so.

至此,您已经使用CentOS软件存储库成功安装了Node.js和npm。 下一部分将显示如何使用节点版本管理器执行此操作。

选项2 —使用节点版本管理器安装节点 (Option 2 — Installing Node Using the Node Version Manager)

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 CentOS 8 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.

要在CentOS 8机器上安装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段:

  • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh

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的最新版本而变化,但是从现在开始,可以通过键入以下内容来下载和执行脚本:

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

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | 重击

This will install the nvm script to your user account. To use it, you must first source your .bash_profile file:

这会将nvm脚本安装到您​​的用户帐户。 要使用它,您必须首先获取.bash_profile文件:

  • source ~/.bash_profile

    来源〜/ .bash_profile

Now, you can ask NVM which versions of Node are available:

现在,您可以询问NVM哪些版本的Node可用:

nvm list-remote
. . .
       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   (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

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,可以输入:

  • nvm install v13.6.0

    nvm安装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 installed through the CentOS software repositories, you may see a system -> v12.13.1 (or some other version number) line here. You can always activate the system version of Node using nvm use system.

注意:如果您还通过CentOS软件存储库安装了一个版本的Node,则可能会在此处看到system -> v12.13.1 (或其他版本号)行。 您始终可以使用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.19.0 (-> N/A) lts/erbium -> v12.16.1 (-> 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 ,请运行以下命令:

  • nvm install lts/erbium

    nvm安装lts / erbium

   
   
Output
Downloading and installing node v12.16.1... . . . Now using node v12.16.1 (npm v6.13.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:

您可以通过输入以下内容使用其他部分中的相同技术来验证安装是否成功:

node --version

   
   
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

选项3-从源安装节点 (Option 3 — Installing Node from Source)

Another way to install Node.js is to download the source code and compile it yourself.

安装Node.js的另一种方法是下载源代码并自行编译。

To do so, use your web browser to navigate to the official Node.js download page, right-click on the Source Code link and click Copy Link Address or whichever similar option your browser gives you.

为此,请使用Web浏览器导航到Node.js官方下载页面 ,右键单击Source Code链接,然后单击Copy Link Address或浏览器为您提供的任何类似选项。

Back in your SSH session, first make sure you’re in a directory you can write to. We’ll use the current user’s home directory:

返回SSH会话,首先请确保您位于可写入的目录中。 我们将使用当前用户的主目录:

  • cd ~

    光盘〜

Then type curl, paste the link that you copied from the website, and follow it with | tar xz:

然后键入curl ,粘贴从网站复制的链接,并在其后跟随| tar xz | tar xz

  • curl https://nodejs.org/dist/v12.16.1/node-v12.16.1.tar.gz | tar xz

    卷曲https://nodejs.org/dist/v12.16.1/node-v12.16.1.tar.gz | 焦油xz

This will use the curl utility to download the source, then pipe it directly to the tar utility, which will extract it into the current directory.

这将使用curl直接实用程序下载源代码,然后通过管道它的tar实用工具,将其解压到当前目录。

Move into the newly created source directory:

移至新创建的源目录:

  • cd node-v*

    cd节点-v *

There are a few packages that we need to download from the CentOS repositories in order to compile the code. Use dnf to install these now:

我们需要从CentOS资料库中下载一些软件包来编译代码。 使用dnf现在安装这些:

  • sudo dnf install gcc-c++ make python2

    须藤dnf安装gcc-c ++使python2

You will be prompted to confirm the installation. Type y then ENTER to do so. Now, we can configure and compile the software:

系统将提示您确认安装。 键入y然后按ENTER 。 现在,我们可以配置和编译软件:

  • ./configure

    。/配置
  • make -j4

    使-j4

The compilation will take quite a while (around 30 minutes on a four-core server). We’ve used the -j4 option to run four parallel compilation processes. You can omit this option or update the number based on the number of processor cores you have available.

编译将花费一些时间(在四核服务器上大约需要30分钟)。 我们使用了-j4选项来运行四个并行的编译过程。 您可以忽略此选项,也可以根据可用处理器核心的数量来更新数量。

When compilation is finished, you can install the software onto your system by typing:

编译完成后,您可以通过键入以下命令将软件安装到系统上:

  • sudo make install

    须藤使安装

To check that the installation was successful, ask Node to display its version number:

要检查安装是否成功,请要求Node显示其版本号:

  • node --version

    节点-版本
v12.16.1

If you see the correct version number, then the installation was completed successfully. By default Node also installs a compatible version of npm, so that should be available as well.

如果看到正确的版本号,则说明安装成功完成。 默认情况下,Node还安装兼容版本的npm ,因此它也应该可用。

结论 (Conclusion)

In this tutorial we’ve shown how to install Node.js using the CentOS AppStream software repository, using Node Version Manager, and by compiling from source.

在本教程中,我们展示了如何使用CentOS AppStream软件存储库,节点版本管理器以及从源代码进行编译来安装Node.js。

If you’d like more information on programming in JavaScript, please read our related tutorial series:

如果您想了解有关JavaScript编程的更多信息,请阅读我们的相关教程系列:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值