nvm安裝多版本NodeJS和切換

1 NVM簡介

  1. nvm是什么
    nvmnodejs 的版本管理器,旨在按用戶要求進行安裝并按進行shell調用。nvm適用于任何符合POSIXshell(sh、dash、ksh厦坛、zsh数焊、bash)表制,特別是在以下平臺上:
  • unix
  • macOS
  • windows WSL

2 NVM安裝

可以通過如下的cURL或者wget命令之一進行安裝:

2.1 install腳本安裝

  • cURL
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
  • wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

安裝之后濒生,為了是nvm命令能夠被識別和生效埋泵,可以重啟terminal或者執(zhí)行屏幕提示的腳本:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

2.2 git倉庫安裝

由于國內對于https://raw.githubusercontent.comhttps://github.com經(jīng)常無法訪問,或者訪問速度很慢罪治。這里做了一個github nvm倉庫gitee nvm鏡像丽声。

  • git倉庫安裝nvm的方法
  1. 首先確保已經(jīng)安裝了git v1.7.10+
  2. 安裝nvm(2023-09-26最新版本是v0.39.5)
cd ~/
git clone https://github.com/nvm-sh/nvm.git .nvm
cd ~/.nvm
git checkout -b v0395 v0.39.5
. ./nvm.sh
  1. 配置環(huán)境變量
    將以下代碼添加到~/.bashrc觉义、~/.profile或者~/.zshrc文件中雁社,從而在登錄系統(tǒng)之后,自動加載nvm配置晒骇。
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

2.3 查看可用的nvm命令:

$ nvm -h

