AmazonLinux2 に Node.js

AmazonLinux2 に Node.js を入れるのはどうしましょう。
いや、今さらそこに入れなくても、ラムダでもドッカーでも呼べばいいんだけど、やっぱり手元環境も欲しくて(手元?)。

この時点、2022/2/半ばで、https://nodejs.org/ja/ にて、バージョンは「16.14.0 LTS 推奨版」「17.5.0 最新版 最新の機能」

(2022/10/1 追記)
その後、nodejs 1.8系をいれようとしたら、AmazonLinux2 では難しくて、AmazonLinux2022 (AL2022) にしました、次の記事 Amazon Linux 2022 (AL2022) と Node.js - hs9587’s diary

バイナリからのインストール

Installing Node.js via binary archive Installation · nodejs/help Wiki · GitHub をみながら

  1. 前項メインサイトより Linux Binaries (x64) node-v16.14.0-linux-x64.tar.xz をダウンロード
    1. 最新 node-v17.5.0-linux-x64.tar.xz でもいいけどここは堅実に推奨版で
  2. how-to-install-nodejs-via-binary-archive-on-linux を参考に /usr/local/lib/nodejs に展開
[ec2-user@ip- ~]$ sudo mkdir -p /usr/local/lib/nodejs
[ec2-user@ip- ~]$ sudo tar -xJvf node-v16.14.0-linux-x64.tar.xz -C /usr/local/lib/nodejs
  1. using-sudo-to-symlink-node-npm-and-npx-into-usrbin を参考に /usr/bin/ に Sリンク
[ec2-user@ip- ~]$ sudo ln -s /usr/local/lib/nodejs/node-v16.14.0-linux-x64/bin/node /usr/bin/node
[ec2-user@ip- ~]$ sudo ln -s /usr/local/lib/nodejs/node-v16.14.0-linux-x64/bin/npm /usr/bin/npm
[ec2-user@ip- ~]$ sudo ln -s /usr/local/lib/nodejs/node-v16.14.0-linux-x64/bin/npx /usr/bin/npx
  1. という事で、バージョン等確認

node

[ec2-user@ip- ~]$ node -v
v16.14.0

npm

