apt を使って Node.js の任意のバージョンをインストールする

CI にて有用な方法と思います。

結論

たとえば、 v16 系を入れたい場合には以下のコマンドを実行します。

$ curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
$ sudo apt update
$ sudo apt install -y nodejs

補足

npm も最新版まで上げるには以下のコマンドを実行します。

$ npm install -g npm

参考

上記の curl コマンド の URL として https://deb.nodesource.com/setup を指定した際に以下のメッセージが表示されます。このメッセージが参考になります。

================================================================================
================================================================================

                           SCRIPT DEPRECATION WARNING                         

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

  You should use the script that corresponds to the version of Node.js you
  wish to install. e.g.

   * https://deb.nodesource.com/setup_12.x — Node.js 12 LTS "Erbium"
   * https://deb.nodesource.com/setup_14.x — Node.js 14 LTS "Fermium" (recommended)
   * https://deb.nodesource.com/setup_15.x — Node.js 15 "Fifteen"
   * https://deb.nodesource.com/setup_16.x — Node.js 16 "Gallium"
  Please see https://github.com/nodejs/Release for details about which
  version may be appropriate for you.

  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 use the install scripts.
    https://github.com/nodesource/distributions

================================================================================
================================================================================
Powered by はてなブログ