Installing a custom version of NVM and Node.js

 

Overview

This article walks you through installing Node.js using Node Version Manager (NVM) on a VPS or Dedicated Server.

DreamHost support is unable to assist with custom installations. The directions below explain how to install Node.js but if you find anything is not working as intended, you must contact a developer for further assistance.

FAQs

What servers can Node.js be installed on?

Node.js can be installed onto VPS and Dedicated Servers.

What version of Node.js is installed?

A default version of Node.js is only installed on certain operating systems. To confirm if Node.js is installed, log into your server via SSH and run the following command:

[server]$ node -v

Is it possible to run Node.js on ports 80 or 443?

While it's not possible to assign your app to use ports 80 or 443, you can configure a Proxy Server in the DreamHost panel. This allows your app to use a non-privileged port in combination with your SSL certificate.

Installing Node Version Manager (NVM)

These steps install Node Version Manager (NVM). You can then use nvm to install Node.js.

Before proceeding, visit the github nvm-sh page to determine what version of NVM to install. These instructions use version v0.40.1.

  1. Log into your server via SSH.
  2. Change into your user's directory.
    [server]$ cd ~
  3. Download NVM using curl. Change the version number as needed.
    [server]$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    • This command installs nvm into a new directory under your user named /.nvm.
    • This command also adds the following to your .bashrc file:
      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
  4. Update your .bashrc file to use these new settings.
    [server]$ . ~/.bashrc
  5. Confirm your local .nvm directory is set.
    [server]$ echo $NVM_DIR
    /home/username/.nvm

    This step only applies to Dedicated servers.

    If installing NVM as a user on a Dedicated Server, make sure the script isn't blocked by running the following as your user:

    [server]$ setfattr -n user.pax.flags -v "mr" $NVM_DIR/nvm.sh

    This command does not return anything. If you see an error such as setfattr: /nvm.sh: No such file or directory, something wasn't installed correctly. Try running the following command.

    [server]$ echo $NVM_DIR
    /home/username/.nvm

    You should see the path to your local .nvm directory. If you do not, make sure the .bashrc file was updated as explained above and try running this command again.

  6. Edit your .bash_profile and add the following line.
    source ~/.bashrc
  7. Update your .bash_profile so you can use this new version of nvm.
    [server]$ . ~/.bash_profile
  8. Run the following command to confirm nvm is now installed.
    [server]$ nvm --version
    0.40.1
    

Installing Node.js

Now that nvm has been installed, you can use it to install Node.js

  1. Check which versions of Node.js are available.
    [server]$ nvm ls-remote
  2. Install any version of Node.js you wish. Versions noted as LTS (Long Term Stable) are recommended.
    [server]$ nvm install v20.18.0

    If installing Nodejs as a user on a Dedicated Server, there may be an error that looks like this:

    #
    # Fatal error in , line 0
    # Check failed: reservation_.SetPermissions(protect_start, protect_size, permission).
    #
    #
    #
    #FailureMessage Object: 0x3dfe93bb400

    If so, you'll need to update the security controls on each newly downloaded binary file before it will work. Run the following command as your user after each new version you download:

    [server]$ setfattr -n user.pax.flags -v "mr" $(find $NVM_DIR -type f -iname "node" -o -iname "npm" -o -iname "npx")

    This will need to be done each time a new version is installed.

  3. Set your current version of node to your new version.
    [server]$ nvm use v20.18.0
    Now using node v20.18.0 (npm v)
  4. Confirm which version of Node.js is running.
    [server]$ node -v
    v20.18.0
    

Setting the default version of Node.js

After installing a new version of Node.js, your current Shell session should use it automatically when you log back in. If you notice that the version is not correct, you may need to review the .bash_profile instructions above.

You could also set the new version as your default version by running the following command. Make sure to change the version number to the version you've installed.

[server]$ nvm alias default v20.18.0
default -> v20.18.0

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?