「古いNode.jsがaptコマンドでインストールされてしまう・・・」
「Ubuntu 22.04 LTSに最新版のNode.jsをインストールしたい」
このような場合には、この記事の内容が参考となります。
この記事では、Ubuntu 22.04 LTSに最新版Node.jsをインストールする方法を解説しています。
本記事の内容
- Ubuntu 22.04におけるOS標準のNode.js
- Ubuntu 22.04における最新版Node.jsのパッケージ取得
- Ubuntu 22.04への最新版Node.jsのインストール
それでは、上記に沿って解説していきます。
Ubuntu 22.04におけるOS標準のNode.js
Ubuntuには、OS標準のソフトウェアが存在しています。
Apacheのような人気ソフトウェアであれば、ほぼ確実にOS標準が存在します。
そして、Node.jsにもOS標準のソフトウェアが存在しています。
Ubuntu 22.04 LTSであれば、以下がUbuntu公式リポジトリに登録されています。
$ sudo apt show nodejs Package: nodejs Version: 12.22.9~dfsg-1ubuntu3 Priority: extra Section: universe /web Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@alioth-lists.debian.net> Bugs: https: //bugs .launchpad.net /ubuntu/ +filebug Installed-Size: 932 kB Provides: node-types-node (= 12.20.42~12.22.9~dfsg-1ubuntu3) Depends: libc6 (>= 2.34), libnode72 (= 12.22.9~dfsg-1ubuntu3) Recommends: ca-certificates, nodejs-doc Suggests: npm Breaks: node-babel-runtime (<< 7), node-typescript-types (<< 20210110~) Homepage: https: //nodejs .org/ Download-Size: 122 kB APT-Sources: http: //archive .ubuntu.com /ubuntu jammy /universe amd64 Packages Description: evented I /O for V8 javascript - runtime executable 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 is bundled with several useful libraries to handle server tasks: . System, Events, Standard I /O , Modules, Timers, Child Processes, POSIX, HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings. |
Node.js 12.22.9が、Ubuntu 22.04におけるOS標準ということです。
このバージョンのNode.jsは、2022年1月10日にリリースされています。

このバージョン自体は、そこまで古くありません。
しかし、Node.js 12はかなり古いです。
実際、現時点(2022年5月)ではサポートが完了しています。
公式のサポート状況を以下のページで確認できます。
https://github.com/nodejs/Release#release-schedule

