如何在Linux中安装最新的NodeJS和NPM

在本指南中,我們將看一下如何在基於RHEL的發行版(如CentOS、Fedora、Rocky和AlmaLinux)以及基於Debian的發行版(如Ubuntu和Linux Mint)上安裝最新版本的Node.js和NPM。

Node.js是一個基於Chrome的V8 JavaScript引擎構建的輕量級高效的JavaScript平台,NPM是NodeJS的默認套件管理器。您可以使用它來構建可擴展的網絡應用程序。

如何在RHEL發行版中安裝Node.js

最新版本的Node.js和NPM可從官方NodeSource企業Linux存儲庫中獲得,該存儲庫由Nodejs網站維護,您需要將其添加到系統中才能安裝最新的Nodejs和NPM套件。

重要: 如果您運行的是較舊的RHEL 6或CentOS 6版本,您可能需要閱讀有關在較舊的發行版上運行Node.js的信息。

在RHEL、CentOS、Fedora、Rocky和Alma中安裝NodeJS

要添加最新版本的Node.js存儲庫,請使用以下命令作為root或非root用戶。

------------- For Node.js v19.x ------------- 
$ curl -fsSL https://rpm.nodesource.com/setup_19.x | sudo bash -
$ sudo yum install -y nodejs

------------- For Node.js v18.x ------------- 
$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
$ sudo yum install -y nodejs

------------- For Node.js v16.x ------------- 
$ curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
$ sudo yum install -y nodejs

------------- For Node.js v14.x ------------- 
$ curl -fsSL https://rpm.nodesource.com/setup_14.x | sudo bash -
$ sudo yum install -y nodejs

可選: 您需要在系統上安裝開發工具,如gcc-c++和make,以便從npm構建本地插件。

# yum install gcc-c++ make
OR
# yum groupinstall 'Development Tools'

在Debian、Ubuntu和Linux Mint中安裝Node.js

最新版本的Node.js和NPM也可從官方NodeSource企業Linux存儲庫中獲得,該存儲庫由Nodejs網站維護,您需要將其添加到系統中才能安裝最新的Nodejs和NPM套件。

在Ubuntu和Mint上安裝Node.js

------------- For Node.js v19.x ------------- 
$ curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v18.x -------------
$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v16.x -------------
$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v14.x -------------
$ curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash - &&\
$ sudo apt-get install -y nodejs

在Debian上安裝Node.js

------------- For Node.js v19.x ------------- 
$ curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v18.x ------------- 
$ curl -fsSL https://deb.nodesource.com/setup_18.x | bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v16.x ------------- 
$ curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &&\
$ sudo apt-get install -y nodejs

------------- For Node.js v14.x ------------- 
$ curl -fsSL https://deb.nodesource.com/setup_14.x | bash - &&\
$ sudo apt-get install -y nodejs

可選:您需要在系統上安裝開發工具,如gcc-c++make,以便從npm構建本機插件。

$ sudo apt-get install -y build-essential

在Linux上測試最新的Nodejs和NPM

要簡單測試nodejsNPM,您只需使用以下命令檢查系統上安裝的版本:

在基於RHEL的系統上

# node --version
# npm --version

在Debian、Ubuntu和Linux Mint上

$ nodejs --version
$ npm --version
Check NodeJS Version on CentOS

就是這樣,NodejsNPM現在已經安裝並準備在您的系統上使用。

I believe these were easy and simple steps to follow but in case of problems you faced, you can let us know and we find ways of helping you. I hope this guide was helpful to you and always remember to stay connected to Tecmint.

Source:
https://www.tecmint.com/install-nodejs-npm-in-centos-ubuntu/