Setting up Node.js on your server is the first step to creating an awesome application on a performant and scalable JavaScript platform built on Chrome’s JavaScript run-time. This tutorial will walk you through three ways to take this first step. The first section will go over installing the server’s default stable version of Node.js. The next section will explain how to install a specific version of Node.js. Finally, the last section explains how to install Node.js via nvm, which will allow you to have multiple versions of Node.js installed and switch back and forth between versions. Let’s get started!
Installing the Distro’s Stable Version
Ubuntu by default includes a version of Node.js in its repositories that is stable for that version of Ubuntu. This is done to provide a consistent environment across servers. The version of Node.js will not be the most current stable version available, and will surely not be the latest version of Node.js.
Using this method is recommended if you are just wanting to spin something up that will be stable and ready for experimentation
Update apt
package index
Start by updating the package index used by the apt
package manager.
sudo apt update
Install Node.js and npm
via apt
package manager
Next, we’ll install the Node.js and npm
packages using the apt
package manager. npm
is not required, however you will need it if you plan to use any npm
packages.
sudo apt install nodejs
sudo apt install npm
Once those finish installing, go ahead and check to see which version of Node.js was installed to verify it was installed correctly.
nodejs -v
If you get a version back, you’re done! The distro’s default stable version of Node.js is now running on your server ready to run an application.
In the next section, we’ll go over how to install a specific version of Node.js in case you would like to install a more recent version.
Installing a Specific Version of Node.js
More than likely, you would like to be working with the latest LTS version of Node.js, or even play around with the latest features in the most current versions of Node.js. To do this, we will add the PPA (personal package archive) maintained by NodeSource of the version of Node.js we wish to use.
Grab the PPA installation script from NodeSource
First make sure you are in the home directory.
cd ~
Use curl
to grab the installation script of your preferred Node.js version from NodeSource. Make sure to replace <xx.x>
with the version you want to install.
curl -sL https://deb.nodesource.com/setup_<xx.x> -o nodesource_setup.sh
This will retrieve a file named nodesource_setup.sh
Run the installation file to add the PPA
sudo bash nodesource_setup.sh
This will run the installer, adding the PPA to your configuration. Your local package cache will be updated automatically.
Install the Node package
Now that we’ve added the PPA for the Node.js version we want to use, we can install the package. The nodejs
package also contains the npm
binary, so you will not need to install that separately like you did using the first method.
sudo apt install nodejs
Some npm
packages will need the build-essential
package to run correctly because they need to compile code from source. Let’s install that to make sure all the packages we might use will work properly.
sudo apt install build-essential
Now we should be good to go! Node.js and npm
are installed and running on our server and we are able to work with packages that need to compile code from source. Let’s verify everything installed correctly just to be safe!
Verify Node.js and npm
installed correctly
To verify that Node.js and npm
installed correctly, we will check the installed versions of each.
nodejs -v
npm -v
If the results of those look good, Node.js and npm
are properly installed and ready to use!
In the next section we will look at how to install Node.js using nvm
instead of apt
, which will allow you to have multiple versions of Node.js installed and switch between versions easily.
Installing Node via NVM (Node Version Manager)
Another option when installing Node.js is to use nvm
(Node.js Version Manager), which, as apposed to apt
which runs on the operating system level, runs within a directory in your home directory. Here you are able to install multiple versions of Node.js without affecting the others. The benefit of doing this is that you are able to switch back and forth between Node.js versions on your server and manage each version independently.
This is great in situations where you want to easily upgrade to a newer version of Node.js but still have the option to switch back to the exact Node.js environment you were running before the switch. It is also useful in scenarios where you might want to test your code on multiple versions of Node.js.
nvm
allows you to install multiple versions of Node.js independently and easily switch between versions on your server. It also has a lot of useful functionality that will not be covered in this tutorial. Check out the docs to learn more about what it can do!
Download and install nvm
First, we will grab the nvm
installation script from the project’s Github page and run it. Note the version of nvm
we use below may be different from the current version on Github. Please double-check the installation instructions on Github, which will have the latest version numbers in the url.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Verify Installation
Now we need to verify nvm
was installed properly. Do do that let’s check the version of nvm
installed.
nvm --version
If that returns a message like nvm: command not found
, you will need to run the following command to let the current session know about the newly installed changes.
source ~/.profile
Now check the version again. If you get the version back, you should be good to go!
Install a version of Node.js
Now that nvm
is installed, we can install an individual version of Node.js. To see the available list of Node.js versions, you can run this command.
nvm ls-remote
Running this command will list all the available versions from oldest to newest. It will also point out useful information such as the LTS versions for each major release and the currently installed version if any, which is highlighted green with an arrow pointing to it.
Once you have decided which version you wish to install, you can run this command to install it.
nvm install <version-string>
Verify Node.js installation
Now, your version of Node.js should be installed! You can verify by running this command.
node -v
Switching Node.js versions
If you have multiple versions of Node.js installed via nvm
, you can switch between them using this command.
nvm use <version-string>
Conclusion
Following any of these three methods, you should be able to get Node.js running on your server and ready for some coding! If you chose to install via nvm
, which is the method I would recommend due to its flexibility, please be sure to check out the links I provided above to resources that will help you learn more about the functionality it provides and can help with troubleshooting any problems that may arise.
Thank you so much for the read!
Top comments (0)