0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSL2 に Node.js をインストール

Posted at

概要

WSL2 & Docker上に、ハンズオン ( 勉強環境 ) で使用する WordPress 環境を wp-env で作成したかったので、Node.js を WSL2にインストールしました。

環境

  • WSL2
    ディストリビューション: Ubuntu-24.04
  • Windows 11 Home 24H2

WSL 2 Docker Desktopとの統合( Powershell 7 にアップデート )

【任意】Docker を使用する場合

上のリンクから、
msi win x64 .msi のやつをインストール

> スタートメニューから PowerShell 7 (x64)
を実行

起動したら確認。

$PSVersionTable
Name                Value
----                    ----
PSVersion.        7.5.2

Windows 上の Docker Desktop WSL 2 バックエンド

【任意】Docker を使用する場合

参考:

前提

WindowsにWSL 2機能をインストール済み。
Docker Desktop をインストール済み、有効化済み。

最新のWSL バージョンに更新

wsl --update

上のリポジトリでLatest のリリースと一致を確認

Docker Desktop WSL 2 を有効にする

Docker Desktop を開く

設定> General > [ Use the WSL 2 based engine ] を選択。

WSL 2 ディストリビューションで Docker サポートを有効にする

ディストリビューションが WSL 2 モードで実行されることを確認します。WSL は、v1 モードと v2 モードの両方でディストリビューションを実行できます。
WSL モードを確認するには、次のコマンドを実行します。

 wsl.exe -l -v

将来のインストールで v2 をデフォルト バージョンとして設定するには、次のコマンドを実行します。

 wsl.exe --set-default-version 2

Docker-WSL統合するデフォルトのWSLディストリビューションを変更するには、以下を実行します
wsl.exe --set-default Ubuntu-24.04
Docker Desktop > 設定アイコン> Resourses > [ WSL integration ] で "enable integration with my default WSL distro" がオフかつ、"enable integration with additional distros Ubuntu-24.04 "をオンにした。その後、wsl --shutdown > PCシャットダウン

設定> Resources > [ WSL integration ]
enable integration with additional distros:

  • AlmaLinux-9 ( 現在インストールしているディストリビーション 1 )
    のチェックを外し、
  • Ubuntu-24.04 ( 同2 )
    をチェックして、
    Apply & restart

Linux で Docker を使用する場合

VS Code を使用してリモート コンテナーで開発するには以下を行う。

  • VS Code WSL 拡張機能をインストール
    Visual Studio Code
    Extensions>"WSL" で検索

  • VS Code Dev Containers 拡張機能をインストール
    Extensions>"dev containers ” で検索

  • VS Code Docker 拡張機能をインストール
    Extensions>"docker ” で検索

nvm, Node.js, npm のインストール

下記リンクのGitHub リポジトリからcurlでnvmをダウンロード。

  • まず、以下を実行 ( WSL の場合のみ)。WSL で、ネームサーバーの設定によってダウンロードできないことがあるので、事前に設定しておく。
.bash
# ここでは Linux の設定をする。

# ファイルの確認。
ls -al /etc/resolv*.conf
-rw-r--r-- 1 root root 19 Aug 13 19:28 /etc/resolv.conf

# すでにあればバックアップして削除
sudo cp -a /etc/resolv.conf /etc/resolv_bak.conf
sudo rm /etc/resolv.conf

# Google のネームサーバーを設定。
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

# ここからはWSLの設定

ls -al /etc/wsl.conf
-rw-r--r-- 1 root root 79 Aug 13 19:26 /etc/wsl.conf

# すでにあればバックアップして削除
sudo cp -a /etc/wsl.conf /etc/wsl_bak.conf
sudo rm /etc/wsl.conf

# ネームサーバーについての設定は、Windows側の WSL設定ではなく、Linux ( Ubuntuなど ) の resolv.conf を優先するようにする。
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'

# GitHub に書いてた公式マニュアル通りにする
sudo chattr +i /etc/resolv.conf

# 設定内容を確認。
cat /etc/resolv.conf
nameserver 8.8.8.8
sudo cat /etc/wsl.conf
[network]
generateResolvConf = false
  • nvm のインストール
.bash
# curl が入っている。
sudo dpkg -l | grep curl
ii  curl                                 8.5.0-2ubuntu10.6                        amd64        command line tool for transferring data with URL syntax

# 依存関係のパッケージリストを更新。
sudo apt-get update

# curl のインストール (アップデート)
sudo apt-get install -y curl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
curl is already the newest version (8.5.0-2ubuntu10.6).

# すでにNode.js がある場合は削除 (Microsoft 公式ページによる )
sudo dpkg -l | grep nodejs

# 自己責任で : (私の環境では Ubuntu-24.04 WSL ディストリーションに nodejs パッケージがインストールされていなかったので以下の apt-get remove コマンドは未検証)
sudo apt-get purge --auto-remove nodejs

# Download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16631  100 16631    0     0   3216      0  0:00:05  0:00:05 --:--:--  4010
=> Downloading nvm from git to '/home/kosuke/.nvm'
=> Cloning into '/home/kosuke/.nvm'...
remote: Enumerating objects: 383, done.
remote: Counting objects: 100% (383/383), done.
remote: Compressing objects: 100% (326/326), done.
remote: Total 383 (delta 43), reused 178 (delta 29), pack-reused 0 (from 0)
Receiving objects: 100% (383/383), 392.12 KiB | 807.00 KiB/s, done.
Resolving deltas: 100% (43/43), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/kosuke/.bashrc
=> Appending bash_completion source string to /home/kosuke/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# 上のメッセージで書いてあることを行う。
\. "$HOME/.nvm/nvm.sh"
  # This loads nvm

\. "$HOME/.nvm/bash_completion"
  # This loads nvm bash_completion

# Download and install Node.js:
nvm install 22
Downloading and installing node v22.18.0...
Downloading https://nodejs.org/dist/v22.18.0/node-v22.18.0-linux-x64.tar.xz...
################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.18.0 (npm v10.9.3)
Creating default alias: default -> 22 (-> v22.18.0)

# Verify the Node.js version:
node -v
v22.18.0
nvm current
v22.18.0

# Verify npm version:
npm -v
10.9.3

参考:

Node.js のリリースについて

Active LTS ( 2025/8/28 現在 v22 ) をインストールします。たしか Microsoft のページのどこかで 「 "Active LTS" をインストールしてね 」って書いてた気がする。

Node.js のインストール方法について:

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?