Ubuntu24.04 最新のnode.jsをnvmを使用してインストールする

Ubuntu24.04 最新のnode.jsをnvmを使用してインストールする

Ubuntu24.04に最新のnode.jsをnvmを使用してインストールするまでの手順を記述してます。

環境

  • OS Ubuntu24.04
  • node.js v22.2.0

node.jsインストール

まずは、以下のshellをダウンロードして実行します。

$ wget -q -O- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

=> Downloading nvm from git to '/home/foo/.nvm'
=> Cloning into '/home/foo/.nvm'...
remote: Enumerating objects: 365, done.
remote: Counting objects: 100% (365/365), done.
remote: Compressing objects: 100% (313/313), done.
remote: Total 365 (delta 43), reused 165 (delta 26), pack-reused 0
Receiving objects: 100% (365/365), 365.08 KiB | 5.53 MiB/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/foo/.bashrc
=> Appending bash_completion source string to /home/foo/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/usr/local/lib
├── corepack@0.28.0
├── n@9.2.3
└── yarn@1.22.22
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> 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
foo@foo-001:~$ . ~/.bashrc

バージョンは以下で確認できます。

$ nvm --version
0.39.7

インストールが終わったので「node.js」をインストールします。

$ nvm install node

Downloading and installing node v22.2.0...
Downloading https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-x64.tar.xz...
########################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.2.0 (npm v10.7.0)
Creating default alias: default -> node (-> v22.2.0)

これでインストール完了です。

$ node -v
v22.2.0

「npm」は最新になっていない場合があるのでアップデートしておきます。

$ npm -v

10.7.0

$ npm update -g npm

removed 2 packages, and changed 53 packages in 3s

24 packages are looking for funding
  run `npm fund` for details

$ npm -v
10.8.0