Node Version Manager (v0.39.5)

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.
    -b                                        Skip source download, install from binary 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:
                                                  bygre
                                               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`)

3 使用nvm安裝多個版本的NodeJS

  1. 查看可用的nodejs版本
# 查看所有可用的nodejs版本
$ nvm ls-remote
# 查看可用的LTS版本
$ nvm ls-remote | grep LTS 
        v4.2.0   (LTS: Argon)
        v4.2.1   (LTS: Argon)
        v4.2.2   (LTS: Argon)
        v4.2.3   (LTS: Argon)
        v4.2.4   (LTS: Argon)
        v4.2.5   (LTS: Argon)
        v4.2.6   (LTS: Argon)
        v4.3.0   (LTS: Argon)
        v4.3.1   (LTS: Argon)
        v4.3.2   (LTS: Argon)
        v4.4.0   (LTS: Argon)
        v4.4.1   (LTS: Argon)
        v4.4.2   (LTS: Argon)
        v4.4.3   (LTS: Argon)
        v4.4.4   (LTS: Argon)
        v4.4.5   (LTS: Argon)
        v4.4.6   (LTS: Argon)
        v4.4.7   (LTS: Argon)
        v4.5.0   (LTS: Argon)
        v4.6.0   (LTS: Argon)
        v4.6.1   (LTS: Argon)
        v4.6.2   (LTS: Argon)
        v4.7.0   (LTS: Argon)
        v4.7.1   (LTS: Argon)
        v4.7.2   (LTS: Argon)
        v4.7.3   (LTS: Argon)
        v4.8.0   (LTS: Argon)
        v4.8.1   (LTS: Argon)
        v4.8.2   (LTS: Argon)
        v4.8.3   (LTS: Argon)
        v4.8.4   (LTS: Argon)
        v4.8.5   (LTS: Argon)
        v4.8.6   (LTS: Argon)
        v4.8.7   (LTS: Argon)
        v4.9.0   (LTS: Argon)
        v4.9.1   (Latest LTS: Argon)
        v6.9.0   (LTS: Boron)
        v6.9.1   (LTS: Boron)
        v6.9.2   (LTS: Boron)
        v6.9.3   (LTS: Boron)
        v6.9.4   (LTS: Boron)
        v6.9.5   (LTS: Boron)
       v6.10.0   (LTS: Boron)
       v6.10.1   (LTS: Boron)
       v6.10.2   (LTS: Boron)
       v6.10.3   (LTS: Boron)
       v6.11.0   (LTS: Boron)
       v6.11.1   (LTS: Boron)
       v6.11.2   (LTS: Boron)
       v6.11.3   (LTS: Boron)
       v6.11.4   (LTS: Boron)
       v6.11.5   (LTS: Boron)
       v6.12.0   (LTS: Boron)
       v6.12.1   (LTS: Boron)
       v6.12.2   (LTS: Boron)
       v6.12.3   (LTS: Boron)
       v6.13.0   (LTS: Boron)
       v6.13.1   (LTS: Boron)
       v6.14.0   (LTS: Boron)
       v6.14.1   (LTS: Boron)
       v6.14.2   (LTS: Boron)
       v6.14.3   (LTS: Boron)
       v6.14.4   (LTS: Boron)
       v6.15.0   (LTS: Boron)
       v6.15.1   (LTS: Boron)
       v6.16.0   (LTS: Boron)
       v6.17.0   (LTS: Boron)
       v6.17.1   (Latest LTS: Boron)
        v8.9.0   (LTS: Carbon)
        v8.9.1   (LTS: Carbon)
        v8.9.2   (LTS: Carbon)
        v8.9.3   (LTS: Carbon)
        v8.9.4   (LTS: Carbon)
       v8.10.0   (LTS: Carbon)
       v8.11.0   (LTS: Carbon)
       v8.11.1   (LTS: Carbon)
       v8.11.2   (LTS: Carbon)
       v8.11.3   (LTS: Carbon)
       v8.11.4   (LTS: Carbon)
       v8.12.0   (LTS: Carbon)
       v8.13.0   (LTS: Carbon)
       v8.14.0   (LTS: Carbon)
       v8.14.1   (LTS: Carbon)
       v8.15.0   (LTS: Carbon)
       v8.15.1   (LTS: Carbon)
       v8.16.0   (LTS: Carbon)
       v8.16.1   (LTS: Carbon)
       v8.16.2   (LTS: Carbon)
       v8.17.0   (Latest LTS: Carbon)
      v10.13.0   (LTS: Dubnium)
      v10.14.0   (LTS: Dubnium)
      v10.14.1   (LTS: Dubnium)
      v10.14.2   (LTS: Dubnium)
      v10.15.0   (LTS: Dubnium)
      v10.15.1   (LTS: Dubnium)
      v10.15.2   (LTS: Dubnium)
      v10.15.3   (LTS: Dubnium)
      v10.16.0   (LTS: Dubnium)
      v10.16.1   (LTS: Dubnium)
      v10.16.2   (LTS: Dubnium)
      v10.16.3   (LTS: Dubnium)
      v10.17.0   (LTS: Dubnium)
      v10.18.0   (LTS: Dubnium)
      v10.18.1   (LTS: Dubnium)
      v10.19.0   (LTS: Dubnium)
      v10.20.0   (LTS: Dubnium)
      v10.20.1   (LTS: Dubnium)
      v10.21.0   (LTS: Dubnium)
      v10.22.0   (LTS: Dubnium)
      v10.22.1   (LTS: Dubnium)
      v10.23.0   (LTS: Dubnium)
      v10.23.1   (LTS: Dubnium)
      v10.23.2   (LTS: Dubnium)
      v10.23.3   (LTS: Dubnium)
      v10.24.0   (LTS: Dubnium)
->     v10.24.1   (Latest LTS: Dubnium)
      v12.13.0   (LTS: Erbium)
      v12.13.1   (LTS: Erbium)
      v12.14.0   (LTS: Erbium)
      v12.14.1   (LTS: Erbium)
      v12.15.0   (LTS: Erbium)
      v12.16.0   (LTS: Erbium)
      v12.16.1   (LTS: Erbium)
      v12.16.2   (LTS: Erbium)
      v12.16.3   (LTS: Erbium)
      v12.17.0   (LTS: Erbium)
      v12.18.0   (LTS: Erbium)
      v12.18.1   (LTS: Erbium)
      v12.18.2   (LTS: Erbium)
      v12.18.3   (LTS: Erbium)
      v12.18.4   (LTS: Erbium)
      v12.19.0   (LTS: Erbium)
      v12.19.1   (LTS: Erbium)
      v12.20.0   (LTS: Erbium)
      v12.20.1   (LTS: Erbium)
      v12.20.2   (LTS: Erbium)
      v12.21.0   (LTS: Erbium)
      v12.22.0   (LTS: Erbium)
      v12.22.1   (LTS: Erbium)
      v12.22.2   (LTS: Erbium)
      v12.22.3   (LTS: Erbium)
      v12.22.4   (LTS: Erbium)
      v12.22.5   (LTS: Erbium)
      v12.22.6   (LTS: Erbium)
      v12.22.7   (LTS: Erbium)
      v12.22.8   (LTS: Erbium)
      v12.22.9   (LTS: Erbium)
     v12.22.10   (LTS: Erbium)
     v12.22.11   (LTS: Erbium)
     v12.22.12   (Latest LTS: Erbium)
      v14.15.0   (LTS: Fermium)
      v14.15.1   (LTS: Fermium)
      v14.15.2   (LTS: Fermium)
      v14.15.3   (LTS: Fermium)
      v14.15.4   (LTS: Fermium)
      v14.15.5   (LTS: Fermium)
      v14.16.0   (LTS: Fermium)
      v14.16.1   (LTS: Fermium)
      v14.17.0   (LTS: Fermium)
      v14.17.1   (LTS: Fermium)
      v14.17.2   (LTS: Fermium)
      v14.17.3   (LTS: Fermium)
      v14.17.4   (LTS: Fermium)
      v14.17.5   (LTS: Fermium)
      v14.17.6   (LTS: Fermium)
      v14.18.0   (LTS: Fermium)
      v14.18.1   (LTS: Fermium)
      v14.18.2   (LTS: Fermium)
      v14.18.3   (LTS: Fermium)
      v14.19.0   (LTS: Fermium)
      v14.19.1   (LTS: Fermium)
      v14.19.2   (LTS: Fermium)
      v14.19.3   (LTS: Fermium)
      v14.20.0   (LTS: Fermium)
      v14.20.1   (LTS: Fermium)
      v14.21.0   (LTS: Fermium)
      v14.21.1   (LTS: Fermium)
      v14.21.2   (LTS: Fermium)
      v14.21.3   (Latest LTS: Fermium)
      v16.13.0   (LTS: Gallium)
      v16.13.1   (LTS: Gallium)
      v16.13.2   (LTS: Gallium)
      v16.14.0   (LTS: Gallium)
      v16.14.1   (LTS: Gallium)
      v16.14.2   (LTS: Gallium)
      v16.15.0   (LTS: Gallium)
      v16.15.1   (LTS: Gallium)
      v16.16.0   (LTS: Gallium)
      v16.17.0   (LTS: Gallium)
      v16.17.1   (LTS: Gallium)
      v16.18.0   (LTS: Gallium)
      v16.18.1   (LTS: Gallium)
      v16.19.0   (LTS: Gallium)
      v16.19.1   (LTS: Gallium)
      v16.20.0   (LTS: Gallium)
      v16.20.1   (LTS: Gallium)
      v16.20.2   (Latest LTS: Gallium)
      v18.12.0   (LTS: Hydrogen)
      v18.12.1   (LTS: Hydrogen)
      v18.13.0   (LTS: Hydrogen)
      v18.14.0   (LTS: Hydrogen)
      v18.14.1   (LTS: Hydrogen)
      v18.14.2   (LTS: Hydrogen)
      v18.15.0   (LTS: Hydrogen)
      v18.16.0   (LTS: Hydrogen)
      v18.16.1   (LTS: Hydrogen)
      v18.17.0   (LTS: Hydrogen)
      v18.17.1   (LTS: Hydrogen)
      v18.18.0   (Latest LTS: Hydrogen)
  1. 安裝需要的nodejs版本
    安裝示例霉撵,安裝版本:v10.24.1 (Latest LTS: Dubnium),v12.22.5 (Latest LTS: Erbium),v14.17.5 (Latest LTS: Fermium)
$ nvm install v10.24.1
Downloading and installing node v10.24.1...
Downloading https://nodejs.org/dist/v10.24.1/node-v10.24.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.24.1 (npm v6.14.12)
Creating default alias: default -> v10.24.1

$ nvm install v12.22.5
Downloading and installing node v12.22.5...
Downloading https://nodejs.org/dist/v12.22.5/node-v12.22.5-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.5 (npm v6.14.14)

$ nvm install v14.17.5
Downloading and installing node v14.17.5...
Downloading https://nodejs.org/dist/v14.17.5/node-v14.17.5-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.17.5 (npm v6.14.14)

檢查nodejs是否安裝成功:

$ node -v
v14.17.5

4 切換nodejs的生效版本

$ node -v
v14.17.5
$ nvm use v10.24.1 
Now using node v10.24.1 (npm v6.14.12)
$ node -v
v10.24.1
$ nvm use v12.22.5 
Now using node v12.22.5 (npm v6.14.14)
$ node -v
v12.22.5

5 其他操作系統(tǒng)安裝nvm和常見問題解決

Troubleshooting On Linux, On Gitee
Troubleshooting on macOS, on Gitee

5 參考

  1. Troubleshooting On Linux, On Gitee
  2. Troubleshooting On Linux, On Github
  3. NodeJS Version Manager, NVM on Gitee
  4. NodeJS Version Manager, NVM on Github
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末洪囤,一起剝皮案震驚了整個濱河市徒坡,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌瘤缩,老刑警劉巖喇完,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異剥啤,居然都是意外死亡锦溪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門府怯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刻诊,“玉大人,你說我怎么就攤上這事牺丙』捣辏” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長是整。 經(jīng)常有香客問我肖揣,道長,這世上最難降的妖魔是什么浮入? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任龙优,我火速辦了婚禮,結果婚禮上事秀,老公的妹妹穿的比我還像新娘彤断。我一直安慰自己,他們只是感情好易迹,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布宰衙。 她就那樣靜靜地躺著,像睡著了一般睹欲。 火紅的嫁衣襯著肌膚如雪供炼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天窘疮,我揣著相機與錄音袋哼,去河邊找鬼。 笑死闸衫,一個胖子當著我的面吹牛涛贯,可吹牛的內容都是我干的。 我是一名探鬼主播蔚出,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼弟翘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了骄酗?” 一聲冷哼從身側響起衅胀,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎酥筝,沒想到半個月后滚躯,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡嘿歌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年掸掏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宙帝。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡丧凤,死狀恐怖,靈堂內的尸體忽然破棺而出步脓,到底是詐尸還是另有隱情愿待,我是刑警寧澤浩螺,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站仍侥,受9級特大地震影響要出,放射性物質發(fā)生泄漏。R本人自食惡果不足惜农渊,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一患蹂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧砸紊,春花似錦传于、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至游添,卻和暖如春系草,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背否淤。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棠隐,地道東北人石抡。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像助泽,于是被迫代替她去往敵國和親啰扛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內容