在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版本。你可以根据需要更改版本号,例如14.x
或18.x
。
安装Node.js和npm:
sudo apt 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配置文件。如果你使用的是bash,可以运行:
source ~/.bashrc
如果你使用的是zsh,可以运行:
source ~/.zshrc
安装Node.js:
nvm install node
这将安装最新版本的Node.js。如果你想安装特定版本,可以使用:
nvm install <version>
例如:
nvm install 16.13.0
使用Node.js:
nvm use node
或者使用特定版本:
nvm use 16.13.0
验证安装:
node -v
npm -v
通过以上方法,你可以在Ubuntu上成功安装Node.js。选择适合你的方法进行安装即可。