Linuxで最新のNodeJSとNPMをインストールする方法

このガイドでは、CentOS、Fedora、Rocky&AlmaLinuxなどのRHELベースのディストリビューションや、Ubuntu&Linux MintなどのDebianベースのディストリビューションに、最新バージョンのNodejsとNPMをインストールする方法を見ていきます。

Node.jsは、ChromeのV8 JavaScriptエンジンに基づいて構築された軽量で効率的なJavaScriptプラットフォームであり、NPMはデフォルトのNodeJSパッケージマネージャーです。スケーラブルなネットワークアプリケーションを構築するために使用できます。

RHELディストリビューションにNode.jsをインストールする方法

最新バージョンのNode.jsおよびNPMは、公式のNodeSource Enterprise Linuxリポジトリから利用可能です。このリポジトリはNodejsのウェブサイトによって管理されており、最新のNodejsおよびNPMパッケージをインストールするためにシステムに追加する必要があります。

重要: より古いバージョンのRHEL 6またはCentOS 6を実行している場合は、古いディストリビューションでのNode.jsの実行について読むことを検討するかもしれません。

RHEL、CentOS、Fedora、Rocky、およびAlmaにNodeJSをインストールする

最新バージョンのNode.jsのリポジトリを追加するには、以下のコマンドをルートまたは非ルートとして使用します。

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

オプション: npmからネイティブアドオンをビルドするために、gcc-c++makeなどの開発ツールが必要です。

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

Debian、Ubuntu、およびLinux MintにNode.jsをインストールする方法

最新バージョンのNode.jsおよびNPMは、公式のNodeSource Enterprise 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/