Ubuntu 22.04 LTS(WSL)へのNode.jsのインストール(非推奨)

はじめに

Zenn CLIの利用のため、Node.jsをインストールします。

Ubuntu22.04のaptでインストールできるパッケージを確認したところバージョンがだいぶ古いため、イントール方法を調査しました。

2023.10.22追記

2023.10.22現在、本ページ記載のやり方で実施すると推奨されない警告が表示されます。

$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

================================================================================
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
================================================================================

                           SCRIPT DEPRECATION WARNING


  This script, located at https://deb.nodesource.com/setup_X, used to
  install Node.js is deprecated now and will eventually be made inactive.

  Please visit the NodeSource distributions Github and follow the
  instructions to migrate your repo.
  https://github.com/nodesource/distributions

  The NodeSource Node.js Linux distributions GitHub repository contains
  information about which versions of Node.js and which Linux distributions
  are supported and how to install it.
  https://github.com/nodesource/distributions


                          SCRIPT DEPRECATION WARNING

================================================================================
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
================================================================================

TO AVOID THIS WAIT MIGRATE THE SCRIPT
Continuing in 60 seconds (press Ctrl-C to abort) ...

下記のリンクを参照に、別の方法で行った方が良さそうです。

GitHub - nodesource/distributions: NodeSource Node.js Binary Distributions
NodeSource Node.js Binary Distributions. Contribute to nodesource/distributions development by creating an account on GitHub.

Ubuntu 22.04 LTS(WSL)へのNode.jsのインストール に新しい方法を記載しました。

バージョン選択

Zenn CLIを使うにはNode.js 14以上が必要とのこと。

Node.js - Wikipedia

2023.8.11現在、Active LTSは18とのことなので、これをインストールすることにします。

手順

作業前は下記の通り12.22.9がインストール可能でした。

$ sudo apt list | grep nodejs

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

netdata-plugins-nodejs/jammy 1.33.1-1ubuntu1 all
nodejs-doc/jammy 12.22.9~dfsg-1ubuntu3 all
nodejs/jammy 12.22.9~dfsg-1ubuntu3 amd64

リポジトリ等を登録するスクリプトを実行します。

$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

aptで取得できるパッケージが所定のバージョンに変ります。

$ sudo apt list | grep nodejs

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

netdata-plugins-nodejs/jammy 1.33.1-1ubuntu1 all
nodejs-doc/jammy 12.22.9~dfsg-1ubuntu3 all
nodejs/unknown 18.17.1-deb-1nodesource1 amd64

下記によりインストールします。

$ sudo apt install nodejs

参考

setup_18.x の実行結果

$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

## Installing the NodeSource Node.js 18.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:6 http://security.ubuntu.com/ubuntu jammy-security InRelease
Reading package lists... Done

## Confirming "jammy" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_18.x/dists/jammy/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 18.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 https://download.docker.com/linux/ubuntu jammy InRelease
Get:2 https://deb.nodesource.com/node_18.x jammy InRelease [4563 B]
Get:3 https://deb.nodesource.com/node_18.x jammy/main amd64 Packages [776 B]
Hit:4 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:5 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:6 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:8 http://security.ubuntu.com/ubuntu jammy-security InRelease
Fetched 5339 B in 4s (1479 B/s)
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 18.x and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
     echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     sudo apt-get update && sudo apt-get install yarn
Copied title and URL