How to Install Node.js with NVM and Nginx on Ubuntu 22.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

How to Install Node.js with NVM and setup Nginx. NVM stands for Node.js Version Manager which is more flexible tool to install and manage multiple versions of Node.js and the associated packages at the same time.

In this guide you are going to learn how install specific version of Node.js using NVM and configure Nginx and secure the installation using Let’s Encrypt. This setup is tested on Google Compute Engine running Ubuntu 22.04 OS

Install NVM

To install or update NVM you can use the curl command to download and execute the file. You can get the latest version from the nvm official repository. Copy the curl command from the readme section of the repo.

curl -o- | bash

This command will install NVM on your account. To use the tool you need to source your .bashrc file.

source ~/.bashrc

To verify the installation use the following command.

command -v nvm

If the installation is successful you should see the output as nvm.

List Node.js Versions

Once NVM is installed you can list all available Node.js versions using the ls-remote command.

nvm ls-remote

There will be a very long list to choose from. You can install any of the version listed here.

If you wish to install Node.js 12.18.3 version you cna simply use the install command with the specific version.

nvm install v16.15.0

Check the installtion using the default way.

node -v

can install a release based on these aliases as well. For example, to install the latest long-term support version, gallium, run the following.

nvm install lts/gallium

You can switch between any installed versions usingnvm use command.

nvm use v16.15.0

That’s it.

Create a Node.js Application

Now you can create a demo Node.js app.

cd ~/
sudo nano server.js

Insert the following code into the file

const http = require('http');
const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to Node.js!\n');

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);

Save the file and exit.

Install and Set up Nginx

sudo apt install nginx

Remove default configurations

sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default

Create new Nginx configuration

sudo nano /etc/nginx/sites-available/yourdomainname.conf

Paste the following

server {
     listen [::]:80;
     listen 80;


     location / {
         proxy_pass http://localhost:3000;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;

Save and exit the file

Enable your configuration by creating a symbolic link

sudo ln -s /etc/nginx/sites-available/yourdomainname.conf /etc/nginx/sites-enabled/yourdomainname.conf

Check your Nginx configuration and restart Nginx

sudo nginx -t
sudo service nginx restart

Now you can visit your domain name in browser, you should view the output of your server.js (Welcome to Node.js!)

Install Let’s Encrypt SSL certificate

HTTPS is a protocol for secure communication between a server (instance) and a client (web browser). Due to the introduction of Let’s Encrypt, which provides free SSL certificates, HTTPS are adopted by everyone and also provides trust to your audiences.

sudo apt install python3-certbot-nginx

Now we have installed Certbot by Let’s Encrypt for Ubuntu 22.04, run this command to receive your certificates.

sudo certbot --nginx --redirect --no-eff-email --agree-tos -m [email protected] -d -d

The Certbot client will automatically generate the new certificate for your domain and also configure HTTPs redirection.

Now you can configure auto renewal.

sudo certbot renew --dry-run


Now you have learned how to use NVM – Node Version Manager to install Node.js and also configure Nginx reverse proxy and install Let’s Encrypt SSL and secure it.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.