DEV Community

Cover image for Linux: Using NVM to upgrade nodejs
Wilberto Gonzalez
Wilberto Gonzalez

Posted on

Linux: Using NVM to upgrade nodejs

Step 1: Install NVM

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

This command will install nvm and adds the source line to yours: '.bash_profile', '.profile' and '.bashrc'.

To check if nvm is installed run following command:

nvm --version

NOTE: If, terminal say: "Command not found" , restart you terminal with exit command, then 'Ctrl + Alt + T'.

Step 2: Install the new version

nvm install node
#or
nvm install <NODE_VERSION>

This will install the most recent version of node inside the '.nvm' file on your '/home/user/' directory.( If you run the first command )

Step 3: Reinstall global packages

Note: In my case, the following command:

nvm install node --reinstall-packages-from=default

...doesn't work on my system or maybe I did it in the wrong way.😒

So, the next step is installing all your global packages ......manually 😩.

You need to switch to your newly version with: nvm use node

...then, do the respectively process to install all your global packages.

Bonus 👊🎉

Purging the old version of Nodejs

In my case: I want to delete the old nodejs version from my system, because it's not longer a supported version.

So ,I need to purge the default Nodejs version.

Type the following to do it:

sudo apt-get purge --autoremove node

Type 'Y' to proceed deleting the default nodejs version and its dependencies.

UPDATE: this command doesn't delete all the folders related to Node.if you change the root directory...like me 😅, something weird occur when you try to remove its packages.

You need to (after running the above command) proceed remove all the hidden folders in your '/home/user/' which start with ".node*" or ".npm" and reboot your computer.

It should work.🖖

NOTE: the " * " means ,whatever follow the string

Setting new version as default

UPDATE: My Apologizes, the command 'nvm alias default node' doesn't initialize node's newest version in the boot process. The above command only set up the new version of node as default.

You still need to run the command manually to use the newest version.

But I found a solution, I'll show you later.

PD:

If you think that I lost something important in this 'tutorial' or may be I wrote something wrong, please leave a comment. I will 'fix that issue'.

Sources:

Top comments (0)