How to install Node. js on Ubuntu

Source: Internet
Author: User
Node. js is a set of JavaScript toolkit used to write high-performance network servers. A series of changes started from this. Specifically, Node. js assumes that you are running it in a POSIX environment (Linux or MacOSX ). If you are running Windows, install MinGW to obtain a POSIX-like environment. In Node, Http is the first priority. Node is optimized to create an http server, so most of the examples and libraries you see on the Internet are concentrated on the web (

Node. js is a set of JavaScript toolkit used to write high-performance network servers. A series of changes started from this. Specifically, Node. js assumes that you are running it in a POSIX environment (Linux or Mac OS X ). If you are running Windows, install MinGW to obtain a POSIX-like environment. In Node, Http is the first priority. Node is optimized to create an http server, so most examples and libraries you can see on the Internet are concentrated on the web (http framework, template library, etc ).

Step 1: Install the dependency package

1. Install python version 2.6 or later (Ubuntu has been installed by default. You can use the pyhton-v command in terminal to view the python version ).
2. install other dependency packages:
Sudo apt-get install g ++ curl libssl-dev apache2-utils
3. Install the git tool:
Sudo apt-get install git-core

Step 2: Obtain the source code

Git clone git: // github.com/joyent/node.git
Wait until the download is completed. The size is 20 + m.

Step 3: Specify the compilation version, which is important!

1. Go to the folder where the source code is downloaded: Cd node

2. Specify the migrated version: Git checkout v0.4.10

This is very important because the latest node version is 0.5.5-pre, but many commonly used packages only support 0.4.10 (such as express). If you use the latest node version, npm cannot download the corresponding package.

3. Specify the path for compilation and execution:
  1. Mkdir ~ /Local
  2. ./Configure-prefix = $ HOME/local/node
  3. Make
  4. Make install
  5. Echo 'export PATH = $ HOME/local/node/bin: $ path'> ~ /. Profile
  6. Echo 'export NODE_PATH = $ HOME/local/node: $ HOME/local/node/lib/node_modules '> ~ /. Profile
  7. Source ~ /. Profile
Step 4: Set the environment variable. If you want to continue using the node command after restart, you need to set the environment variable:
Run sudo gedit/etc/profile to open the configuration file. Add the following two lines at the end of the file:
Export PATH = "$ HOME/local/node/bin: $ PATH"
Export NODE_PATH = "$ HOME/local/node: $ HOME/local/node/lib/node_modules"

Save and restart the system to make the settings take effect.

Step 5: Install npm Curl http://npmjs.org/install.sh | sh

Install the corresponding package as needed, such as express:
Npm install express

If the command does not respond for a long time after it is entered, you can view the detailed execution information by adding the-verbose parameter, that is:
Npm install express-verbose

Generally, downloading fails for two reasons:
1. The network speed is too slow and exits upon timeout.
2. the node version is too new, and the downloaded package is not supported. (The solution is described in step 3 .)


Step 6: test the HelloWorld program to save the following code as a hello. js file.
  1. VarHttp = require ('Http');
  2. Http. createServer (Function(Req, res ){
  3. Res. writeHead (200 ,{'Content-type':'Text/plain'});
  4. Res. end ('Hello Node. jsn');
  5. }). Listen (8124,"127.0.0.1");
  6. Console. log ('Server running at http: // 127.0.0.1: 8124 /');
Then run the following command: Node hello. jsStart the service.
Open your browser and enter the address: http: // 127.0.0.1: 8124/
If Hello Node. jsn is returned, the installation is successful!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.