Set up a ubuntu 14.04.02 LTS Node.js server

There are tons of essays or tutorials available that introduce how to set up a ubuntu Node.js server. For me, the workable way is:

to Install through NVM which stands for “Node.js version manager”

Here is the detail from:

To start off, we’ll need to get the software packages from our Ubuntu repositories that will allow us to build source packages. The nvm script will leverage these tools to build the necessary components:

sudo apt-get update
sudo apt-get install build-essential libssl-dev

Once the prerequisite packages are installed, you can pull down the nvm installation script from the project’s GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:

curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh

This will download the script and run it. It will install the software into a subdirectory of your home directory at

~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.

Here, for me, I use /home/federer/.profile instead of ~/.profile since my home file path is  /home/federer. Both should be OK.

To gain access to the nvm functionality, you’ll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:

source ~/.profile

Now that you have nvm installed, you can install isolated Node.js versions.

To find out the versions of Node.js that are available for installation, you can type:

nvm ls-remote
v0.11.12
v0.11.13
...
V0.12.7

As you can see, the newest version at the time of this writing is v0.11.13. You can install that by typing:

nvm install 0.11.13

For me, I install v0.12.6

Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:

nvm use 0.11.13

For me, I use v0.12.6

When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:

node -v
 
v.0.12.6

If you have multiple Node.js versions, you can see what is installed by typing:

nvm ls

If you wish to default one of the versions, you can type:

nvm alias default 0.11.13

This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:

nvm use default

Each version of Node.js will keep track of its own packages and has npm available to manage these.

You can have npm install packages to the Node.js project’s ./node_modules directory by using the normal format:

npm install express

If you’d like to install it globally (available to the other projects using the same Node.js version), you can add the -g flag:

npm install -g express

This will install the package in:

~/.nvm/node_version/lib/node_modules/package_name

Installing globally will let you run the commands from the command line, but you’ll have to use link the package into your local sphere to require it from within a program:

npm link express

You can learn more about the options available to you with nvm by typing:

nvm help
nvm help

Conclusion

As you can see, there are a quite a few ways to get up and running with Node.js on your Ubuntu 14.04 server. Your circumstances will dictate which of the above methods is the best idea for your circumstance. While the packaged version in Ubuntu’s repository is the easiest, the nvm method is definitely much more flexible.

Note: for me, other two methods list on this page do not work, so I just use NVM

And everytime reboot the system, it has to command to boot the node server. though there should be some setting to boot the node server automatically:

nvm use 0.12.6
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s