更新时间:2023-02-03 10:25:13浏览次数:282+次
本文介绍如何在CentOS 8/RHEL 8/Rocky Linux 8系统上安装Node.js 18 LTS的方法。
安装方法
选项1、在CentOS 8/RHEL 8上使用RPM存储库安装安装Node.js 18
在CentOS 8/RHEL 8上添加Node.js 18 LTS存储:
curl --silent --location https://rpm.nodesource.com/setup_18.x | sudo bash -
存储库在/etc/yum.repos.d/nodesource-el7.repo文件中配置。配置存储库后,继续安装Node.js 18 LTS:
sudo yum -y install nodejs
对于正在为Node.js构建插件和扩展的用户,您可能需要安装开发工具:
sudo yum -y install gcc-c++ make
如果需要,请安装Yarn包管理器:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum -y install yarn
检查已安装的NodeJS版本:
$ node -v
v18.0.0
选项2、在CentOS 8/RHEL 8上使用NVM安装Node.js 18
还可以使用NVM在Linux系统上安装Node.js包。NVM(Node Version Manager)是每个shell调用的Node.js的版本管理器。它适用于任何符合POSIX的shell(bash、sh、dash、ksh、zsh)。通过使用nvm,您可以通过命令行快速安装和使用不同版本的节点。
由于默认情况下,nvm未安装在基于RHEL的系统上,请运行以下命令来安装它:
curl -LsS https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | sh -s --
通过获取shell rc文件开始使用nvm:
### Bash ###
$ source ~/.bashrc
### Zsh ###
$ source ~/.zshrc
执行的脚本将nvm存储库克隆到~/.nvm,并尝试将以下代码段中的源代码行添加到正确的配置文件(~/.bash_profile,~/.zshrc,~/.profile,或~/.bashrc)中。
使用ls-remote列出Node.js的可用版本:
$ nvm ls-remote
会返回包含v18.0.0的信息,如下截图:
让我们安装Node.js 18版本:
$ nvm install 18
示例执行输出:
Downloading and installing node v18.0.0...
Downloading https://nodejs.org/dist/v18.0.0/node-v18.0.0-linux-x64.tar.xz...
################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
^[[Now using node v18.0.0 (npm v8.6.0)
Creating default alias: default -> 18 (-> v18.0.0)
您可以使用nvm命令安装其他LTS版本:
### 安装Node.js 16 ###
$ nvm install 16
Downloading and installing node v16.14.2...
Downloading https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz
################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.2 (npm v8.5.0)
### 安装Node.js 14 ###
$ nvm install 14
Downloading and installing node v14.19.1...
Downloading https://nodejs.org/dist/v14.19.1/node-v14.19.1-linux-x64.tar.xz
################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.19.1 (npm v6.14.16)
### 安装Node.js 12 ###
$ nvm install 12
Downloading and installing node v12.22.12...
Downloading https://nodejs.org/dist/v12.22.12/node-v12.22.12-linux-x64.tar.xz
################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.12 (npm v6.14.16)
列出已安装的Node.js版本:
$ nvm list
会返回包含已安装的v18.0.0版本信息,如下截图:
使用NodeJS的特定版本:
###语法###
$ nvm use <version
#示例
$ nvm use 18
Now using node v18.0.0 (npm v8.6.0)
$ nvm use 16
Now using node v16.14.2 (npm v8.5.0)
至此,安装Node.js 18成功。