在nvm环境下安装nodejs、npm和yarn

安装nvm

1
# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

or

1
# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

.bashrc中已经自动添加,在不退出当前终端的情况下用下面的命令设置环境变量:

1
2
# export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

安装nodejs和npm

1
# nvm ls-remote --lts

从列出的远程lts版本中选择最新的一个。

1
2
3
# nvm install 8.11.3
# nvm alias default 8.11.3
# nvm use default

升级npm

1
# npm i -g npm

安装Yarn

1
2
3
# curl -o- -L https://yarnpkg.com/install.sh | bash
# export PATH="$HOME/.yarn/bin:$PATH"
# yarn config set prefix ~/.yarn

$HOME/.yarn/bin路径添加到.bashrc或者.bash_profilePATH环境变量中。

清理cache

1
# nvm cache clear

Done.