How to install Node. js on Ubuntu

Source: Internet
Author: User
Tags install node
Finally, Node. js has been installed in Ubuntu, and all kinds of ups and downs are recorded here for the benefit of future users. Step 1: Install the dependency Package 1. Install python 2.6 or later (ubuntu is installed by default. You can use the pyhton-v command in terminal to view the python version ). 2. install other dependent packages: sudoapt-getinstallg ++ curllibssl-devapache2-utils3.

Finally, Node. js has been installed in Ubuntu, and all kinds of ups and downs are recorded here for the benefit of future users.

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:
Mkdir ~ /Local
./Configure-prefix = $ HOME/local/node
Make
Make install
Echo 'export PATH = $ HOME/local/node/bin: $ path'> ~ /. Profile
Echo 'export NODE_PATH = $ HOME/local/node: $ HOME/local/node/lib/node_modules '> ~ /. Profile
Source ~ /. Profile

Step 4: Set Environment Variables

If you want to continue using the node command after the 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

Save the following code as a hello. js file.

Varhttp = require ('http ');

Http. createServer (function (req, res ){

Res. writeHead (200, {'content-type': 'text/plain '});

Res. end ('Hello Node. jsn ');

}). Listen (8124, "127.0.0.1 ");

Console. log ('server running at http: // 127.0.0.1: 8124 /');
 
 

Then run the following command:
Node hello. js
Start 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.