Question

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
v20.12.2

As non-root:

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

node -v
v20.12.2
sudo node -v
v18.12.1

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- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    
  3. Close and reopen your terminal to start using nvm, or run:

    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. 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 https://deb.nodesource.com/setup_20.x | 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!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.