【Amazon Linux 2】 間違えて追加した Node.js の yum リポジトリを削除して別のバージョンの Node.js リポジトリを追加する

最初に以下コマンドで Node.js 18 のリポジトリを追加してインストールしようとしたのだが、Amazon Linux 2 には Node.js 18 が要求する依存ライブラリのうち足りないものがあり、インストールができなかった。

# curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
# yum install --enablerepo=nodesource nodejs
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                                            | 3.7 kB  00:00:00
224 packages excluded due to repository priority protections
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nodejs.aarch64 2:18.12.1-1nodesource を インストール
--> 依存性の処理をしています: libc.so.6(GLIBC_2.28)(64bit) のパッケージ: 2:nodejs-18.12.1-1nodesource.aarch64
--> 依存性の処理をしています: libm.so.6(GLIBC_2.27)(64bit) のパッケージ: 2:nodejs-18.12.1-1nodesource.aarch64
--> 依存性解決を終了しました。
エラー: パッケージ: 2:nodejs-18.12.1-1nodesource.aarch64 (nodesource)
             要求: libm.so.6(GLIBC_2.27)(64bit)
エラー: パッケージ: 2:nodejs-18.12.1-1nodesource.aarch64 (nodesource)
             要求: libc.so.6(GLIBC_2.28)(64bit)
 問題を回避するために --skip-broken を用いることができます。
 これらを試行できます: rpm -Va --nofiles --nodigest

代わりに Node.js 16 を入れようとしたのだが、すでに 18用のリポジトリがある状態で 16 のリポジトリを追加したい場合、どうすればいいんだ?と迷った。ひとまず、16用のリポジトリを追加する以下のコマンドを実行すれば 16用のリポジトリ情報で上書きできるのでは?と思った。しかし、これではダメだった。 yum info nodejs の結果が 18.12 のままだし、インストールを実行しようとしても 18 の依存性確認に進んでしまい、当然失敗する。

# curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
# yum info nodejs
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
224 packages excluded due to repository priority protections
利用可能なパッケージ
名前                : nodejs
アーキテクチャー    : aarch64
エポック            : 2
バージョン          : 18.12.1
リリース            : 1nodesource
容量                : 33 M
リポジトリー        : nodesource/aarch64
要約                : JavaScript runtime
URL                 : http://nodejs.org
ライセンス          : MIT and ASL 2.0 and ISC and BSD
説明                : Node.js is a platform built on Chrome's JavaScript runtime
                    : for easily building fast, scalable network applications.
                    : Node.js uses an event-driven, non-blocking I/O model that
                    : makes it lightweight and efficient, perfect for data-intensive
                    : real-time applications that run across distributed devices.

結論

下記の手順を踏む必要があった。

  • リポジトリファイルの物理的に削除
  • キャッシュファイルを物理的に削除
  • Node.js 16のリポジトリを再追加

リポジトリファイルの物理的削除

追加したリポジトリは、 /etc/yum.repos.d/ 配下に置かれており、これを物理的に削除するしかなかった。 (yum や rpm コマンドで削除できるならばそのほうが正しい手段だったのだろうが、それはできなかった)

# ls -l /etc/yum.repos.d/ | grep node
-rw-r--r-- 1 root root  474 11月 26  2020 nodesource-el7.repo
[root@ip-172-31-34-174 ec2-user]# rm -rf /etc/yum.repos.d/nodesource-el7.repo

キャッシュファイルを物理的に削除

以下2行がとにかく重要で、両方とも必須。 これをやっていなかったばかりに、この後の手順で新たに node 16 のリポジトリを追加したはずなのに、 yum info nodejs の結果が 18.12 から変わらず、無駄な時間を浪費してしまった。

yum clean all
rm -rf /var/cache/yum

キャッシュを侮ってはいけない。

16のリポジトリを再追加

これで再度 yum info nodejs を実行したところ、今度は Amazon Linux に標準で入っているNodeのバージョンに変わった。(ただしこれは相当古い)

# yum info nodejs
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
224 packages excluded due to repository priority protections
利用可能なパッケージ
名前                : nodejs
アーキテクチャー    : aarch64
エポック            : 1
バージョン          : 6.17.1
リリース            : 1.el7
容量                : 4.5 M
リポジトリー        : epel/aarch64
要約                : JavaScript runtime
URL                 : http://nodejs.org/
ライセンス          : MIT and ASL 2.0 and ISC and BSD
説明                : Node.js is a platform built on Chrome's JavaScript runtime
                    : for easily building fast, scalable network applications.
                    : Node.js uses an event-driven, non-blocking I/O model that
                    : makes it lightweight and efficient, perfect for data-intensive
                    : real-time applications that run across distributed devices.

改めて Node.16 のリポジトリを追加する。
もし yum info nodejs の結果が引き続き 18.12 と表示されるならば、それはキャッシュが削除しきれていないことが原因。

# curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
# yum info nodejs --enablerepo=nodesource
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
224 packages excluded due to repository priority protections
利用可能なパッケージ
名前                : nodejs
アーキテクチャー    : aarch64
エポック            : 2
バージョン          : 16.18.1
リリース            : 1nodesource
容量                : 32 M
リポジトリー        : nodesource/aarch64
要約                : JavaScript runtime
URL                 : http://nodejs.org
ライセンス          : MIT and ASL 2.0 and ISC and BSD
説明                : Node.js is a platform built on Chrome's JavaScript runtime
                    : for easily building fast, scalable network applications.
                    : Node.js uses an event-driven, non-blocking I/O model that
                    : makes it lightweight and efficient, perfect for data-intensive
                    : real-time applications that run across distributed devices.

満を持してインストール。バージョンが 16.18 であることを確認し、問題なければ y で続行する。

# yum install nodejs --enablerepo=nodesource
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
224 packages excluded due to repository priority protections
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nodejs.aarch64 2:16.18.1-1nodesource を インストール
--> 依存性解決を終了しました。

依存性を解決しました

============================================================================================================
 Package             アーキテクチャー     バージョン                          リポジトリー             容量
============================================================================================================
インストール中:
 nodejs              aarch64              2:16.18.1-1nodesource               nodesource               32 M

トランザクションの要約
============================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 32 M
インストール容量: 90 M
Is this ok [y/d/N]:

最後にバージョン確認をして終了。

$ node --version
v16.18.1
執筆日: