13
14

More than 5 years have passed since last update.

nodenvで最新のNode.jsを入れてみる

Posted at

lightsail起動して、nodenvでNode.js入れるまで。普通にlithgsailのNode.js入ってるイメージ使えばいいんだけど、何か色々の用途で使いたかったからプレーンなOSスタートだった。

nodenvインストールする

この辺見ながら上からする。

% git clone https://github.com/nodenv/nodenv.git ~/.nodenv
% cd ~/.nodenv && src/configure && make -C src

使ってるshellはzshなので指示に従ってzshrcでPATHを通す。

~/.zshrc
export PATH="$HOME/.nodenv/bin:$PATH"

initしろって書いてあるからする。

% ~/.nodenv/bin/nodenv init
# Load nodenv automatically by appending
# the following to ~/.zshrc:

eval "$(nodenv init -)"

なぜzshrcにもう一度書き込まなきゃならないのか分からないけど、書いてあるからする。

~/.zshrc
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

nodenv-doctor とやらが状態を見てくれるというのでする。これで全部OKって出るらしい。

% curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash
Checking for `nodenv' in PATH: /home/ec2-user/.nodenv/bin/nodenv
Checking for nodenv shims in PATH: OK
Checking `nodenv install' support: not found
Unless you plan to add Node versions manually, you should install node-build.
Please refer to https://github.com/nodenv/node-build#installation

Counting installed Node versions: none
  There aren't any Node versions installed under `/home/ec2-user/.nodenv/versions'.
  You can install Node versions like so: nodenv install 2.2.4
Auditing installed plugins: OK

nodenv installnot found って言ってて急に裏切られた気になる。

node-build入れる

Optionalって書いてあるけど、 node-build を入れなきゃだめみたいだ。

上記を見ると、インストール先が /usr/local になってる。今までec2-userでnodenv入れようとしてたけど、大丈夫なのかな? PATHが通ってればいいのかもしれない。rootになって下記をした。

$ git clone https://github.com/nodenv/node-build.git /usr/local/src
$ PREFIX=/usr/local /usr/local/src/node-build/install.sh

実行しても何も表示されないけど、これでいいっぽい。無慈悲。ec2-userに戻って、doctorに聞いてみる。

% curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash
Checking for `nodenv' in PATH: /home/ec2-user/.nodenv/bin/nodenv
Checking for nodenv shims in PATH: OK
Checking `nodenv install' support: /usr/local/bin/nodenv-install (node-build 4.6.1)
Counting installed Node versions: none
  There aren't any Node versions installed under `/home/ec2-user/.nodenv/versions'.
  You can install Node versions like so: nodenv install 2.2.4
Auditing installed plugins: OK

あ、installが使えるようになったっぽい。

最新のnode入れてみる

インストールできるバージョン確認

% nodenv install -l
・・・
12.4.0
・・・・

インストール

% nodenv install 12.4.0
Downloading node-v12.4.0-linux-x64.tar.gz...
-> https://nodejs.org/dist/v12.4.0/node-v12.4.0-linux-x64.tar.gz
Installing node-v12.4.0-linux-x64...
Installed node-v12.4.0-linux-x64 to /home/ec2-user/.nodenv/versions/12.4.0

結構早く終わる。

% node --version
nodenv: node: command not found

The `node' command exists in these Node versions:
  12.4.0

見つからないのか、見つかったのか、どっちなのか……。で、これはきっとアレだと思った。 use とか global でバージョン指定してないからだと思った。global らしい。

% nodenv global 12.4.0
% node --version
v12.4.0
% npm --version
6.9.0

あ、できた。

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