Node.js on Ubuntu Server

How to install and manage Node.js on Ubuntu Server?

How to Install Node.js on Ubuntu?

There are 3 ways of installing Node.js on Ubuntu Server.

Installing Node.js with Apt from the Default Repositories

Node.js is a part of default repositories of Ubuntu Server and thus may be installed through Apt from default repositories. In such way, the Node.js service is installed with root permissions which should be changes for security reasons as soon as possible.

  1. sudo apt update - update the package lists for upgrades and updates on your Linux system
  2. sudo apt install nodejs - install Node.js through the apt package manager
  3. node -v - check installed node version (verification, than the node.js was installed)
  4. Change permissions of Node.js service

Installing Node Using the Node Version Manager (Recommended)

Node Version Manager (NVM) is a piece of shell code that allows you to easily install and maintain many different independent versions of Node.js, and their associated Node packages.

NodeJs may be installed under any account, see below:

  1. Check latest verision of NVM on GitHub.
  2. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh - Check NVM code
  3. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash - install NVM code. The NVM is installed with permissions of a user that processing that request (you). NVM is installed to user account, specifically "~/.bashrc" directory.
  4. source ~/.bashrc - source/reload the script for the option to use it
  5. nvm list-remote - check available Node.js versions
  6. nvm install v18.17.0 - install requested version of Node.js, in this case Node Js v18.17.0. Node.js is installed with permissions of a user that processing that request (you)
  7. node -v - show currently active version

As of Node.js is installed for user account only, it is known under node -v of current user, but not under any other user nor sudo node -v. There are 2 ways to make node command (access to teh Node.js client) available also for other users:

  1. Symlink

    There's possible to create a symlink from /usr/local/bin/node to the node client in the user's account $NVM_DIR/versions/node/$(nvm version)/bin/node

    sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
  2. Environment variable with adding to user group (recommended)

    When creating native services, you can add there a Environment variable referring to the Node.js client, see

    [Unit]
    Description="My service"
    Wants=network-online.target
    After=network-online.target
    [Service]
    User=<serviceUser>
    Group=<serviceUser>
    Type=simple
    Restart=always
    RestartSec=5
    WorkingDirectory=/directory/to/service/client
    Environment="PATH=/home/<userNameWithNodeClient>/.nvm/versions/node/v18.17.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ExecStart=...

    Note, to make the node accessible, the <serviceUser> must be added into the <userNameWithNodeClient> group.

    sudo usermod -aG <userNameWithNodeClient> <serviceUser>
  1. Create user and directory
    sudo useradd --system --shell /bin/bash --home /srv/nodejs nodejsusr
    sudo mkdir -p /srv/nodejs
    sudo chown nodejsusr:nodejsusr /srv/nodejs
  2. Switch at nodejsusr
    sudo -i -u nodejsusr
  3. Install and enable NVM
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
    Create bashsrc
    touch ~/.bashrc && nano ~/.bashrc
    Open the file with following content
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
    Reload shell profile
    source ~/.bashrc
    Check installed nvm version
    nvm --version
  4. Install Node js
    • nvm list-remote - check available Node.js versions
    • nvm install v18.17.0 - install requested version of Node.js, in this case Node Js v18.17.0.
    Check Installed NodeJs version
    node -v
    Check installed npm version
    npm -v
  5. Retrun back at standard user
    exit
  6. Set shared access for nodejs and npm
    sudo ln -s /srv/nodejs/.nvm/versions/node/$(ls /srv/nodejs/.nvm/versions/node)/bin/node /usr/local/bin/node
    sudo ln -s /srv/nodejs/.nvm/versions/node/$(ls /srv/nodejs/.nvm/versions/node)/bin/npm /usr/local/bin/npm
    Shared access for yarn
    • Log in to nodejsusr and
      sudo -i -u nodejsusr
      Reload bashrc
      source ~/.bashrc
      get corepack yarn
      which yarn
    • Return back at standard user
      exit
    • Create the symlink to corepack path
      sudo ln -s /srv/nodejs/.nvm/versions/node/vXX.X.X/bin/yarn /usr/local/bin/yarn
    • Check corepack availability
      yarn --version

Useful commands

  • nvm ls - Listing Installed NodeJS Instances
  • npm -v - get version of NPM (When installing a Node.js instance, nvm also installs a compatible npm version.)

Installing next versions

  • nvm list-remote - check available Node.js versions
  • nvm install vXX.XX.X - install requested version of Node.js. After that, this new version will begin default running node version on Ubuntu Server. The previous version is still available, see nvm ls.

Switching to selected node version

  • nvm use XX.X.X

Uninstalling useless versions

  • nvm uninstall XX.X.X