Skip to content

How to Install Latest Node.js on Ubuntu

Advertisement

To install the latest version of Node.js on Ubuntu, follow a few simple steps. First, you’ll need to update your package list. Then, install Node.js from the official repository, which always provides the newest version. Node.js is a popular runtime environment that allows you to run JavaScript code outside a browser. It’s essential for developing modern web applications.

You can also use the Node Version Manager (NVM) to install a specific version of Node.js on your system.

Step 1: Configuring Node.Js PPA

Node.js has two types of releases: LTS (Long Term Support) and the current release. Choose the version you need for your project. Let’s add the PPA to your system to install Node.js on Ubuntu.

  • Use Current Release: During the last update of this tutorial, Node.js 22 is the current Node.js version available.
    sudo apt install -y curl 
    curl -sL https://deb.nodesource.com/setup_22.x | sudo -E bash - 
    
  • Use LTS Release – Instead of the current release, it’s a good idea to use a stable version. During the last update of this tutorial, Node.js 20 is the latest LTS release available.
    sudo apt install -y curl 
    curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash - 
    

For this tutorial, I am using the latest current release and added their PPA to my system.

Step 2: Install Latest Node.js on Ubuntu

You have successfully configured Node.js PPA in your Ubuntu system. Now execute the below command to install Node on Ubuntu using apt-get. This will also install NPM with node.js. This command also installs many other dependent packages on your system.

sudo apt install -y nodejs 

That’s it. This will install Node.js on your Ubuntu system.

Step 3: Check Node.js and NPM Version

After installing node.js verify and check the installed version. You can find more details about the current version on node.js official website.

node -v

v22.9.0

Also, check the npm version

npm -v

8.19.3

Step 4: Create Demo Web Server (Optional)

This is an optional step. Suppose you want to test your node.js install. Let’s create a web server with “Hello World!” text. Create a file server.js

sudo nano server.js 

and add the following content


var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

Save the file and close. Then start the Node application using the following command.

node server.js

debugger listening on port 5858
Server running at http://127.0.0.1:3000/

You can also start the application with debugging enabled with the following commands.

node --inspect server.js

Debugger listening on ws://127.0.0.1:9229/938cf97a-a9e6-4672-922d-a22479ce4e29
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://127.0.0.1:3000/

The web server has been started on port 3000. Now access http://127.0.0.1:3000/ URL in browser. Now you will need to configure a front-end server for your app.

Conclusion

In summary, we have learned how to install Node.js on Ubuntu and similar systems using a new official PPA repository. This makes it easy to start developing with Node.js on Linux. We can also use this repository to keep our Node.js installations up-to-date with the latest stable versions. Be sure to check out other tutorials on our website for more tips and tricks about using Node.js!

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

55 Comments

  1. W: GPG error: http://dl.google.com/linux/chrome/deb stable InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 78BD65473CB3BD13
    E: The repository ‘http://dl.google.com/linux/chrome/deb stable InRelease’ is not signed.
    N: Updating from such a repository can’t be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.

    I’m getting these error while adding nodejs ppa

    1. It looks some other application is already running on port 3000. To find out process run below command:

      netstat -tulpn | grep 3000

  2. E: coult not get lock /var /lib/dpkg/lock-fronted -open
    this error shows when i enters te command

  3. http://archive.ubuntu.com/ubuntu yakkety InRelease
    Could not connect to 125.200.13.2:80 (125.200.13.2). – connect (111: Connection refused)

    this is the error i’m getting while installing ppa

    even sudo apt-get update is also throwing the same error
    What can i do please do reply

  4. On Ubuntu 18.04 server, I had to use ‘software-properties-common’ instead of ‘python-software-properties’:

    virtualbox-iso: Package python-software-properties is not available, but is referred to by another package.
    virtualbox-iso: This may mean that the package is missing, has been obsoleted, or
    virtualbox-iso: is only available from another source
    virtualbox-iso: However the following packages replace it:
    virtualbox-iso: software-properties-common
    virtualbox-iso:
    virtualbox-iso: E: Package ‘python-software-properties’ has no installation candidate

  5. node -v tells me node is not installed. Installing node-legacy puts down a symlink so that node -v works.

    However it returns v4.7.2.

    Ubuntu 17.04.

    Turns out my apt sources.list had a site which couldn’t be reached, so the PPA wasn’t being added, so it was just installing the distro version. With that fixed nodejs -v returns v8.9.1. nodejs-legacy won’t install though. I’ll just make the symlink myself.

  6. How set for custom domain on ubuntu ?

    var http = require(‘http’);
    http.createServer(function (req, res) {
    res.writeHead(200, {‘Content-Type’: ‘text/plain’});
    res.end(‘Hello World\n’);
    }).listen(80, “customdomain.com”);
    console.log(‘Server running at http://customdomain.com:80/‘);

  7. I have tried installing nodejs 8 , but my system always installs 4.2.6 version.Am using ubuntu 16.04.
    How can i solve this?Thanks

    1. Make sure that Curl is installed on your system, pull the repo, it should usually update your repos automatically, then you can run ‘sudo apt-get install nodejs’ to get the latest version

  8. node -v _does not display the version_, it prompts you to install nodelegacy.

    To view the node_js_ version, you run _nodejs -v_

    sudo apt-get install nodejs *does not install npm, you need to install it manually*

  9. Did you do sudo before running this command? The previous command uses sudo which is carried forward here too. If you took a long time to run the second command, the sudo will need to be revoked again.

    Simply follow the entire tutorial again but a bit faster this time.

  10. Small typo in the node.js code:

    res.end(‘Hello Worldn’);

    Should be:

    res.end(‘Hello World\n’);

    just a simple fix by adding the forward slash.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top