What is Node.js?
Node.js lets you run JavaScript outside a web browser, for example on a server. It comes with npm, a tool that downloads code libraries for your project.
Method 1: Install from the Ubuntu package list
This is the simplest way, but the version is old.
- Connect to your server with SSH.
- Update the package list:
sudo apt-get update
This refreshes the list of available software.
- Install Node.js and npm:
sudo apt-get install -y nodejs npm
This installs both tools.
- Ubuntu names the program
nodejs. To use the namenode, add a link:
sudo ln -s /usr/bin/nodejs /usr/bin/node
This makes a shortcut so both names work.
Method 2: Install a newer version with NodeSource
NodeSource is a group that offers newer Node.js packages. Version 10 is a safe choice for Ubuntu 16.04.
- Install curl, a tool that downloads files:
sudo apt-get install -y curl
- Add the NodeSource package source:
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
This tells Ubuntu where to find the newer packages.
- Install Node.js:
sudo apt-get install -y nodejs
This installs Node.js and npm together.
Method 3: Use nvm
nvm is a Node Version Manager. It lets you install many versions and switch between them.
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- Close the terminal and open it again.
- Install a version:
nvm install 10
This installs Node.js version 10.
Check the install
node -v
npm -v
These print the version numbers of Node.js and npm.
Try a small test
- Create a file:
echo "console.log('Hello Node');" > hello.js
- Run it:
node hello.js
If you see "Hello Node", all is well. For help with your server, open a support ticket.
Quick recap
- Use apt for the easy, older version.
- Use NodeSource or nvm for a newer version.
- Check with
node -vandnpm -v.