LoginSignup
4
5

More than 1 year has passed since last update.

2つの方法でnodeをインストール、yumの不思議な動作

Posted at

環境

環境:EC2
OS:Amazon Linux 2

通常のnodeインストールとyumの不思議

まずはnodejsをインストールしましょう!
nodejs 18が最新版のようですのでそれで行ってみましょう!
最初のステップはリポジトリの追加みたいですね、以下のコマンドで追加します。

[ec2-user@ip-10-0-251-210 ~]$ sudo yum install -y gcc-c++ make
...(略)
Complete!
[ec2-user@ip-10-0-251-210 ~]$ curl -sL https://rpm.nodesource.com/setup_18.x | sudo -E bash -

## Installing the NodeSource Node.js 18.x repo...
...(略)
## Run `sudo yum install -y nodejs` to install Node.js 18.x and npm.
...(略)

OK、良さそうです!インストールのコマンドを実行します!

[ec2-user@ip-10-0-251-210 ~]$ sudo yum install -y nodejs
Failed to set locale, defaulting to C
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
nodesource                                               | 2.5 kB     00:00
nodesource/x86_64/primary_db                               |  37 kB   00:00
277 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:18.14.2-1nodesource will be installed
--> Processing Dependency: libc.so.6(GLIBC_2.28)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Processing Dependency: libm.so.6(GLIBC_2.27)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Finished Dependency Resolution
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libc.so.6(GLIBC_2.28)(64bit)
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libm.so.6(GLIBC_2.27)(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

あれ? Requires: libc.so.6(GLIBC_2.28)(64bit)となると、このlibc.so.6(GLIBC_2.28)(64bit)をインストールしなくちゃみたいです。

ちょっと調べてみたら、nodejs 18はAmazon Linux 2と互換性があまり良くなさそうです。
https://itneko.com/amazon-linux2-nodejs18/

ですので、18じゃなくて、16をインストールしようと思います!

じゃあ、まずは16のリポジトリを追加します!

[ec2-user@ip-10-0-251-210 ~]$ curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -

## Installing the NodeSource Node.js 16.x repo...
...(略)
## Run `sudo yum install -y nodejs` to install Node.js 16.x and npm.
...(略)

よし、良さそうですね!sudo yum install -y nodejsコマンドでnodejsをインストールします!

[ec2-user@ip-10-0-251-210 ~]$ sudo yum install -y nodejs
Failed to set locale, defaulting to C
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
277 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:18.14.2-1nodesource will be installed
--> Processing Dependency: libc.so.6(GLIBC_2.28)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Processing Dependency: libm.so.6(GLIBC_2.27)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Finished Dependency Resolution
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libc.so.6(GLIBC_2.28)(64bit)
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libm.so.6(GLIBC_2.27)(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

え、ちょっと待って、また18をインストールしようとしました?16のリポジトリを追加したのに。。。

まあ、node関連のRPMパッケージを全部削除してまたインストールしてみます!

[ec2-user@ip-10-0-251-210 ~]$ rpm -qa | grep -i node
nodesource-release-el7-1.noarch
[ec2-user@ip-10-0-251-210 ~]$ sudo yum remove nodesource-release-el7-1.noarch -y
...(略)
Removed:
  nodesource-release.noarch 0:el7-1

Complete!

ファイルが/etc/yum.repos.dから消えていることも確認しましょう。

[ec2-user@ip-10-0-251-210 ~]$ ls -a /etc/yum.repos.d | grep node
[ec2-user@ip-10-0-251-210 ~]$

良さそうです!じゃあ、もう一回インストールしてみます!

[ec2-user@ip-10-0-251-210 ~]$ curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
(略)
[ec2-user@ip-10-0-251-210 ~]$ sudo yum install -y nodejs
Failed to set locale, defaulting to C
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
277 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:18.14.2-1nodesource will be installed
--> Processing Dependency: libc.so.6(GLIBC_2.28)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Processing Dependency: libm.so.6(GLIBC_2.27)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Finished Dependency Resolution
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libc.so.6(GLIBC_2.28)(64bit)
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libm.so.6(GLIBC_2.27)(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

うそ、まだ18になっています!

実は、sudo yum remove {パッケージ}でRPMパッケージを削除してもキャッシュが残ります。

もう一度消して見てみましょう。

[ec2-user@ip-10-0-251-210 ~]$ sudo yum remove nodesource-release-el7-1.noarch -y
...
Removed:
  nodesource-release.noarch 0:el7-1

Complete!
[ec2-user@ip-10-0-251-210 ~]$ ls -a /etc/yum.repos.d | grep node
[ec2-user@ip-10-0-251-210 ~]$
[ec2-user@ip-10-0-251-210 ~]$ ls /var/cache/yum/x86_64/2 | grep nodesource
nodesource
nodesource-source

あら、そこにあったんですね!

手動で削除してみます。

[ec2-user@ip-10-0-251-210 ~]$ sudo rm -rf /var/cache/yum/x86_64/2/nodesource
[ec2-user@ip-10-0-251-210 ~]$ sudo rm -rf /var/cache/yum/x86_64/2/nodesource-source/

もう一回インストールしてみると。。。

[ec2-user@ip-10-0-251-210 ~]$ curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
(略)
[ec2-user@ip-10-0-251-210 ~]$ sudo yum install -y nodejs
...(略)
Installed:
  nodejs.x86_64 2:16.19.1-1nodesource

Complete!

今回は成功しました! :tada:

あとで調べて気づいたんですが、yum cleanで同じぐらいなRPMパッケージとキャッシュ削除ができそうです。

バージョンを確認しましょう。

[ec2-user@ip-10-0-251-210 ~]$ node --version
v16.19.1
[ec2-user@ip-10-0-251-210 ~]$ npm --version
8.19.3

バージョンは正しそうです!

でもOSにインストールすることはやっぱりめんどくさいです。Dockerを使いましょう!

dockerとdocker-composeのインストール、nodeのdockerコンテナ立ち上げ

[ec2-user@ip-10-0-251-210 ~]$ sudo amazon-linux-extras install -y docker
Installing docker
...(略)
Installed:
  docker.x86_64 0:20.10.17-1.amzn2.0.2

Complete!
...(略)

インストール完了です!起動させてステータスを確認します。

[ec2-user@ip-10-0-251-210 ~]$ sudo systemctl start docker
[ec2-user@ip-10-0-251-210 ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-03-09 17:22:40 UTC; 5s ago
...(略)

順調そうです!次はdocker-composeをインストールします。

[ec2-user@ip-10-0-251-210 ~]$ sudo mkdir -p /usr/local/lib/docker/cli-plugins
[ec2-user@ip-10-0-251-210 ~]$ VER=2.4.1
[ec2-user@ip-10-0-251-210 ~]$ sudo curl \
>   -L https://github.com/docker/compose/releases/download/v${VER}/docker-compose-$(uname -s)-$(uname -m) \
>   -o /usr/local/lib/docker/cli-plugins/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 25.2M  100 25.2M    0     0  8885k      0  0:00:02  0:00:02 --:--:-- 11.3M
[ec2-user@ip-10-0-251-210 ~]$ sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
[ec2-user@ip-10-0-251-210 ~]$ sudo ln -s /usr/local/lib/docker/cli-plugins/docker-compose /usr/local/bin/docker-compose

docker-composeのインストール完了です!バージョンを確認しましょう。

[ec2-user@ip-10-0-251-210 ~]$ docker-compose --version
Docker Compose version v2.4.1

よし、全部いけそうです!

composeファイルを書いてコンテナを立ち上げるだけですね。

まず、コード用のフォルダーを作成してその中に移動します。

mkdir node-test-project
cd node-test-project

続いて、以下のファイルを作成して保存します。

# docker-compose.yml

version: '3.7'
services:
  node:
    image: node:18.15.0-slim
    container_name: node
    tty: true
    ports:
      - 3000:3000
    logging:
      driver: "json-file"
      options:
        max-size: "50m"

最後は立ち上げるだけです!

[ec2-user@ip-10-0-251-210 node-test-project]$ docker-compose up -d
[+] Running 6/6
 ⠿ node Pulled                         6.0s
   ⠿ 3f9582a2cbe7 Pull complete        1.7s
   ⠿ 94e5d5746476 Pull complete        1.8s
   ⠿ 11587a4aecf2 Pull complete        3.4s
   ⠿ d1ed1db27c1b Pull complete        3.5s
   ⠿ 8f4c41bcb791 Pull complete        3.6s
[+] Running 2/2
 ⠿ Network node-test-project_default  Created  0.0s
 ⠿ Container node                     Started  1.7s

コンテナが起動していることとnodeとnpmのバージョンを確認しましょう!

[ec2-user@ip-10-0-251-210 node-test-project]$ docker-compose ps
NAME     COMMAND                  SERVICE     STATUS      PORTS
node     "docker-entrypoint.s…"   node        running     0.0.0.0:3000->3000/tcp, :::3000->3000/tcp
[ec2-user@ip-10-0-251-210 node-test-project]$ docker exec node node --version
v18.15.0
[ec2-user@ip-10-0-251-210 node-test-project]$ docker exec node npm --version
9.5.0

dockerのインストール自体がちょっとめんどくさいですが、
インストールできたらどんなソフトでもすぐに使えることがすごいですね。 :sunglasses:

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