在Ubuntu下安装Node.JS的不同方式

Kashif Siddique 的头像

·

·

·

36,845 次阅读

如果你要在Ubuntu 15.04上安装Node.js的话,这篇教程对你来说肯定很重要。Node.js从本质上来说就是一个运行在服务端上的封装好了输入输出流的javascript程序。Node.js巧妙的使用单线程的事件循环来处理高吞吐量和非阻塞IO。同时它也是一个提供了通过操作系统读写文件和网络操作功能的平台层。所以这篇文章将展示在Ubuntu 15.04 server上不同的安装Node.Js的方式。

安装Node.JS 的方法

有许多安装Node.JS的不同的方法,我们可以选择其一。通过本篇文章我们将手把手带着你在Ubuntu 15.04上安装Node.Js,在此之前请卸载旧版本的包以免发生包冲突。

  • 从源代码安装Node.JS
  • 用包管理器安装Node.JS
  • 从Github远程库安装Node.JS
  • 用NVM安装Node.JS

1) 从源代码安装Node.JS

让我们开始从源代码安装Node.JS之前,请确认系统上的所有的依赖包都已经更新到最新版本。然后跟着以下步骤来开始安装:

步骤1: 升级系统

用以下命令来升级系统,并且安装一些Node.JS必要的包。

root@ubuntu-15:~# apt-get update
root@ubuntu-15:~# apt-get install python gcc make g++

步骤2: 获取Node.JS的源代码

安装好依赖包之后我们可以从官方网站上下载Node.JS的源代码。下载以及解压的命令如下:

root@ubuntu-15:~# wget http://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz
root@ubuntu-15:~# tar zxvf node-v0.12.4.tar.gz

步骤3: 开始安装

现在我们进入源代码的目录,然后运行.configure文件

NodeJS Configure
root@ubuntu-15:~# ls
node-v0.12.4 node-v0.12.4.tar.gz
root@ubuntu-15:~# cd node-v0.12.4/
root@ubuntu-15:~/node-v0.12.4# ./configure
root@ubuntu-15:~/node-v0.12.4# make install

安装后测试

只要运行一下上面的命令就顺利安装好了Node.JS,现在我们来确认一下版本信息和测试以下Node.JS是否可以运行输出。

root@ubuntu-15:~/node-v0.12.4# node -v
v0.12.4
Node.Js Test

创建一个以.js为扩展名的文件然后用Node的命令运行

root@ubuntu-15:~/node-v0.12.4# touch helo_test.js
root@ubuntu-15:~/node-v0.12.4# vim helo_test.js
console.log('Hello World');

现在我们用Node的命令运行文件

root@ubuntu-15:~/node-v0.12.4# node helo_test.js
Hello World

输出的结果证明我们已经成功的在Ubuntu 15.04安装好了Node.JS,同时我们也能运行JavaScript文件。

2) 利用包管理器安装Node.JS

在Ubuntu下用包管理器安装Node.JS是非常简单的,只要增加NodeSource的个人软件包档案(PPA)即可。

我们将下面通过PPA安装Node.JS。

步骤1: 用curl获取源代码

在我们用curl获取源代码之前,我们必须先升级操作系统,然后用curl命令获取NodeSource添加到本地仓库。

root@ubuntu-15:~#apt-get update
root@ubuntu-15:~# curl -sL https://deb.nodesource.com/setup | sudo bash -

curl将运行以下任务

## Installing the NodeSource Node.js 0.10 repo...
## Populating apt-get cache...
## Confirming "vivid" is supported...
## Adding the NodeSource signing key to your keyring...
## Creating apt sources list file for the NodeSource Node.js 0.10 repo...
## Running `apt-get update` for you...
Fetched 6,411 B in 5s (1,077 B/s)
Reading package lists... Done
## Run `apt-get install nodejs` (as root) to install Node.js 0.10 and npm

步骤2: 安装NodeJS和NPM

运行以上命令之后如果输出如上所示,我们可以用apt-get命令来安装NodeJS和NPM包。

root@ubuntu-15:~# apt-get install nodejs
NodeJS Install

步骤3: 安装一些必备的工具

通过以下命令来安装编译安装一些我们必需的本地插件。

root@ubuntu-15:~# apt-get install -y build-essential

通过Node.JS Shell来测试

测试Node.JS的步骤与之前使用源代码安装相似,通过以下node命令来确认Node.JS是否完全安装好:

root@ubuntu-15:~# node
> console.log('Node.js Installed Using Package Manager');
Node.js Installed Using Package Manager

via: http://linoxide.com/ubuntu-how-to/setup-node-js-ubuntu-15-04-different-methods/

作者:Kashif Siddique 译者:NearTan 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注