この表を見ると、なんとNode.js 12はもうサポートが終了しています。
存在すらしていません・・・
今後は、16以降を利用するのが無難と言えます。
「Active LTS」であるから、最も安定していると言えるでしょう。
ちなみに、Node.jsでは偶数バージョンがLTSになります。
そうなると、現在開発中の18系が次のLTSとなるということです。
とにかく、Ubuntu 22.04でOS標準となっているNode.jsはインストールしない方がよいでしょう。
そもそも、Node.js 12はもうNode.jsの開発元では「終わった」扱いです。
以上、Ubuntu 22.04におけるOS標準のNode.jsについて説明しました。
次は、Ubuntu 22.04における最新版Node.jsのパッケージ取得について説明します。
Ubuntu 22.04における最新版Node.jsのパッケージ取得
PPAを利用して最新版のパッケージを利用します。
PPAは、非公式リポジトリになります。
この場合の公式とは、Ubuntu公式のことです。
これを先ほどから、OS標準と言っています。
ただ、PPAと言っても、Node.jsが公認しているモノを利用します。
通常は、PPAを利用するには以下の作業が必要です。
- リポジトリの追加
- パッケージの更新
そして、これらを一気にやってくれるスクリプトをNodeSourceという会社が用意しています。
NodeSource
https://nodesource.com/
追記 2023年11月04日
このスクリプトを用いた方法ではインストールできなくなっています。
NodeSourceと喧嘩別れでもしたのでしょうか?
その代わりのインストール方法を次の記事で説明しています。
そのスクリプトは、以下のコマンドで取得・実行可能です。
3つのバージョンの分を載せておきます。
Node.js 16系
$ curl -fsSL https: //deb .nodesource.com /setup_16 .x | sudo -E bash - |
Node.js 17系
$ curl -fsSL https: //deb .nodesource.com /setup_17 .x | sudo -E bash - |
Node.js 18系
$ curl -fsSL https: //deb .nodesource.com /setup_18 .x | sudo -E bash - |
アクティブなLTSの最新と言えば、Node.js 16になります。
また、Node.js 17はもうお役御免でメンテンナンス状態に入りました。
開発中となるのは、ダウンロード可能な次期LTS版のNode.js 18です。
このままであれば、Node.js 18は2022年10月25日にアクティブなLTSとなります。
したがって、今回はNode.js 18を最新版として取得します。
そのためのコマンドを実行した結果が、以下となります。
$ 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 取得:1 http: //security .ubuntu.com /ubuntu jammy-security InRelease [110 kB] 無視:2 https: //repo .mongodb.org /apt/ubuntu focal /mongodb-org/5 .0 InRelease ヒット:3 http: //archive .ubuntu.com /ubuntu jammy InRelease 取得:4 http: //archive .ubuntu.com /ubuntu jammy-updates InRelease [109 kB] ヒット:5 https: //packages .redis.io /deb jammy InRelease ヒット:6 https: //repo .mongodb.org /apt/ubuntu focal /mongodb-org/5 .0 Release 取得:8 https: //nginx .org /packages/ubuntu jammy InRelease [2,840 B] ヒット:9 https: //ppa .launchpadcontent.net /ondrej/apache2/ubuntu jammy InRelease ヒット:10 http: //archive .ubuntu.com /ubuntu jammy-backports InRelease 取得:11 http: //archive .ubuntu.com /ubuntu jammy-updates /restricted amd64 Packages [50.7 kB] 取得:12 http: //archive .ubuntu.com /ubuntu jammy-updates /universe amd64 Packages [63.5 kB] 337 kB を 2秒 で取得しました (148 kB /s ) パッケージリストを読み込んでいます... 完了 W: https: //repo .mongodb.org /apt/ubuntu/dists/focal/mongodb-org/5 .0 /Release .gpg: Key is stored in legacy trusted.gpg keyring ( /etc/apt/trusted .gpg), see the DEPRECATION section in apt-key(8) for details. W: https: //nginx .org /packages/ubuntu/dists/jammy/InRelease : Key is stored in legacy trusted.gpg keyring ( /etc/apt/trusted .gpg), see the DEPRECATION section in apt-key(8) for details. ## 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 無視:1 https: //repo .mongodb.org /apt/ubuntu focal /mongodb-org/5 .0 InRelease ヒット:2 https: //repo .mongodb.org /apt/ubuntu focal /mongodb-org/5 .0 Release ヒット:4 http: //security .ubuntu.com /ubuntu jammy-security InRelease ヒット:5 http: //archive .ubuntu.com /ubuntu jammy InRelease 取得:6 https: //deb .nodesource.com /node_18 .x jammy InRelease [4,563 B] ヒット:7 http: //archive .ubuntu.com /ubuntu jammy-updates InRelease ヒット:8 https: //packages .redis.io /deb jammy InRelease ヒット:9 https: //ppa .launchpadcontent.net /ondrej/apache2/ubuntu jammy InRelease ヒット:10 http: //archive .ubuntu.com /ubuntu jammy-backports InRelease 取得:11 https: //nginx .org /packages/ubuntu jammy InRelease [2,840 B] 取得:12 https: //deb .nodesource.com /node_18 .x jammy /main amd64 Packages [773 B] 8,176 B を 1秒 で取得しました (5,521 B /s ) パッケージリストを読み込んでいます... 完了 W: https: //repo .mongodb.org /apt/ubuntu/dists/focal/mongodb-org/5 .0 /Release .gpg: Key is stored in legacy trusted.gpg keyring ( /etc/apt/trusted .gpg), see the DEPRECATION section in apt-key(8) for details. W: https: //nginx .org /packages/ubuntu/dists/jammy/InRelease : Key is stored in legacy trusted.gpg keyring ( /etc/apt/trusted .gpg), see the DEPRECATION section in apt-key(8) for details. ## 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 |
上記の内容より、以下の処理を実行していることがわかります。
- リポジトリの追加
- パッケージの更新
リポジトリの追加は、「/etc/apt/sources.list.d」の下にファイルを作成する形式です。
ファイルの内容を確認してみましょう。
$ cat /etc/apt/sources .list.d /nodesource .list deb [signed-by= /usr/share/keyrings/nodesource .gpg] https: //deb .nodesource.com /node_18 .x jammy main deb-src [signed-by= /usr/share/keyrings/nodesource .gpg] https: //deb .nodesource.com /node_18 .x jammy main |
では、再度パッケージを確認しましょう。
すでにパッケージの更新も済んでいます。
$ sudo apt show nodejs Package: nodejs Version: 18.1.0-deb-1nodesource1 Priority: optional Section: web Maintainer: Ivan Iguaran <ivan@nodesource.com> Installed-Size: 184 MB Provides: nodejs-dev, nodejs-doc, nodejs-legacy, npm Depends: libc6 (>= 2.17), libgcc1 (>= 1:3.4), libstdc++6 (>= 5.2), python3-minimal, ca-certificates Conflicts: nodejs-dev, nodejs-doc, nodejs-legacy, npm Replaces: nodejs-dev (<= 0.8.22), nodejs-legacy, npm (<= 1.2.14) Homepage: https: //nodejs .org Download-Size: 27.7 MB APT-Sources: https: //deb .nodesource.com /node_18 .x jammy /main amd64 Packages Description: Node.js event-based server-side javascript engine Node.js is similar in design to and influenced by systems like Ruby 's Event Machine or Python' s Twisted. . It takes the event model a bit further - it presents the event loop as a language construct instead of as a library. . Node.js is bundled with several useful libraries to handle server tasks : System, Events, Standard I /O , Modules, Timers, Child Processes, POSIX, HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings. |
現時点(2022年5月)における最新バージョンのNode.js 18.1.0を確認できます。

これにより、最新版のNode.jsをインストールする準備が整いました。
Ubuntu 22.04への最新版Node.jsのインストール
Node.jsのインストールは、次のコマンドを実行するだけです。
sudo apt-get install -y nodejs |
インストールは、すぐに終わります。
それでは、確認を行います。
$ node --version v18.1.0 |
パッケージ管理システムのnpmも確認しておきます。
$ npm --version 8.8.0 |
問題なく動いていますね。
以上、Ubuntu 22.04 LTSへの最新版Node.jsのインストールについて説明しました。