LoginSignup
58
64

More than 5 years have passed since last update.

【俺用メモ】 CentOS6にNode.jsをインストールする

Last updated at Posted at 2016-06-26

Node.jsのインストール方法が複数あって、忘れてしまうのでメモっとく。
(※2018-02-10時点)

nvmでインストールする場合

nvmはNode.jsのバージョン管理ツール。(https://github.com/creationix/nvm)
複数バージョンのNode.jsをインストールして管理することが可能。
コマンド一発で簡単に使用するバージョンの切り替えが行える。
基本的にはこの方法を使っている。

1. nvmのインストール

https://github.com/creationix/nvm#installation を参考に、最新バージョンを入れる。

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

2. nvmインストール確認

$ nvm --version
0.33.8

3. インストール可能なNode.jsのバージョン確認

$ nvm ls-remote

4. インストール

  • 最新stableを入れる場合
$ nvm install stable

Downloading and installing node v9.5.0...
Downloading https://nodejs.org/dist/v9.5.0/node-v9.5.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v9.5.0 (npm v5.6.0)
Creating default alias: default -> stable (-> v9.5.0)
  • 特定バージョンを指定する場合

(例: v4.4.5)

$ nvm install v4.4.5

Downloading and installing node v4.4.5...
Downloading https://nodejs.org/dist/v4.4.5/node-v4.4.5-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v4.4.5 (npm v2.15.5)

(例: v5.12.0)

$ nvm install v5.12.0

Downloading and installing node v5.12.0...
Downloading https://nodejs.org/dist/v5.12.0/node-v5.12.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v5.12.0 (npm v3.8.6)

5. インストール確認

$ node -v
v5.12.0

6. インストール済みバージョンの確認

-> 印が付いているのが、現在使用中のバージョン。
systemとなっているのは、yumインストールしたバージョン。

$ nvm ls
         v4.4.5
->      v5.12.0
         v9.5.0
         system
default -> stable (-> v9.5.0)
node -> stable (-> v9.5.0) (default)
stable -> 9.5 (-> v9.5.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.8.7 (-> N/A)
lts/boron -> v6.12.3 (-> N/A)
lts/carbon -> v8.9.4 (-> N/A)

7. バージョン切り替え

$ nvm use v4.4.5
Now using node v4.4.5 (npm v2.15.5)

8. デフォルトバージョンの指定

毎回 useコマンドでバージョン指定しなくて良いように、defaultバージョンを指定可能。

$ nvm alias default v4.4.5
$ nvm ls
->       v4.4.5
        v5.12.0
         v9.5.0
         system
default -> v4.4.5
node -> stable (-> v9.5.0) (default)
stable -> 9.5 (-> v9.5.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.8.7 (-> N/A)
lts/boron -> v6.12.3 (-> N/A)
lts/carbon -> v8.9.4 (-> N/A)

yumでインストールする場合 (epelリポジトリでv6.x系)

epelリポジトリでyumインストールした場合は、結構古いバージョンが入るけど
ササッと入れてちょっとした検証をしたい場合や、バージョンなんて気にしないぜ!という場合はこちらで。
(※ yumの場合は1つのバージョンしか管理できない)

1. epelリポジトリ追加

$ yum install epel-release

2. Node本体をインストール

$ yum install nodejs

3. g++, makeのインストール

$ yum install gcc-c++ make

4. インストール確認

$ node -v
v6.12.3
$ npm -v
3.10.10

yumでインストールする場合 (nodesourceリポジトリでv8.x系など)

epelリポジトリでyumインストールされるバージョンは古いので
もう少し新しいバージョンを入れたい場合は、nodesourceリポジトリを追加して対応。
(※ yumの場合は1つのバージョンしか管理できない)

公式ドキュメントに記載されている手順を実行すればOK
https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

1. リポジトリ追加 (スクリプト実行)

$ curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -

2. Node本体をインストール

$ yum install nodejs

※ epelの古いバージョンが入っている場合は、yum remove nodejs npmで先に削除しておく。

3. g++, makeのインストール

$ yum install gcc-c++ make

4. インストール確認

$ node -v
v8.9.4
$ npm -v
5.6.0
58
64
0

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