在Linux安装nodejs

2020-05-22  本文已影响0人  阿拉斌

推荐安装方式

说明

对应linux下node的安装,推荐使用nvm进行管理

nvm (Node Version Manager) 是Nodejs版本管理器,可对不同的node版本快速进行切换。

nvm

查看Readme

To install or update nvm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash   (使用这个)

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

会出现无法访问的情况,这样的话,就需要设置下host了。。

修改hosts
CentOS及macOS直接在终端输入

sudo vi /etc/hosts

199.232.4.133 raw.githubusercontent.com

win10

C:\Windows\System32\drivers\etc

继续安装

命令:

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

记录:

root@DESKTOP-AVEHH5N:~# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0   1617      0  0:00:08  0:00:08 --:--:--  3197
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 290, done.
remote: Counting objects: 100% (290/290), done.
remote: Compressing objects: 100% (257/257), done.
remote: Total 290 (delta 35), reused 97 (delta 20), pack-reused 0
Receiving objects: 100% (290/290), 163.27 KiB | 7.00 KiB/s, done.
Resolving deltas: 100% (35/35), done.
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.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
root@DESKTOP-AVEHH5N:~# export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
root@DESKTOP-AVEHH5N:~# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
root@DESKTOP-AVEHH5N:~# 
root@DESKTOP-AVEHH5N:~# nvm --version
0.35.3
root@DESKTOP-AVEHH5N:~# 

之后就可以继续操作了。

使用nvm

1、查看本地安装的所有版本;有可选参数available,显示所有可下载的版本。

nvm list [available]

2、安装,命令中的版本号可自定义,具体参考命令1查询出来的列表

nvm install 11.13.0

3、使用特定版本

nvm use 11.13.0

4、卸载

nvm uninstall 11.13.0

记录:

root@DESKTOP-AVEHH5N:~# nvm install 12.16.3
Downloading and installing node v12.16.3...
Downloading https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.xz...
############################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.16.3 (npm v6.14.4)
Creating default alias: default -> 12.16.3 (-> v12.16.3)
root@DESKTOP-AVEHH5N:~# node -v
v12.16.3
root@DESKTOP-AVEHH5N:~# 

其他安装方式

去官网下载nodejs

nodejs.cn

二进制包下载地址:https://npm.taobao.org/mirrors/node/v14.2.0/node-v14.2.0-linux-x64.tar.xz

下载

我们在package目录下进行操作

drwxr-xr-x 1 root root 4096 May 16 14:24 ./
drwxr-xr-x 1 root root 4096 Apr  2 21:52 ../
drwxr-xr-x 1 root root 4096 May 16 14:24 package/
drwxrwxrwx 1 root root 4096 Apr  2 21:57 project/
root@DESKTOP-AVEHH5N:/opt# cd package/
root@DESKTOP-AVEHH5N:/opt/package# 
wget https://nodejs.org/dist/v12.16.3/node-v12.16.3-darwin-x64.tar.gz

如果在内网,可以使用内网地址:

解压并修改名称

root@DESKTOP-AVEHH5N:/opt/package# tar -xvf node-v12.16.3-darwin-x64.tar.gz
root@DESKTOP-AVEHH5N:/opt/package# mv node-v12.16.3-darwin-x64/ nodejs

备份配置文件

root@DESKTOP-AVEHH5N:/opt/package# cp /etc/profile /etc/profile.bak

然后vim /etc/profile,在最下面添加 export PATH=$PATH: 后面跟上node下bin目录的路径

export PATH=$PATH:/opt/package/nodejs/bin

立即生效

source /etc/profile
上一篇下一篇

猜你喜欢

热点阅读