在Ubuntu上安装Node.js有多种方法,以下是两种常用的方法:
更新APT包列表:
sudo apt update
安装Node.js和npm:
sudo apt install nodejs npm
验证安装:
node -v
npm -v
NodeSource提供了一个方便的方式来安装特定版本的Node.js。以下是安装最新稳定版Node.js的步骤:
添加NodeSource库:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
这里的16.x
是你想要安装的Node.js版本,你可以根据需要更改版本号。
安装Node.js和npm:
sudo apt-get install -y nodejs
验证安装:
node -v
npm -v
nvm是一个用于管理多个Node.js版本的工具。以下是安装和使用nvm的步骤:
安装nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
或者使用wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
加载nvm:
安装完成后,你需要重新加载你的shell配置文件(例如.bashrc
或.zshrc
):
source ~/.bashrc
或者重新打开一个新的终端窗口。
安装Node.js:
nvm install node # 安装最新稳定版
或者安装特定版本:
nvm install 16.14.0
使用Node.js:
node -v
npm -v
切换Node.js版本(如果需要):
nvm use 16.14.0
选择适合你的方法进行安装即可。如果你需要管理多个Node.js版本,推荐使用nvm。