[ec2-user@ip- ~]$ npm version
{
  npm: '8.3.1',
  node: '16.14.0',
  v8: '9.4.146.24-node.20',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '93',
  nghttp2: '1.45.1',
  napi: '8',
  llhttp: '6.0.4',
  openssl: '1.1.1m+quic',
  cldr: '40.0',
  icu: '70.1',
  tz: '2021a3',
  unicode: '14.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}

npx

[ec2-user@ip- ~]$ npx -v
8.3.1

これで良かったのだけど、それは下記が難しかったから。

パッケージからのインストール、難

AmazonLinux2 の yumパッケージだと、amazon-linux-extras から epel を入れてそのパッケージ「yum list nodejs」

nodejs.x86_64                        1:16.13.2-7.el7                        epel

まあ、16 です。
そして「sudo yum install nodejs」

Error: Package: 1:nodejs-16.13.2-7.el7.x86_64 (epel)
           Requires: libuv >= 1:1.42.0
           Available: 1:libuv-1.23.2-1.amzn2.0.2.i686 (amzn2-core)
               libuv = 1:1.23.2-1.amzn2.0.2
           Available: 1:libuv-1.39.0-1.amzn2.i686 (amzn2-core)
               libuv = 1:1.39.0-1.amzn2
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

とのこと。なので「 sudo yum install nodejs --skip-broken」
まあ、進むような、Skipするような、そして失敗

--> Processing Dependency: libuv >= 1:1.42.0 for package: 1:nodejs-16.13.2-7.el7.x86_64

だけど

Packages skipped because of dependency problems:
    brotli-1.0.7-5.el7.x86_64 from epel
    1:libuv-1.39.0-1.amzn2.x86_64 from amzn2-core
    1:nodejs-16.13.2-7.el7.x86_64 from epel
    1:nodejs-libs-16.13.2-7.el7.x86_64 from epel
    1:openssl11-1.1.1g-12.amzn2.0.4.x86_64 from amzn2-core

libuv のバージョンが足りないわけです。
ということで、yumからのインストールは出来なかったです。

(追記) NodeSource パッケージ、これが良いかな

コメントいただきました、あり難うございます。

いけむらさん@fd0
既に確認済みかもしれませんが、ここの手順でやると自分は楽にできました
午前0:14 ・ 2022年2月14日

distributions/README.md at master · nodesource/distributions · GitHub

NodeSource Node.js Binary Distributions
https://github.com/nodesource/distributions/blob/master/README.md#nodesource-nodejs-binary-distributions

NodeSourceさん、Node.js本体とは違うところでディストリビューション別のパッケージインストールの案内がありました。

Enterprise Linux based distributions にて、Amazon Linux 2 (64-bit) の項あり、
Installation instructions との事です

Node.js LTS (16.x)

curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -

こちら、setup_lts.x 文書はシェルスクリプト、環境というかディストリビューションをチェックして、良さそうな rpmパッケージをダウンロードしてくる、或いはパッケージのリポジトリをセットします。

そして、

## Run `sudo yum install -y nodejs` to install Node.js 16.x and npm.
## You may run dnf if yum is not available:
     sudo dnf install -y nodejs
## You may also need development tools to build native addons:
     sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
     sudo yum install yarn

と言ってきます。なのでそうする。

sudo yum install -y nodejs

いろいろあって

Running transaction
Warning: RPMDB altered outside of yum.
  Installing : 2:nodejs-16.14.0-1nodesource.x86_64                          1/1
  Verifying  : 2:nodejs-16.14.0-1nodesource.x86_64                          1/1

Installed:
  nodejs.x86_64 2:16.14.0-1nodesource

Complete!
[ec2-user@ip- ~]$ 

という事で、入りました。
バージョン確認。

[ec2-user@ip- ~]$ node -v
v16.14.0
[ec2-user@ip- ~]$ npm version
{
  npm: '8.3.1',
  node: '16.14.0',
  v8: '9.4.146.24-node.20',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '93',
  nghttp2: '1.45.1',
  napi: '8',
  llhttp: '6.0.4',
  openssl: '1.1.1m+quic',
  cldr: '40.0',
  icu: '70.1',
  tz: '2021a3',
  unicode: '14.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}
[ec2-user@ip- ~]$ npx -v
8.3.1
[ec2-user@ip- ~]$

一応 reboot。

(1)ec2-user@ip-[6] ll /etc/yum.repos.d                14:28 0216 Wed
total 16
-rw-r--r-- 1 root root 1003 Oct 27 02:55 amzn2-core.repo
-rw-r--r-- 1 root root 4168 Feb 16 14:12 amzn2-extras.repo
-rw-r--r-- 1 root root  474 Apr 20  2021 nodesource-el7.repo
[~] <pts/2>

nodesource のレポジトリが追加されてるわけです。
yarn もインストールしましょう。

[~] <pts/2>
(1)ec2-user@ip-[7] curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
[yarn]
name=Yarn Repository
baseurl=https://dl.yarnpkg.com/rpm/
enabled=1
gpgcheck=1
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg
[~] <pts/2>

そして「sudo yum install yarn」します

(1)ec2-user@ip-[9] yarn -v                            15:28 0216 Wed
1.22.17
[~] <pts/2>

そして「sudo npm install -g typescript ts-node typesync」この辺もインストールしました

(0)ec2-user@ip-[14] ts-node -v                        15:53 0216 Wed
v10.5.0
[~] <pts/1>
(0)ec2-user@ip-[23] tsc -v                            15:57 0216 Wed
Version 4.5.5
[~] <pts/1>

良いですね。
コメントのようなエイリアス書かなくても、実行できてます。このインストールが良いです。

ソースからのビルド v16.14.0、難

Building Node.js from source on supported platforms node/BUILDING.md at main · nodejs/node · GitHub より

Source Code node-v16.14.0.tar.gz をダウンロードしたうえで

sudo yum install python3 gcc-c++ make python3-pip
tar xvzf node-v16.14.0.tar.gz
cd node-v16.14.0/
./configure
[ec2-user@ip- node-v16.14.0]$ ./configure
Node.js configure: Found Python 3.7.10...
WARNING: C++ compiler (CXX=g++, 7.3.1) too old, need g++ 8.3.0 or clang++ 8.0.0
WARNING: warnings were emitted in the configure phase
INFO: configure completed successfully
[ec2-user@ip- node-v16.14.0]$

ふむ、「yum list gcc

Installed Packages
gcc.x86_64                      7.3.1-13.amzn2    

ちょっとバージョンが足りない模様。

ちょっとカーネル「 sudo amazon-linux-extras install kernel-5.10」してみるけど、「./configure」メッセージ変わらない。
一応 completed successfully というので作りましょう

[ec2-user@ip- node-v16.14.0]$ make -j4

ちょっと時間かかる、10分とかそれ以上、t3.microインスタンスです。
それで

g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make[1]: *** [/home/ec2-user/node-v16.14.0/out/Release/obj.target/torque_base/deps/v8/src/torque/cc-generator.o] Error 4
make[1]: *** Waiting for unfinished jobs....
rm 5761517c9fac3ccdca772ce0e817a3e42baba5f2.intermediate
make[1]: Leaving directory `/home/ec2-user/node-v16.14.0/out'
make: *** [node] Error 2

出来なかったです。

node-v15.14.0.tar.gz、難

https://nodejs.org/download/release/v15.14.0/ より、node-v15.14.0.tar.gz、「tar xvzf」して「./configure」

[ec2-user@ip- node-v15.14.0]$ ./configure
Node.js configure: Found Python 3.7.10...
INFO: configure completed successfully
[ec2-user@ip- node-v15.14.0]$

いいんでしょうか「make -j4」

[ec2-user@ip- node-v15.14.0]$ make -j4

20分とかかな、t3.microインスタンス

g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make[1]: *** [/home/ec2-user/node-v15.14.0/out/Release/obj.target/torque_base/deps/v8/src/torque/cfg.o] Error 4
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory `/home/ec2-user/node-v15.14.0/out'
make: *** [node] Error 2

失敗

node-v14.19.0.tar.gz、難

https://nodejs.org/download/release/v14.19.0/ より、node-v14.19.0.tar.gz
「./configure」

Node.js configure: Found Python 3.7.10...
INFO: configure completed successfully

「make -j4」
20分とかかな、t3.microインスタンス

g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make[1]: *** [/home/ec2-user/node-v14.19.0/out/Release/obj.target/torque_base/deps/v8/src/torque/cfg.o] Error 4
make[1]: *** Waiting for unfinished jobs....
rm 671b9b97581075a7d3d68486280cfbd3d862ba1e.intermediate
make[1]: Leaving directory `/home/ec2-user/node-v14.19.0/out'
make: *** [node] Error 2

失敗

それぞれ、エラー詳細までは検討せず。