02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Managing Different Versions of Node.js on Linux

Managing Different Versions of Node.js on Linux

In this tutorial, we provide a comprehensive guide on managing different versions of Node.js on a Linux system. Using multiple versions of Node.js is often necessary when working on different projects that require specific versions. In this guide, we'll show you how to switch between versions seamlessly using tools like nvm (Node Version Manager). This tutorial was originally created in 2016, and we’ve included any critical updates to ensure you have the latest information.

Step 1: Installing Node Version Manager (nvm)

To effectively manage multiple versions of Node.js, the Node Version Manager (nvm) is the best tool to use. It allows you to easily install, update, and switch between different versions of Node.js.

  1. Install nvm by running the following command in your terminal:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    

    This command will download and install nvm on your system.

  2. Load nvm into your current terminal session:

    source ~/.nvm/nvm.sh
    

    You may need to restart your terminal for the changes to take effect.

Critical Update (2024): The latest version of nvm is v0.39.1, which provides better compatibility and more features. Always check the nvm GitHub page for the latest version.

Step 2: Installing and Managing Node.js Versions

With nvm installed, you can now install different versions of Node.js as needed.

  1. Install a Specific Version:

    nvm install 16
    

    Replace 16 with the version number you need. For example, you can install Node.js version 18 by running nvm install 18.

  2. Check Installed Versions:

    nvm list
    

    This will display all the versions of Node.js installed on your system.

  3. Switch Between Versions:

    nvm use 16
    

    Use the version number that you want to activate for your current terminal session. This flexibility is very useful when switching between projects with different requirements.

Note: It is recommended to use Node.js version 18 or higher for improved performance and compatibility with modern JavaScript features.

Step 3: Setting a Default Node.js Version

If you frequently use a specific version of Node.js, you can set it as your default version.

  1. Set a Default Version:
    nvm alias default 16
    
    This ensures that whenever you open a new terminal session, Node.js version 16 will be automatically activated.

Step 4: Uninstalling Node.js Versions

If you no longer need a particular version of Node.js, you can easily uninstall it to save space and reduce clutter.

  1. Uninstall a Version:
    nvm uninstall 16
    
    This command will remove the specified version from your system.

Conclusion

Managing multiple versions of Node.js on a Linux system is made simple with nvm. This tool allows you to install, switch, and remove Node.js versions easily, making it an essential utility for developers working on projects with varying requirements.

Using nvm ensures that you can keep your development environment organized and that your projects use the Node.js version they need for optimal functionality. Make sure to always check for updates and use the latest LTS version to ensure the best performance and security.

Video Transcript

