nvm是nodejs官方用于管理nodejs多版本环境的一个工具 ,今天,我带领大家基于nvm完成nodejs在Linux mint 22.1上的安装。
考虑到Linux mint 22.1是基于ubuntu 24.04.1 LTS的,所以,这里的安装也完全适用于nodejs在nodejs上的安装。
不过,我们看到,nodejs官方给出的说明,主流Linux发行版都是可以基于nvm来安装的,这一点非常的好,我们再也不用针对不同Linux发行版采用不同的安装方式了。
Install nvm
lwk@qwfys:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16563 100 16563 0 0 12373 0 0:00:01 0:00:01 --:--:-- 12378
=> Downloading nvm from git to '/home/lwk/.nvm'
=> Cloning into '/home/lwk/.nvm'...
remote: Enumerating objects: 381, done.
remote: Counting objects: 100% (381/381), done.
remote: Compressing objects: 100% (324/324), done.
remote: Total 381 (delta 43), reused 175 (delta 29), pack-reused 0 (from 0)
Receiving objects: 100% (381/381), 383.82 KiB | 662.00 KiB/s, done.
Resolving deltas: 100% (43/43), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/lwk/.bashrc
=> Appending bash_completion source string to /home/lwk/.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
lwk@qwfys:~$
这个时候,nvm已经安装成功,不过重新打一个命令行终端。
Install nodejs
这里,我们重新开启一个命令行终端,尝试下一nvm命令可否正常使用。
lwk@qwfys:~$ nvm
Node Version Manager (v0.40.1)
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>)
--save After installing, write the specified version to .nvmrc
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.
--save Writes the specified version to .nvmrc.
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`)
lwk@qwfys:~$
我们看到,nvm已经可以正常使用,拉下来,我们借助nvm install
命令完成尝试安装一下nodejs 18。
lwk@qwfys:~$ nvm install 18
Downloading and installing node v18.20.7...
Downloading https://nodejs.org/dist/v18.20.7/node-v18.20.7-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.20.7 (npm v10.8.2)
Creating default alias: default -> 18 (-> v18.20.7)
lwk@qwfys:~$
这里,我们看到,nodejs 18已经安装成功。
View
这里,我们查看一下nodejs命令行可否正常使用。
lwk@qwfys:~$ nodejs
Command 'nodejs' not found, but can be installed with:
sudo apt install nodejs
lwk@qwfys:~$ node
Welcome to Node.js v18.20.7.
Type ".help" for more information.
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the REPL
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL
> .exit
lwk@qwfys:~$
我们看到,使用node命令即可。