LoginSignup
39

More than 3 years have passed since last update.

CentOSにyumでNodeJS最新版をインストール(NodeSource)

Posted at

NodeJSで開発したアプリケーションを運用するために
CentOSに最新版(またはLTS)のNodeJSをインストールする手順です

パッケージマネージャyumを使ってインストールしたいところですが
EPELリポジトリで提供されているnodejsは
バージョンが古めのLTS v6.xとなっています
※v6.xは2019年4月にサポート終了(EOL)予定となっています

そこでNodeJS公式サイトでも案内されている
NodeSourceのリポジトリを追加してyumインストールします

yumのリポジトリを追加

ここに記載されている1行のコマンドを実行します
最新版v11.xをインストールする場合

$ curl -sL https://rpm.nodesource.com/setup_11.x | bash -

※異なるメジャーバージョンのNodeJSをインストールする場合は上記コマンドのURLの数字部分が変わる

私の環境ではこのシェルスクリプトを使ったインストール方法では
エラーとなりうまくいかなかったので
シェルスクリプト実行時に出力されていたURLから
直接rpmをインストールしました

RPMファイルを直接インストールする場合
$ sudo yum install https://rpm.nodesource.com/pub_11.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm
インストール後のyumリポジトリディレクトリ
$ ls -1 /etc/yum.repos.d
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
CentOS-fasttrack.repo
epel-testing.repo
epel.repo
nodesource-el7.repo  ←←←追加されています

NodeJSのインストール

yumでNodeJSをインストール

$ sudo yum install nodejs -y
インストールされたバージョンを確認
$ node -v
v11.12.0

npmを最新版にする

$ sudo npm update -g npm

※グローバルにインストール/アップデート/削除する場合はsudoで実行します

$ npm -v
6.9.0

パッケージをグローバルインストールする

npm代替のパッケージマネージャとして人気のYarnをインストールする場合

$ sudo npm install -g yarn
$ which yarn
/usr/bin/yarn

Global install (yarn)

$ sudo yarn global add (パッケージ名)

例として、人気のNodeJSプロセスマネージャPM2をインストールしてみます

$ sudo yarn global add pm2
$ which pm2
/usr/local/bin/pm2

※yarn global addでインストールしたコマンドは/usr/local/binにインストールされました

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39