How to install node.js with nvm on Ubuntu

Node.js

It’s recommend to use nvm

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

Check nvm github to find the latest version and run command like:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

If you see “Command ‘nvm’ not found” while typing “nvm”, try to close and reopen your terminal.

If the problem still exists, check if nvm has been configured in .bash_profile (sometimes it’s been configured in .bashrc on ubuntu 16)

Check remote version:

nvm ls-remote

Check current node version:

node -v
v8.10.0

Install a new version:

nvm install v10.12.0

Switch version:

nvm use 10.12.0

Install a module:

npm install -g hexo

It will be installed in:

~/.nvm/versions/node/v10.12.0/lib/node_modules

Write a test script, name it as 01.js:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end("Hello, I'm running on port 4000!\n");
}).listen(4000);

console.log('Server running at http://127.0.0.1:4000/');

Run:

node 01.js

Now it can be accessed via http://127.0.0.1:4000

Share