CLOVER🍀

That was when it all began.

Node.jsの管理ツール、nvmをインストールする

これは、なにをしたくて書いたもの?

これまで、Node.jsのインストールにはずっとnodebrewを使っていました。

Node.jsを管理したくて、nodebrewをインストールする - CLOVER🍀

GitHub - hokaccha/nodebrew: Node.js version manager

開発が止まっていることはなんとなく知っていましたが、それでも特に困ることはなかったのですが。

そろそろ他のものに乗り換えておこうかな、と思いまして。

nodebrewを選んだ時にも見ていたのですが、今ならnvmなんでしょうね。

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

というわけで、今回はnvmをインストールしていきます。

nvm

nvmはNode.jsの管理ツールで、複数のNode.jsをインストールして、切り替えたりできます。

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

見ていると、LTSの考慮などもありそうですね。

対象プラットフォームは、UnixmacOSWindows WSLなどのPOSIX準拠のシェルが動作する環境です。

Windowsなら、こちらでしょうか?(nvmとは違うリポジトリ、ツールで、別物ではありますが)

GitHub - coreybutler/nvm-windows: A node.js version management utility for Windows. Ironically written in Go.

環境

今回の環境は、こちらです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:    20.04
Codename:   focal


$ uname -srvmpio
Linux 5.4.0-67-generic #75-Ubuntu SMP Fri Feb 19 18:03:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

$ bash --version
GNU bash, バージョン 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

インストール

インストール方法は、こちらに従います。

Installing and Updating

実行。

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

実行すると、.bashrcに追記したので、bashを開き直すように言われます。

=> Appending nvm source string to $HOME/.bashrc
=> Appending bash_completion source string to $HOME/.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

確かに追記されています。

$ tail -n 3 $HOME/.bashrc 
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

bashを開き直して、バージョン確認。

$ nvm --version
0.37.2

インストールできたようです。

ヘルプ。

$ nvm --help

Node Version Manager (v0.37.2)

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.

Usage:
  nvm --help                                  Show this message
    --no-colors                               Suppress colored output
  nvm --version                               Print out the installed version of nvm
  nvm install [<version>]                     Download and install a <version>. Uses .nvmrc if available and version is omitted.
   The following optional arguments, if provided, must appear directly after `nvm install`:
    -s                                        Skip binary download, install from source only.
    --reinstall-packages-from=<version>       When installing, reinstall packages installed in <node|iojs|node version number>
    --lts                                     When installing, only select from LTS (long-term support) versions
    --lts=<LTS name>                          When installing, only select from versions for a specific LTS line
    --skip-default-packages                   When installing, skip the default-packages file if it exists
    --latest-npm                              After installing, attempt to upgrade to the latest working npm on the given node version
    --no-progress                             Disable the progress bar on any downloads
    --alias=<name>                            After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)
    --default                                 After installing, set default alias to the version specified. (same as: nvm alias default <version>)
  nvm uninstall <version>                     Uninstall a version
  nvm uninstall --lts                         Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
  nvm uninstall --lts=<LTS name>              Uninstall using automatic alias for provided LTS line, if available.
  nvm use [<version>]                         Modify PATH to use <version>. Uses .nvmrc if available and version is omitted.
   The following optional arguments, if provided, must appear directly after `nvm use`:
    --silent                                  Silences stdout/stderr output
    --lts                                     Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=<LTS name>                          Uses automatic alias for provided LTS line, if available.
  nvm exec [<version>] [<command>]            Run <command> on <version>. Uses .nvmrc if available and version is omitted.
   The following optional arguments, if provided, must appear directly after `nvm exec`:
    --silent                                  Silences stdout/stderr output
    --lts                                     Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=<LTS name>                          Uses automatic alias for provided LTS line, if available.
  nvm run [<version>] [<args>]                Run `node` on <version> with <args> as arguments. Uses .nvmrc if available and version is omitted.
   The following optional arguments, if provided, must appear directly after `nvm run`:
    --silent                                  Silences stdout/stderr output
    --lts                                     Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=<LTS name>                          Uses automatic alias for provided LTS line, if available.
  nvm current                                 Display currently activated version of Node
  nvm ls [<version>]                          List installed versions, matching a given <version> if provided
    --no-colors                               Suppress colored output
    --no-alias                                Suppress `nvm alias` output
  nvm ls-remote [<version>]                   List remote versions available for install, matching a given <version> if provided
    --lts                                     When listing, only show LTS (long-term support) versions
    --lts=<LTS name>                          When listing, only show versions for a specific LTS line
    --no-colors                               Suppress colored output
  nvm version <version>                       Resolve the given description to a single local version
  nvm version-remote <version>                Resolve the given description to a single remote version
    --lts                                     When listing, only select from LTS (long-term support) versions
    --lts=<LTS name>                          When listing, only select from versions for a specific LTS line
  nvm deactivate                              Undo effects of `nvm` on current shell
    --silent                                  Silences stdout/stderr output
  nvm alias [<pattern>]                       Show all aliases beginning with <pattern>
    --no-colors                               Suppress colored output
  nvm alias <name> <version>                  Set an alias named <name> pointing to <version>
  nvm unalias <name>                          Deletes the alias named <name>
  nvm install-latest-npm                      Attempt to upgrade to the latest working `npm` on the current node version
  nvm reinstall-packages <version>            Reinstall global `npm` packages contained in <version> to current version
  nvm unload                                  Unload `nvm` from shell
  nvm which [current | <version>]             Display path to installed node version. Uses .nvmrc if available and version is omitted.
    --silent                                  Silences stdout/stderr output when a version is omitted
  nvm cache dir                               Display path to the cache directory for nvm
  nvm cache clear                             Empty cache directory for nvm
  nvm set-colors [<color codes>]              Set five text colors using format "yMeBg". Available when supported.
                                               Initial colors are:
                                                   g  b  y  r  e
                                               Color codes:
                                                 r/R = red / bold red
                                                 g/G = green / bold green
                                                 b/B = blue / bold blue
                                                 c/C = cyan / bold cyan
                                                 m/M = magenta / bold magenta
                                                 y/Y = yellow / bold yellow
                                                 k/K = black / bold black
                                                 e/W = light grey / white

