「古いNode.jsがaptコマンドでインストールされると困る」
「Ubuntu 20.04 LTSに最新版のNode.jsをインストールしたい」
このような場合には、この記事が参考となります。
この記事では、Ubuntu 20.04 LTSに最新版Node.jsをインストールする方法を解説しています。
もちろん、aptコマンドによる簡単インストールです。
本記事の内容
- OS標準のNode.js
- 最新版Node.jsのパッケージ取得
- 最新版Node.jsのインストール
それでは、上記に沿って解説していきます。
OS標準のNode.js
Ubuntuには、OS標準のソフトウェアが存在しています。
Nginxのような人気ソフトウェアであれば、ほぼ確実にOS標準が存在します。
もちろん、Node.jsにもOS標準のソフトウェアが存在しています。
Ubuntu 20.04 LTSであれば、それが以下となります。
$ sudo apt show nodejs Package: nodejs Version: 10.19.0~dfsg-3ubuntu1 Priority: extra Section: universe/web Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 158 kB Depends: libc6 (>= 2.4), libnode64 (= 10.19.0~dfsg-3ubuntu1) Recommends: ca-certificates, nodejs-doc Suggests: npm Conflicts: nodejs-legacy Replaces: nodejs-legacy Homepage: http://nodejs.org/ Download-Size: 61.1 kB APT-Sources: http://archive.ubuntu.com/ubuntu focal/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 10.19.0が、Ubuntu 20.04におけるOS標準ということです。
このバージョンのNode.jsは、2020年2月にリリースされています。

実際、そこまで昔という訳ではありません。
しかし、最新版バージョンはNode.js 17.0.1です。

メインバージョンで7つも前というのは、かなり厳しいでしょう。
そもそも、バージョンが上がるには理由があります。
セキュリティの事情が、最も大きいはずです。
Node.jsのように、外部からの攻撃を受ける可能性があるソフトウェアは特に。
あとは、ライブラリも古いバージョンだと問題がありそうです。
ライブラリをインストールできないというケースも普通にあるでしょう。
このようなことからも、やはりNode.jsは最新を入れておきたいです。
まずは、そのために最新版Node.jsのパッケージを手にいれます。
最新版Node.jsのパッケージ取得
Ubuntuを使っている以上は、aptコマンドでインストールを管理しましょう。
それ以外のインストールは、基本的にはナシとします。
通常、aptで最新版のパッケージをインストールできるようにするには次の手順が必要です。
ただし、今回はこれらの処理を自分でやる必要はありません。
自動的にやってくれるスクリプトが提供されています。
- リポジトリの追加
- パッケージの更新
リポジトリの追加には、以下の方法があります。
- /etc/apt/sources.listに追記
- /etc/apt/sources.list.dにファイルを設置
今回のケースであれば、次のような情報をリポジトリに追加することになります。
deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_17.x focal main deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_17.x focal main
リポジトリの追加が終わったら、パッケージの更新を行います。
通常は、公開鍵のインポートがその前に必要な処理となります。
どこの馬の骨かわからないパッケージを受け入れるのは危険ですからね。
そのための認証作業が、公開鍵のインポートということです。
とにかく、いろいろとそれなりに処理があります。
しかし、Node.jsにはそれらを一気に自動でやってくれるスクリプトが用意されています。
そのスクリプト(パッケージ含め)を用意しているのは、NODESOURCEです。
Node.jsが公認して、かつ、企業として運営しているので安心できます。
https://nodesource.com/
では、そのスクリプトを取得(同時に処理も実行される)しましょう。
これもコマンドが用意されています。

実際に、コマンドを実行した結果が以下となります。
$ curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash - ## Installing the NodeSource Node.js 17.x repo... ## Populating apt-get cache... + apt-get update Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease Hit:2 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease Hit:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease Reading package lists... Done ## Confirming "focal" is supported... + curl -sLf -o /dev/null 'https://deb.nodesource.com/node_17.x/dists/focal/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 17.x repo... + echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_17.x focal main' > /etc/apt/sources.list.d/nodesource.list + echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_17.x focal main' >> /etc/apt/sources.list.d/nodesource.list ## Running `apt-get update` for you... + apt-get update Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease Hit:3 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease Get:5 https://deb.nodesource.com/node_17.x focal InRelease [4583 B] Hit:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease Get:7 https://deb.nodesource.com/node_17.x focal/main amd64 Packages [764 B] Fetched 5347 B in 1s (3848 B/s) Reading package lists... Done ## Run `sudo apt-get install -y nodejs` to install Node.js 17.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_17.x focal main deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_17.x focal main
では、再度パッケージを確認しましょう。
$ sudo apt show nodejs Package: nodejs Version: 17.0.1-deb-1nodesource1 Priority: optional Section: web Maintainer: Ivan Iguaran <ivan@nodesource.com> Installed-Size: 179 MB Provides: nodejs-dev, nodejs-legacy, npm Depends: libc6 (>= 2.17), libgcc1 (>= 1:3.4), libstdc++6 (>= 4.8), python3-minimal, ca-certificates Conflicts: nodejs-dev, nodejs-legacy, npm Replaces: nodejs-dev (<= 0.8.22), nodejs-legacy, npm (<= 1.2.14) Homepage: https://nodejs.org Download-Size: 26.5 MB APT-Sources: https://deb.nodesource.com/node_17.x focal/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. N: There is 1 additional record. Please use the '-a' switch to see it
最新バージョンのNode.js 17.0.1に更新されています。
これにより、最新版のNode.jsをインストールする準備が整いました。
最新版Node.jsのインストール
まずは、OSのバージョンを確認しておきます。
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"
Node.jsのインストールは、次のコマンドを実行するだけです。
sudo apt-get install -y nodejs
インストールは、すぐに終わります。
それでは、確認を行います。
$ node --version v17.0.1
$ npm --version 8.1.0
それぞ、次の通りです。

問題ありませんね。
以上、Ubuntu 20.04 LTSへの最新版Node.jsのインストールについて説明しました。