如何在 RHEL 9 上安装 Node.js
Node.js NVM
![](/data/attachment/album/202211/17/092223tdodvvfdnsjiezxv.jpg)



> 
> 在这篇文章中，我们将逐步解释如何在 RHEL 9 系统上安装 Node.js。
> 
> 
> 


[Node.js](https://nodejs.org/en/about/) 基于谷歌的 V8 JavaScript 引擎构建，它是一个自由开源的跨平台 JavaScript 运行时环境，主要用于构建服务器端应用。它使用事件驱动和异步模型，帮助开发人员构建高度可扩展的数据密集型的实时应用（RTA）。你可以使用 NodeJS 来构建前端和后端应用。


Node.js 通常用于构建以下应用：


* 聊天应用
* 流媒体应用
* 浏览器游戏
* 命令行工具
* 嵌入式系统


在其技术栈中使用 NodeJS 的顶级公司包括 PayPal、NetFlix 和 Uber 等等。


安装 Node.js 主要有以下三种方式：


* 从 NodeSource 仓库安装 Node.js
* 从发行版的官方仓库安装 Node.js
* 使用 NVM 安装 Node.js


让我们看看如何使用这些方法在 RHEL 9 上安装 Node.js。


先决条件：


* 最小化安装的 RHEL 9 系统
* 具有管理员权限的 [sudo 用户](https://www.linuxtechi.com/create-sudo-user-on-rhel-rocky-linux-almalinux/)
* 互联网连接
* Red Hat 订阅或本地配置的仓库


### 从 NodeSource 存储库安装 Node.js


[NodeSource](https://nodesource.com/) 是一家技术公司，旨在帮助组织运行生产就绪的 Node.js 应用，关注资源使用以及增强的安全性和应用程序性能。它提供了最新版本的 Node.js 和 NPM。


要从 NodeSource 安装 Node.js，首先，按如下所示更新系统包。



```
$ sudo dnf update -y

```

![](/data/attachment/album/202211/17/092231siytzmdiydtml9t0.png)


接下来，安装这期间所需的构建工具。其中包括 GCC C/C++ 编译器、Perl 和 Python 调试器等等。



```
$ sudo dnf groupinstall 'Development Tools' -y

```

![](/data/attachment/album/202211/17/092232ejjqzwx7yyg7wlgb.png)


接下来，我们将从 NodeSource 安装 Node.js 18.x。为此，请下载并运行 NodeSource 设置脚本，如下所示。



```
$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

```

该脚本在其他任务中将 NodeSource 仓库添加到你的系统。


![](/data/attachment/album/202211/17/092233zx952zwz297vvvba.png)


在输出的末尾，你将看到一些关于如何安装 Node.js 和 NPM 的附加说明。


![](/data/attachment/album/202211/17/092234swnpfa2waaw2aosp.png)


因此，要安装 Node.js 和 NPM（Node 包管理器），请运行以下命令：



```
$ sudo dnf install nodejs -y

```

![](/data/attachment/album/202211/17/092235q7uv3nprzvpo5hpd.png)


安装完成后，按如下所示验证 Node.js 和 NPM 的版本。



```
$ node -v
$ npm -v

```

![](/data/attachment/album/202211/17/092236eo6ih6e96576q66c.png)


输出显示我们正在运行 Node v18.12，它是最新的 LTS 版本和 NPM 8.19.2。


### 从官方 RHEL 仓库安装 Node.js


安装 NodeJS 和 NPM 的另一种方法是从发行版的官方仓库中安装它们。但是，这种方法不提供最新版本。


如果你不介意不安装最新版本的 Node 和 NPM。 那么在命令行上运行以下命令。



```
$ sudo dnf update -y
$ sudo dnf install nodejs npm -y

```

![](/data/attachment/album/202211/17/092236ky3a33a22hbvqiaa.png)


### 使用 NVM 安装 Node.js


最后，你可以使用 NVM（Node 版本管理器）安装 Node.js，这是一种用于管理系统上 Node 版本的工具。该工具可帮助开发人员在需要不同版本 Node.js 的不同项目上高效工作。


默认情况下没安装 NVM。你需要通过运行 [官方 GitHub 页面](https://github.com/nvm-sh/nvm) 上提供的 Shell 脚本来安装它。



```
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

```

这会下载 NVM 并将其保存在主目录的 `.nvm` 目录中。


![](/data/attachment/album/202211/17/092237vaac91cjfhsf5rca.png)


安装后，关闭终端会话并打开一个新终端。然后运行以下命令确认 NVM 已经安装。



```
$ command -v nvm

```

![](/data/attachment/album/202211/17/092237regs0qdlsejzzrlq.png)


接下来，你可以使用以下命令列出所有可用的 Node.js 版本：



```
$ nvm ls-remote

```

![](/data/attachment/album/202211/17/092238ditrrh98ot8kdn9k.png)


或者，你可以列出 Node.js 版本的所有最新 LTS 版本，如图所示。



```
$ nvm ls-remote | grep -i latest

```

![](/data/attachment/album/202211/17/092240agf0c6gc1svv5o7s.png)


要安装最新版本的 Node.js（当前为 v19.0.0），请运行以下命令：



```
$ nvm install node

```

![](/data/attachment/album/202211/17/092240nttt7jnnatysjst7.png)


然后，你可以验证安装的 Node.js 版本，如下所示。



```
$ node -v

```

![](/data/attachment/album/202211/17/092241a95qtajtstqns0lj.png)


此外，你可以安装特定版本的 Node.js。例如，要安装 v18.2.0，请运行以下命令：



```
$ nvm install v18.12.0

```

![](/data/attachment/album/202211/17/092242tek7f7p6zmk7u7ad.png)


要列出系统上所有已安装的 NodeJS 版本，请运行以下命令：



```
$ nvm ls

```

第一行带有 “->” 符号的条目指向当前使用的 Node.js 版本。然后是其他版本。


![](/data/attachment/album/202211/17/092242jz6bcbry6bsbdmdr.png)


要切换到另一个版本的 Node.js，请使用以下语法：



```
$ nvm use <version>

```

例如，要使用 Node 版本 19.0.0，请运行以下命令：



```
$ nvm use 19.0.0

```

![](/data/attachment/album/202211/17/092243ck6o6q8q4lqtq8ik.png)


再次检查已安装的 Node.js 版本，这次“->” 符号将指向 v19.0.0。


![](/data/attachment/album/202211/17/092243eprirn7n0ejbeill.png)


### 总结


在本指南中，我们演示了如何使用三种不同的方法安装 Node.js。此外，我们还提供了几种使用 NVM 管理 Node 版本的方法。我们希望可以帮助你轻松地在 RHEL 上安装 NodeJS，并选择你想要在项目中使用的版本。




---


via: <https://www.linuxtechi.com/how-to-install-nodejs-on-rhel/>


作者：[James Kiarie](https://www.linuxtechi.com/author/james/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this post, we will explain how to install Node.js on RHEL 9 system step-by-step.

Built on Google’s V8 Javascript engine, [Node.js](https://nodejs.org/en/about/) is a free and opensource, cross-platform JavaScript runtime that is mostly used for building server-side applications. It uses an event-driven and asynchronous model that helps developers build highly scalable, data-intensive real-time applications (RTAs ). You can use NodeJS to build both front-end and back-end applications.

Node.js is commonly used in building the following applications:

- Chat applications
- Streaming applications
- Browser games
- Command-line tools
- Embedded systems

Top companies that use NodeJS in their tech stacks include PayPal, Netflix, and Uber to mention a few.

There are three main ways of installing Node.JS:

- Installing Node.JS from the NodeSource repository
- Installing Node.JS from the distribution’s Official repository
- Installing Node.JS using NVM

Let us check out how to install Node.JS on RHEL 9 using each of these methods.

#### Prerequisites

- Minimal Installed RHEL 9 System
[Sudo User](https://www.linuxtechi.com/create-sudo-user-on-rhel-rocky-linux-almalinux/)with admin rights- Internet Connectivity
- Red Hat Subscription or locally configured repository

## Installing Node.js from NodeSource Repository

[NodeSource ](https://nodesource.com/)is a technology company that seeks to help organizations run production-ready Node.Js applications with more focus on resource usage and enhanced security and application performance. It provides the latest versions of Node.JS and NPM.

To install Node.SJ from Nodesource, first, update the system packages as shown.

$ sudo dnf update -y

Next, install the required build tools which will be required during the installation of Node.JS. These include the GCC c/c++ compiler, Perl, and Python debuggers to mention a few.

$ sudo dnf groupinstall 'Development Tools' -y

Next, we are going to install Node.JS 18.x from Nodesource. To do this, download and run the NodeSource setup script as follows.

$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

The script adds the Nodesource repository to your system among other tasks

At the tail end of the output, you will see some additional instructions provided on how to install Node.JS and npm.

Therefore, to install Node.JS and npm (Node Package Manager), run the command:

$ sudo dnf install nodejs -y

Once the installation is complete, verify the version of Node.JS and NPM as shown.

$ node -v $ npm -v

The output shows that we are running Node v18.12 which is the latest LTS release and NPM 8.19.2.

## Installing Node.js from the official RHEL repositories

The other way of installing NodeJS and NPM is by installing them from your distribution’s official repository. However, this approach does not provide the latest versions.

If you don’t mind not installing the latest versions of Node and NPM. , then run the following command on the command line.

$ sudo dnf update -y $ sudo dnf install nodejs npm -y

## Installing Node.js using NVM

Lastly, you can install Node.JS using NVM ( Node Version Manager) which is a tool for managing Node versions on your system. The tool helps developers work efficiently on different projects which require different versions of Node.JS

NVM is not installed by default You need to install it by running the Shell script which is available on the [Official GitHub Page](https://github.com/nvm-sh/nvm).

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

This downloads and saves nvm in the .nvm directory in your home directory.

Once installed, close your terminal sessions and open a new terminal. Then run the following command to confirm that NVM has been installed.

$ command -v nvm

Next, you can list all the available versions of Node.JS using the following command:

$ nvm ls-remote

Alternatively, you can list all the latest LTS releases for Node.JS versions as shown.

$ nvm ls-remote | grep -i latest

To install the very latest version of Node.JS (currently v19.0.0 ), run the command:

$ nvm install node

You can then verify the version of Node installed as shown.

$ node -v

In addition, you can install a specific version of Node. JS. For example, to install v18.2.0, run the command:

$ nvm install v18.12.0

To list all the installed versions of NodeJS on your system, run the command:

$ nvm ls

The first entry with the sign ( – > ) points to the version of Node.JS that is currently in use. This is then followed by other versions as indicated below

To switch to another version of Node.JS, use the syntax:

$ nvm use <version>

For example, to use Node version 19.0.0, run the command:

$ nvm use 19.0.0

Again, check the installed versions of Node.JS, and this time the ( – > ) sign will point to v19.0.0

#### Conclusion

In this guide, we have demonstrated how to install Node.js using three different methods. In addition, we have provided a few ways in which you can manage the Node versions using NVM. We hope that you can now comfortably install NodeJS on RHEL and choose the version that you want to work with on your project.