Example:
  nvm install 8.0.0                     Install a specific version number
  nvm use 8.0                           Use the latest available 8.0.x release
  nvm run 6.10.3 app.js                 Run app.js using node 6.10.3
  nvm exec 4.8.3 node app.js            Run `node app.js` with the PATH pointing to node 4.8.3
  nvm alias default 8.1.0               Set default node version on a shell
  nvm alias default node                Always default to the latest available node version on a shell

  nvm install node                      Install the latest available version
  nvm use node                          Use the latest version
  nvm install --lts                     Install the latest LTS version
  nvm use --lts                         Use the latest LTS version

  nvm set-colors cgYmW                  Set text colors to cyan, green, bold yellow, magenta, and white

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

nvm自身のアップデートは、インストールと同じようですね。

Installing and Updating

とにかく最新版をインストールしたかったら、こんな感じでいいのではないでしょうか。

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/`curl -s https://api.github.com/repos/nvm-sh/nvm/releases/latest | jq -r .tag_name`/install.sh | bash

使い方

使い方については、こちら。

Usage

Node.jsをインストールする

nvm installで、最新のNode.jsをインストール。

$ nvm install node
$ node -v
v15.12.0


$ npm -v
7.6.3

--ltsで、最新のLTSをインストール。

$ nvm install --lts
$ node -v
v14.16.0


$ npm -v
6.14.11

バージョンを指定してのインストールもできます。

$ nvm install v13.14.0


$ nvm install --lts v12.21.0

LTSについては、名前でもインストールできそうですね。

Long-term Support

切り替え

バージョンの切り替えは、nvm useで。

$ nvm use v14.16.0
Now using node v14.16.0 (npm v6.14.11)
ls

現在インストールされているバージョンを見るには、nvm lsで。

$ nvm ls
->     v14.16.0
       v15.12.0
default -> node (-> v15.12.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v15.12.0) (default)
stable -> 15.12 (-> v15.12.0) (default)
lts/* -> lts/fermium (-> v14.16.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.0 (-> N/A)
lts/erbium -> v12.21.0 (-> N/A)
lts/fermium -> v14.16.0

インストールされていないバージョンも含めて見るには、nvm ls-remoteで。

$ nvm ls-remote

こちらは、大量に表示されますが。

指定のバージョンをアンインストール

nvm uninstallで。

$ nvm uninstall v13.14.0
bash completion

bash completionあるんですね。便利です。

Bash Completion

$ nvm 
alias               deactivate          install             list-remote         reinstall-packages  uninstall           version             
cache               exec                install-latest-npm  ls                  run                 unload              version-remote      
current             help                list                ls-remote           unalias             use                 which

バージョンの補完もできる時がありそうです。

その他

指定のバージョンに一時的に切り替えて実行するnvm runがあったり、

Usage

.nvmrcというファイルで、プロジェクトのNode.jsのバージョンを指定できたりもするようです。

.nvmrc

nvm自体のアンインストール

$NVM_DIRディレクトリ(デフォルトでは$HOME/.nvm)を削除して、.bashrcに追記された内容を削除すればおしまい。

Uninstalling / Removal

こんな感じでしょうか。雰囲気はわかったので、このまま使っていきましょう。