This article is deprecated and no longer maintained.
Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates.
This article may still be useful as a reference, but may not follow best practices or work on this or other Ubuntu releases. We strongly recommend using a recent article written for the version of Ubuntu you are using.
If you are currently operating a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
If you already know what Node.js is what it’s for and why it’s cool, then skip straight to the installation directions. If you want to know a bit more about node and it’s ecosystem read on.
For those who haven’t heard node.js is, it is the hot new cool kid on the block in web application development. It lets you write web apps that use Javascript on both the server and the client, so you don’t need to know multiple programming languages to program your website. It’s also really good at handling real-time concurrent web applications, which makes it a great choice for a lot of modern web apps.
The downside though is that all these cool new features are really, really new. As a result, getting up and running with node.js isn’t as easy as, say, getting WordPress up and running on your web server.
This is the first in a series of how to install, code in, and use node. Joyent, the team behind node.js, has been improving node.js at a frantic pace, to the point where there are multiple releases of the software every month. For the most part, they’ve done a pretty good job of keeping things compatible; the things you write for one version of node will work just as well in the next. But nonetheless, sometimes a particular node app will only work with one version of node. And you will need to upgrade or downgrade your node.js install in order to use it.
This used to be a pain, but the node community has come together and created a great solution that lets you easily manage all your node installations and change node versions whenever you feel like it. It’s called NVM, or the Node Version Manager.
The install process couldn’t be easier. Once you’re logged into your VPS, run this command:
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
You’ll see some output fly by, and then nvm will be installed. You will see a line that says:
=> Close and reopen your terminal to start using NVM
It’s not actually necessary to log out, we just need to make sure that the changes nvm made to your path are actually reflected, so just do:
source ~/.profile
Alternatively, run the command suggested in the output of the script. Now type:
nvm ls-remote
Should you see the error, -bash: nvm: command not found
it may be because git is not installed.
Go ahead and install git and rerun the script:
apt-get install git
And you will be shown a list of all the available versions of node.js. You can always find out the latest stable release by heading to the node.js website, where it’s printed in the center of the page.
To install version 0.10.13 (the latest as of this writing) type:
nvm install 0.10.13
If you type:
node --version
You will now see that node v0.10.13 is installed and active. If you had an older node app that only works with node v0.8.16, and wanted to downgrade, then you would input:
nvm install v0.8.16
to install and switch to v0.8.16.
When you’re done and want to switch back to v0.10.13, you can do so with nvm’s use command:
nvm use v0.10.13
Nvm is great and makes switching between node versions easy and convenient. However, there’s one caveat. If you type:
which node
you will see something interesting. Nvm installs node.js inside your user’s home directory. This is fine for development, but if you want to actually host node applications, you don’t want to install the latest new version of node via nvm and discover that you’ve inadvertently caused your production node app (which can be incompatible with the latest node.js) to stop working. It’s best to install one copy of node globally so that other users can access it, and use nvm to switch between your development versions.
To do this, run the following command (entering your user’s password at the prompt):
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
The above command is a bit complicated, but all it’s doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS) and setting the permissions so that all users can access them.
If you ever want to change the version of node that’s installed system wide, just do another nvm use vXX.XX.XX to switch your user’s node to the version you want, and then re-run the above command to copy it to the system directory.
To check that it works, become the root user and do another which command to make sure that node is now installed to /usr/local/bin:
sudo -s
which node
You should see:
/usr/local/bin/node
Congrats! Node.js is now installed and ready for use. Enjoy!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Great article. Straightforward instructions.
Great, thanks!
The only line that didn’t work for me is “source ~/.bash_profile”.
I (on Ubuntu 12.04) had to do “source ~/.bashrc” and “source ~/.profile” .
@erelsgl: Thanks, I’ve updated the article. It should be .profile.
Another problem I just encountered is that, after reboot, the node version goes back to the previous one (see also this SO post: http://stackoverflow.com/questions/9170713/node-js-version-goes-back-to-0-4-form-0-6-on-reboot-nvm ). To solve this, I had to add the line “nvm use xxx” to my .profile script.
@erelsgl You could also do
nvm alias default XX.XX.XX
From what I see, .profile starts nvm and that will set the $PATH correctly.
I have found that when using non-login shell (for example an SSH terminal), the .profile does not get sourced. I found some info about how to deal with it:
I ended up moving: [ -s $HOME/.nvm/nvm.sh ] && . $HOME/.nvm/nvm.sh # This loads NVM from .profile to .bashrc
In order to select the correct path anytime the server restarts, with or without console, I had to put it in a “@reboot” record in the root crontab:
@reboot PATH=/root/.nvm/v0.10.18/bin:$PATH
Another way to solve this is to use nvm-global: https://github.com/xtuple/nvm
Great article, nice and clear – thanks!
This tutorial didn’t work on the LAMP droplet, it asked me to install git first (the tutorial says you can do it later). So I tried installing git and THAT didn’t work <grumble> Here are the required steps that work for the LAMP stack image: apt-get install git-core (which fails) apt-get update (which works) apt-get install git-core (which now works) curl https://raw.github.com/creationix/nvm/master/install.sh | sh (which now works) n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local (which fails) cd ~ cp -r .nvm/v0.10.13/bin/node /usr/local (which works)
I very much appreciate the effort Digital Ocean is making in these tutorials, but they are either not configuring some of their droplet images properly or these instruction don’t work across all versions of unix. This tutorial was marked ‘beginner’ and obviously, it was far from that.
@scott it didn’t say ubuntu or did he mention a droplet. If you would have used Arch or Fedora there wouldn’t even be an apt-get, also explaining for every distro doesn’t make a lot of sense and would just clutter the tutorial. Updating repos and installing apps should count before beginner level, as its the first thing you learn. Also Updating repos should be the first thing to do on any distro anyway as you can’t expect DO to do daily updates of their droplets. But i agree that if you need git for the first command, it should say so in the beginning, but the ‘copy’ command of yours in not a good solution, as its explicit for one node version and not the one you are using right now. But maybe it should mention to move yourself to you home folder (cd ~) before executing.
NVM install script needs to be updated. Here is the newest curl, courtesy of the github page:
curl https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh | bash
BE CAREFUL! The command listed above, is actually not well thought out and could lead to SUDO breaking (at the least) on many systems. Consider the following
This recursively will change permissions on all executable files within /bin to 755, which is not a good thing in the case of SU or SUDO, which requires the sticky bit being set and will result in the following, unwanted behavior as the final part relocates the entire /bin to /usr/local/bin…
Perhaps a safer way to do this command, would be to remove the wildcard ( * ) and do something like this…
Even wild card on say /bin/n* would include - /usr/bin/nproc - used for limiting system processes and resources… either way… I really suggest NOT using that command and add the following code for each users ~/.profile
This is not a complete example but using environment variables is a lot safer than changing system binary permissions and copying these without the correct permissions, setuid, stickybit etc… I appreciate the article and thanks for sharing! :-)
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
should be
curl https://raw.github.com/creationix/nvm/master/install.sh | bash
This helped a lot. Thanks.
Hi - I maintain nvm. Please update your link to use the latest tagged release (per the readme on the github repo). Installing from
master
is dangerous, as you may install unstable code. Similarly, please pipe it to bash and not to sh (as is in the readme).I’ve updated the command, thanks!
hmm does following the above mean you can only install new packages under root permissions? I previously had installed with npm but it was in my users home directory, which I understand is not ideal,
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
command to move it to the/usr/local/bin
folder. Now I can no longer install packages, like ember-cli, without using sudo, which you are not supposed to do according to their documentation.The only problem I have with this is that node isn’t in my path. That is to say that running
$ node --version
gets mebash: node: command not found
. Anyone got any thoughts on this?Try adding the following line to
/home/youruser/.bashrc
:You can then either run it manually to log out and log back in in order to have node added properly to your $PATH.
This comment has been deleted
Yeah, I did that but when I open my shell I now get
bash: [/home/<my username>/.nvm/nvm.sh: No such file or directory
This comment has been deleted
In case of the node.js app running on a server with a non-interactive shell, the PATH is missing /usr/local/bin. What’s the best practice for that ? Should I export it in the init script ?
Your
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
command has messed up with my server and now I cannot sudo, because I started getting this error:Any suggestions?
Very good tutorial. I like the part when installing Node globally and using it as a standard user. Thanks for sharing.
Great article! But I’m unable to start an application with this, I’m trying to install an application generated with Express, but
npm install
does not create ./bin/www required by the app. Is there way to remove all of this so I can try another nodejs installation methods? Regards!for every new terminal wndow, i have to source the ~/.profile and run nvm use <node version> any sollution to this?
Thanks!
Perhaps worth adding that it’s necessary to deactivate your node version through nvm after running the command to copy it to
/usr/local/
in order for that to be used. Stumped me for a little while. Otherwise, thanks for the great article!I tried to run the big command
but the first time I got
I tried again, this time without any feedback, which I think was ok. I then run
sudo -s; which node
I gotTo get it right I had to restart the terminal.
Broke my sudo D:
You should listen to mrhassell’s comment further up this comment thread and change that part of the article to something safer. Luckily this just happened on my personal server and reinstalling Ubuntu won’t be hard. Here’s the whole story.
Now I know not to trust odd-looking commands even on digitalocean…
I’m getting this response when I run the command to fix which node:
cp: cannot create regular file ‘/usr/local/bin/node’: Text file busy
Hi, I’m running a Centos 7 and I followed all the steps from above, so far everything goes ok, but I’m having this issue, all my logged in users can see and make use of node js, except for one: the root user, it cannot see or execute node, even thought when I execute:
which node
I get this message: “/usr/bin/which: no node in (/sbin:/bin:/usr/sbin:/usr/bin)”
It only happens with the root user.
By the way great article.
I know that isn’t the best way to resolve the “user variable environment” issue, but here is the way that I found to set node in env everytime that change node version by using
nvm use <version>
command:This code is a piece found in
~/.nvm/nvm.sh
that log when thenvm use <version>
command is runned successfully.The difference here, is that
sudo
password will be asked always that you runnvm use <version>
[]'s
Latest install script:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash
source: https://github.com/creationix/nvm#install-script
**Great ** this helped alot.