00:00In the last lecture, we installed no Js, NNPM,
00:04supposedly onto our server,
00:06but the version was very late, a very, very late,
00:11you say no, very old.
00:12So we wanna go ahead
00:14and make sure that the version on our Linux
00:15is more up to date.
00:16So we're gonna take a look at
00:17how do you install different versions
00:20of No Jazz onto your operating system.
00:23So by the end of this lecture,
00:24we're gonna get the newer packages to run on my CentOS,
00:30and we're also gonna have to use Root to Do It,
00:33and we're gonna show you how to pick
00:34between production and latest.
00:36And also, where could you get more
00:38information about running it?
00:39If you're running on a server that is not sendo,
00:41that doesn't have Yum, we're gonna show you that as well.
00:45So let's jump right into it.
00:48One last thing, we have to talk about installations,
00:51namely related to when you're running on
00:55a Linux on an operating system
00:57that is geared towards production.
00:59Go ahead and go into your, uh, SSHS, not into it,
01:03but SSH, into your server.
01:05And I'm gonna go ahead. I'm, I'm, I'm in my server right now
01:08and I just wanna show you something.
01:10The version that is installed
01:11by default onto the production is a really,
01:14really low version, zero point 10, point 42,
01:18which is extremely low.
01:19And as we talked about previously when installing onto the
01:22desk desktop version four point onwards is encouraged
01:26by Node as the stable version to use in production,
01:29while right now six point something is in the works every
01:32time, you would want to go onto your,
01:35onto the nodejs.org website
01:37and check to see what is the most stable build
01:40that you could update towards
01:41and update your code if you wish to do so.
01:44Now I wanna show you
01:45how could you update the version from such a low version
01:47to a four or even six point something.
01:51Now, to do that, we have to go into the SIO command,
01:54which basically means we wanna connect as the route itself,
01:57not as an administrator.
01:59And make sure that you type in the password of your route
02:02to move into the root structure.
02:04Once in the route, you could go back into your browser
02:07and you'll find a link in the link descriptions
02:10that are added to this chapter.
02:12Or if you're in the main page, just go
02:15and click onto other downloads
02:17and you'll find on the bottom installing node js
02:20via package manager.
02:22In your case, look
02:23for the distribution that you're working with.
02:25In my case, I know I'm working with Yum,
02:27so I'm just gonna search for Yum to find the section
02:30that relates to node js installations on Santos
02:35N-R-H-E-L five, which is red hat,
02:39red hot hat, hot hat.
02:41Okay, so what, what I want to do next as root,
02:45because we're root in our, in in our, um,
02:48Linux right now in our Santos right now, I'm gonna go ahead
02:51and choose if I want to set up, uh, if I want
02:55to set up right now I'm using Node version
02:57zero 10 currently,
02:58So I don't have to do anything for that.
03:01If I wanna run on the version four point something,
03:03I'm gonna copy this into my command prompt If I want a
03:07version six onwards that I'm gonna go ahead and copy here.
03:10But in our case, because we already, uh, decided
03:13that version four is the most stable version currently
03:16and is the one is that is encouraged currently by node js.
03:20'cause we saw that on the main page of node js.org.
03:23So I'm just gonna copy that version four,
03:26and I'm gonna go ahead into my terminal again
03:28and just paste that line of code and just enter.
03:32Once I do that, it's gonna go through a process
03:34of installing the repositories
03:39that enable me to install the version that I want of Node.
03:43Now the system already has node,
03:46it's telling me from an alternative source.
03:49So I wanna first remove the version of node
03:51that I have and remove it.
03:53Currently we don't have NPM as well,
03:55so I could go ahead into Yum.
03:57And I'm gonna remove and notice I'm not using pseudo
04:00because I'm working as the root right now.
04:02And I'm gonna say that I wanna remove no js.
04:05Remember in the last video, we did not re,
04:07we did not install NPM, so I don't need to remove NPM
04:11because it's not installed.
04:13So I'm gonna go ahead and remove Yum from my packages,
04:17and I'm gonna say yes for that removal.
04:19Once I'm done with the removal process, I could go ahead
04:22and, and, and approach yum.
04:25A new time asking to install no js Notice
04:29that previously the version
04:30that was installed was zero point 10, point 42.
04:33But now when it's gonna go through the process
04:36of figuring out to install, oh, I, I typed your.by mistake,
04:40so let me just run it one more time.
04:41So now when it's gonna run through the process,
04:43it's gonna tell us, Hey, do I want to install version 4.45?
04:47And I sure do, and I'm gonna go ahead
04:49and install no JS in version 4.45.
04:53Once this installation is complete,
04:54I'm gonna have the package in version four and onwards.
04:58If I wanted to install the version in six
05:00and onwards, then I would've set up this versioning
05:03setup before that.
05:05Now, in any stage, if you have any regrets, you could
05:08always revert back
05:10and the process of reverting back always involves you
05:13deleting the the version that you've installed.
05:16So it's gonna ask me, am I okay with this?
05:18I'm gonna say, yes, I'm gonna install this package
05:21and it's gonna install the package.
05:22I'm gonna have no js.
05:24And once I have this installed, if I just check to see
05:26what version I have, you'll see I'm in version 4.45.
05:30And again, at any stage, I could delete it by calling Yum
05:34and remove and node js.
05:37And once I've removed the node js,
05:39I could go into the package manager, I could go in this list
05:42and say, do I want the version four point something,
05:45do I want the six point something
05:46or do I wanna set up with the original version,
05:49which was the zero point 10 versioning?
05:52In this case, we wanna make sure
05:53that we're staying on the four point something,
05:55so we're gonna leave it the way
05:56It is. And
05:57that's all we have
05:58to talk about control installing throughout this chapter,
06:02we went ahead and and set up our command prompt on Windows.
06:06We installed Node and OTO NPM,
06:11and lastly, we made sure that we had the version
06:14that we wanted installed Oso Analytics.
06:17Before we wrap things up, I just wanted
06:19to say one more thing, that there's one more way
06:21of installing, and this one involves having multiple
06:24versions of no JS running at the same time.
06:28For example, if you have a few different sites running on no
06:31js, when one site has one version
06:34and another has a different, in that case, you'd want
06:37to use a package manager that is called NVM.
06:40More information about
06:42that which is a little bit more advanced you could find in
06:44the links that are associated to this chapter.
06:47So that's it for this chapter.
06:51In this lesson, we updated our version of node js
06:54to be the latest version that we've wanted it to be,
06:58and that really wraps up our conversations about
07:01installations and setups.
07:02In the next chapter, we're gonna start diving deep into the
07:06world of NPM and understand what package managers are
07:09and more explicitly what is the NPM package manager
07:13and how to work with it.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!