如何在 Ubuntu 和 CentOS 上安装 Node.js
Node.js 的流行度正以惊人的速度增长。如果您最近开始涉足 Node.js 开发,那么安装 Node.js 是您的首要任务。
虽然安装方法多种多样,但遵循一个简单且正确的流程可以大大简化您的操作。
以下是在 DigitalOcean 服务器上安装 Node.js 的详细步骤。让我们开始吧!
在 Ubuntu 16.x 或 18.x 上安装
默认存储库中可能无法找到最新版本的 Node.js。不用担心,您可以使用 NodeSource 发行版进行安装。
- 首先,以 root 用户身份登录您的服务器。
- 然后,根据您想要安装的 Node.js 版本,执行以下命令:
安装 Node.js 11.x
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
安装 Node.js 12.x
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
安装 Node.js 14.x
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
上述命令将下载并安装 NodeSource 的 Node.js 存储库。在命令执行完成后,您应该看到类似以下的输出:
Reading package lists... Done ## Run `sudo apt-get install -y nodejs` to install Node.js 11.x and npm ## You may also need development tools to build native addons: sudo apt-get install gcc g++ make ## To install the Yarn package manager, run: curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update && sudo apt-get install yarn
- 接下来,使用以下命令安装 Node.js:
apt-get install -y nodejs
这个过程可能需要几秒钟。安装完成后,您可以通过以下命令验证 Node.js 版本:
[email protected]:~# nodejs -v v11.7.0 [email protected]:~#
如您所见,已成功安装了 11.7.0 版本。
在 CentOS/RHEL 7.x 或 8.x 上安装
首先,您需要使用以下命令安装 NodeSource 存储库:
安装 Node.js 11.x
curl -sL https://rpm.nodesource.com/setup_11.x | bash -
安装 Node.js 12.x
curl -sL https://rpm.nodesource.com/setup_12.x | bash -
安装 Node.js 14.x
curl -sL https://rpm.nodesource.com/setup_14.x | bash -
接下来,使用以下命令安装 Node.js:
yum install -y nodejs
如果您使用的是 CentOS 8.x,也可以尝试使用 DNF:
dnf install -y nodejs
这个过程也需要几秒钟。安装完成后,您可能会看到类似以下的输出:
Running transaction Preparing : 1/1 Installing : python3-setuptools-39.2.0-5.el8.noarch 1/4 Installing : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4 Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4 Installing : python3-pip-9.0.3-16.el8.noarch 3/4 Running scriptlet: nodejs-2:14.9.0-1nodesource.x86_64 4/4 Installing : nodejs-2:14.9.0-1nodesource.x86_64 4/4 Running scriptlet: nodejs-2:14.9.0-1nodesource.x86_64 4/4 Verifying : python3-pip-9.0.3-16.el8.noarch 1/4 Verifying : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4 Verifying : python3-setuptools-39.2.0-5.el8.noarch 3/4 Verifying : nodejs-2:14.9.0-1nodesource.x86_64 4/4 Installed: nodejs-2:14.9.0-1nodesource.x86_64 python3-pip-9.0.3-16.el8.noarch python3-setuptools-39.2.0-5.el8.noarch python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 Complete! [[email protected] ~]#
这意味着 Node.js 已经安装成功,您可以使用 -v
语法来验证其版本:
[[email protected] ~]# node -v v11.7.0 [[email protected] ~]#
上述步骤也适用于 Fedora 29 或更高版本。
从源代码安装
如果您的服务器处于无法连接互联网的 DMZ 环境中,您仍然可以通过构建源代码的方式安装 Node.js。虽然这个过程没有二进制分发那么简单,但是完全可行。
- 首先,登录您的 Ubuntu 或 CentOS 服务器。
- 然后,使用 wget 下载最新或您需要的 Node.js 版本,您可以从 这里 下载。这里,我以最新版本为例:
wget https://nodejs.org/dist/v11.7.0/node-v11.7.0.tar.gz
tar -xvf node-v11.7.0.tar.gz
- 您会发现当前工作目录下创建了一个新的文件夹:
drwxr-xr-x 9 502 501 4096 Jan 17 21:27 node-v11.7.0
- 进入新创建的文件夹:
cd node-v11.7.0/
现在,您可以开始从源代码构建 Node.js 了。
但在继续之前,请确保您已经安装了所有必要的先决条件:
对于 Ubuntu,请安装以下依赖项:
apt-get update apt-get install gcc g++ clang make
对于 CentOS,请执行以下命令:
yum update yum install gcc clang gcc-c++
./configure
- 确保没有任何错误,然后继续执行以下操作:
make make install
构建和安装过程需要一些时间。完成后,您可以通过执行以下命令来验证安装的版本:
[email protected]:~# node --version v11.7.0 [email protected]:~#
正如您所看到的,安装 Node.js 非常简单。
接下来,您可以探索 Node.js 框架,以便成为一名 专业程序员。