Installing Node.js Tutorial: Using nvm
Installing Node.js Tutorial: Using nvm
In this quick tutorial, we'll take a look at how to install nvm, and then how to start using it as your version manager for Node.js
Join the DZone community and get the full member experience.Join For Free
Learn how Crafter’s Git-based content management system is reinventing modern digital experiences.
As with any programming language, platform, or tool, the first step to using it is getting it installed. Many of them typically come with a speedy way to upgrade when a new version is available.
By default, there's not a way to upgrade the version of Node.js you've got from within Node.js itself. That said, there's a fantastic tool for the community called nvm that allows you to manage the versions of Node.js that you've got installed locally.
One awesome aspect of
nvm is that it manages the versions of Node.js, it doesn't just upgrade them. This means you can have the latest version of Node.js, the latest versions of all the LTS release lines, and any number of other versions you want to use or test as well.
In this quick tutorial, we'll take a look at how to install nvm, and then how to start using it as your version manager for Node.js. Once we've completed the tutorial, you'll be ready to take the next step with Node.js.
This guide covers installing nvm on MacOS and Linux - note that all versions of Node.js may not support every version of MacOS or Linux.
Step 0: The Quick Guide (TL;DR) to Get Node.js Installed Using nvm
Here's the abbreviated guide, highlighting the major steps:
* Download the nvm install script via cURL: *
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash * Ensure that nvm was installed correctly with
nvm --version, which should return the version of nvm installed. * Install the version of Node.js you want * Install the latest version with
nvm install node * Use the latest version with
nvm use node * Install the latest LTS version with
nvm install --lts * Use the latest LTS verison with
nvm use --lts
Step 1 (Optional): Ensure Your System has the Appropriate C++ Compiler
In some cases, like when installing Node.js releases from their source or installing versions of Node.js before
0.8.6 (when the project started shipping binaries), you'll need to ensure that your system has the appropriate C++ build tools.
For LTS and modern releases, you will not need this step. That said, it's nice to have to ensure that the majority of requirements are met in any scenario.
On MacOS, you've got two options for a C++ compiler: the full XCode application or the stand-alone Command Line Tools portion of Xcode.
To get these on MacOS, you can follow these steps:
- Open your terminal of choice.
xcode-select --installas a command.
- A popup will appear.
- Allow the download to run to completion.
- If the installation went uninterrupted, you should have the necessary tools to use nvm!
On Linux, the C++ compiler will vary from distribution to distribution. For example, on Debian and Ubuntu, you'll need to install
libssl-dev, but this may be different on your given Linux distribution.
libssl-dev on Debuan and Ubuntu distributions, you can run these commands:
sudo apt-get install build-essential # Install the build-essential package - let this run to completion sudo apt-get install libssl-dev # Install the libssl-dev package - also let this one run to completion
Step 2: Download nvm with the Install Script
Once you've got the right C++ compiler for your system, it's time to run the nvm install script. Here are the single-step install scripts for both MacOS and Linux. You've got the option of cURL or Wget, but both achieve the same result.
Note: If your Linux system doesn't have either cURL or Wget, you can run
sudo apt-get install curl and use the cURL method.
Running the Install Script with cURL:
To install nvm with the cURL method, run the following command in your terminal:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
Using the Install Script with Wget:
To install nvm with the Wget method, run the following command in your terminal:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
Step 3: Verify that nvm was Properly Installed
After running the install script from Step 2 nvm should have successfully installed. To ensure that nvm is up and running on your machine, you can test it with the following command:
This command will return something like (though not necessarily exactly) the following:
nvm --version # The command we ran - it checks the currently installed version of nvm 0.33.0 # The current version of nvm - yours may differ!
nvm Command After Running the Install Script?
If you're using MacOS, you may be missing a
.bash_profile file - to troubleshoot this, you can run
touch ~/.bash_profile in your command line and re-run the installer script.
If the problem persists after that, you can open the existing
.bash_profile file (using your favorite text editor) and add the following line to it:
If you're still having issues, you can take a peek at this issue to find a discussion of the problem and a collection of possible resolutions.
Step 3: Using nvm to Manage Node.js
Congratulations! You've now got
nvm - a tool to easily allow you to manage and swap out the versions of Node.js you've got installed locally. Now, let's get you started with doing just that.
Install the Latest Node.js Version
To install the latest available version of Node.js, you can use the following command:
nvm install node
Next, to use that version of Node.js in any new shell, you can simply run the
nvm use node
Install the Latest Node.js LTS Version
To install the latest available LTS version of Node.js, you can run the following command:
nvm install --lts
And to use that latatestTS version of Node.js in any new shell, you can simply run the
nvm use --lts
Step 6: Go Build Applications, APIs, Tools, and More with Node.js!
Now you've got a fantastic version manager for Node.js. It's time to start building!
That said, if you want to keep in touch with the Node.js ecosystem, you should follow @NodeSource on Twitter! We'll keep you updated with important news from the core Node.js project, fresh, and useful Node.js tutorials, and more.
Published at DZone with permission of Tierney Cyren , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.