Updating node v18 to v20 Ubuntu.. root and non-root showing different versions

Hi there, I’m trying to update node versions from v18 to v20 via NVM with a droplet that was created with node from the marketplace. Node installs just fine, I can set the version nvm use or nvm alias default vx.x.x. Challenge is for my non-root user the version shows as v20.12.2 (correct) but not when using sudo node -v or nvm ls As Root:

node -v

As non-root:

nvm ls
->     v20.12.2
default -> v20.12.2

node -v
sudo node -v

How do i make the update to the right version? thanks in advance

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 18, 2024

Hi Greg,

I believe that this is a standard thing when using NVM.

NVM installations are typically user-specific, meaning the Node versions installed and managed by NVM for one user are not automatically available for other users, including the root user.

If you intend for the root user to also use NVM to manage Node versions, you will need to install NVM separately for the root user. Here’s how you can do it:

  1. Switch to the root user:

    sudo su -
  2. Install NVM: Follow the same installation process you did for the non-root user. You can find the installation command on the NVM GitHub page, usually:

    curl -o- | bash
  3. Close and reopen your terminal to start using nvm, or run:

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  4. Install and use the desired Node version:

    nvm install v20.12.2
    nvm use v20.12.2

If you do not want to install NVM for the root user, avoid using sudo with NVM-managed Node commands. NVM and sudo typically do not work well together because sudo does not have the same environment variables as your user. Therefore, sudo node -v might refer to a different Node installation.

If configuring NVM for root isn’t desirable, another approach is to install Node.js globally using a package manager which will be available system-wide, including for the root user:

  1. Uninstall the current system Node.js (if it was installed and not managed by NVM):

    sudo apt-get remove nodejs
  2. Install Node.js globally:

    curl -fsSL | sudo -E bash -
    sudo apt-get install -y nodejs
  3. Verify the installation:

    node -v  # Should show v20.x.x

If you cannot install NVM or do not want to change the global Node version for the root, consider setting an alias in the root’s shell configuration file (like .bashrc or .bash_profile) to explicitly point to the Node version managed by NVM for the non-root user:

echo 'alias node="/home/nonrootuser/.nvm/versions/node/v20.12.2/bin/node"' >> /root/.bashrc
source /root/.bashrc

Adjust /home/nonrootuser to the actual path where the non-root user’s home directory is located.

Let me know how it goes!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel