Installing Node.js on Ubuntu
Login
Node.js is a JavaScript runtime used for server-side programming.
It allows developers to create scalable backend functionality using the JavaScript language they are already familiar with from browser-based web development.
In this guide, we will show you three different ways to install Node.js on an Ubuntu server:
- Installing the
nodejspackage from Ubuntu's default software repository using apt - Installing specific versions of the
nodejspackage using apt and an alternative PPA repository - Installing and managing multiple Node.js versions by installing the nvm (Node Version Manager) tool
For most users, installing with apt from the default repository will be sufficient.
If you need a newer or older (legacy) version, you should choose the PPA repository.
If you are actively developing Node applications and need to change versions frequently, the nvm method is the best choice.
Installing Node.js with Apt from Default Repositories
Ubuntu 20.04 includes a version of Node.js in its default repositories.
This version can be used to provide a consistent experience across different systems.
While this guide was being prepared, the version available in the repositories was 10.19.
This may not be the most current version, but it is a stable version and is more than enough to quickly try out the language or work on simple projects.
You can use apt package manager to get this version.
First update the local package index:
sudo apt update
Then install the Node.js package:
sudo apt install nodejs
To verify that the installation was successful, check the version number by running node:
node -v
Outputv10.19.0
If the package in the repositories meets your needs, that's all you need to do to install Node.js.
In most cases you'll also want to install npm (Node.js package manager).
To do this, you can install the npm package using apt:
sudo apt install npm
In this way, you can install modules and packages that you can use with Node.js.
At this point, Node.js and npm have been successfully installed using apt and Ubuntu's default repositories.
In the next section, we will see the use of an alternative repository to install different versions of Node.js.
Installing Node.js with Apt Using NodeSource PPA
To install a different version of Node.js, you can use a PPA (personal package archive) provided by NodeSource.
These PPAs have more versions of Node.js compared to Ubuntu's official repositories.
While this guide was being prepared, Node.js v16 and v18 versions were available.
First, you need to access the packages by adding the PPA.
Download the installation script for your preferred version using the command curl from your home directory (home directory).
If you want to install a different version, replace 16.x with the version you want:
cd ~curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
For more information about available versions, you can check out NodeSource documentation.
You can review the contents of the downloaded script with nano or another text editor of your choice:
nano /tmp/nodesource_setup.sh
Once you're sure it's OK to run the script, exit the editor.
Then run the script with sudo:
sudo bash /tmp/nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be automatically updated.
You can now install the Node.js package as you did in the previous section:
sudo apt install nodejs
To verify that the new version has been installed, run the command node with the parameter -v:
node -v
Outputv16.19.0
The nodejs package provided by NodeSource includes both the node executable and npm, so you don't need to install npm separately.
At this point, Node.js and npm have been successfully installed using apt and the NodeSource PPA.
In the next section, we will see how to use Node Version Manager (nvm) to install and manage multiple Node.js versions.
Node Installation Using Node Version Manager (nvm)
Another and very flexible way to install Node.js is to use nvm (Node Version Manager).
Thanks to this software, you can simultaneously install and manage multiple and independent Node.js versions and packages for these versions.
To install NVM on your Ubuntu 20.04 machine, visit the project's GitHub page.
Copy the command curl from the README file on the main page. This command will allow you to download the latest version of the installation script.
It's a good idea to review the script before running the command directly from bash.
To do this, you can remove the | bash at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
Review the script file and make sure of the changes it made. When you decide the content is safe, run the command again, but this time add | bash to the end.
The URL you use may vary depending on the most current version of nvm.
For now, the command you can use to download and run the installation script is:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
This command will install the nvm script in the user account.
Before you can use it, you need to reload your .bashrc:
source ~/.bashrc
You can now query which Node versions are available from NVM:
nvm list-remote
Output. . . v18.0.0 v18.1.0 v18.2.0 v18.3.0 v18.4.0 v18.5.0 v18.6.0 v18.7.0 v18.8.0 v18.9.0 v18.9.1 v18.10.0 v18.11.0 v18.12.0 (LTS: Hydrogen) v18.12.1 (LTS: Hydrogen) v18.13.0 (Latest LTS: Hydrogen) v19.0.0 v19.0.1 v19.1.0 v19.2.0 v19.3.0 v19.4.0
The list is quite long.
You can install the relevant version of Node by typing any of the versions you see there.
For example, to install v14.10.0:
nvm install v14.10.0
To see the different versions you have installed, you can run this command:
nvm list
Output-> v14.10.0 v14.21.2default -> v14.10.0iojs -> N/A (default)unstable -> N/A (default)node -> stable (-> v14.21.2) (default)stable -> 14.21 (-> v14.21.2) (default). . .
This output shows the currently active version in the first line (for example -> v14.10.0).
Then some named aliases and the versions these shortcuts point to are listed.
Note: If you also have a Node.js version installed via apt, you may see an entry like system in the list.
Additionally, there are special aliases for different LTS – Long Term Support versions of Node:
Outputlts/* -> lts/hydrogen (-> N/A)lts/argon -> v4.9.1 (-> N/A)lts/boron -> v6.17.1 (-> N/A)lts/carbon -> v8.17.0 (-> N/A)lts/dubnium -> v10.24.1 (-> N/A)lts/erbium -> v12.22.12 (-> N/A)lts/fermium -> v14.21.2lts/gallium -> v16.19.0 (-> N/A)lts/hydrogen -> v18.13.0 (-> N/A)
You can also install the version using these shortcuts.
For example, to install hydrogen, the latest long-term support (LTS) release:
nvm install --lts=hydrogen
OutputDownloading and installing node v18.13.0.... . .Now using node v18.13.0 (npm v8.19.3)
You can use the nvm use command to switch between installed versions:
nvm use v14.10.0
OutputNow using node v14.10.0 (npm v6.14.8)
To verify whether the installation was successful, you can check the version using the same method we used in previous sections:
node -v
v14.10.0
As expected, the correct Node version is now installed on your machine.
It also comes with a compatible npm version.
Node.js Removal
You can uninstall Node.js with apt or nvm, depending on your installation method.
If you installed Node.js from the system repositories, you can uninstall it with the following command:
sudo apt remove nodejs
By default apt remove does not delete local configuration files created after installation.
If you want to completely remove these configuration files, you can use the apt purge command:
sudo apt purge nodejs
To uninstall a Node.js version you installed using nvm, you must first check whether it is the currently active version:
nvm current
If the version you are targeting is not currently active, you can run this command to uninstall it:
nvm uninstall node_version
OutputUninstalled node node_version
This command will uninstall the Node.js version you selected.
If the version you want to remove is the currently active version, you will need to disable nvm before the changes can be applied:
nvm deactivate
You can now uninstall the current version with the uninstall command you used before.
This will completely delete all files belonging to the selected Node.js version:
Practical Usage Scenarios
Installing Node.js for Application Development
Installing Node.js is a critical step when starting a new project.
Node.js allows JavaScript to be run on the server side, which is why it is very popular for web applications.
For example, if you are developing a real-time chat application, you can use Node.js with a framework such as Express.js to handle incoming requests and send responses.
To see this more clearly, let's consider a simple chat app scenario.
The aim here is to set up a server, listen to incoming messages and send them to all connected users.
To do this we can use Node.js and Express.js.
const express = require('express');const app = express();const server = require('http').createServer(app);const io = require('socket.io')(server);app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html');});io.on('connection', (socket) => { console.log('Bir kullanıcı bağlandı'); socket.on('disconnect', () => { console.log('Kullanıcı ayrıldı'); }); socket.on('chat message', (msg) => { io.emit('chat message', msg); // Mesajı tüm kullanıcılara gönder });});server.listen(3000, () => { console.log('Sunucu 3000 portunda dinleniyor');});
This example shows how Node.js can be used to create a server that handles real-time communication between clients.
If you want to install Node.js for application development, you can use the methods mentioned earlier in this guide.
For example, you can install Node.js from the Ubuntu repository with apt or choose nvm to install a specific version.
Sample command to install Node.js using Apt:
sudo apt update && sudo apt install nodejs
Alternatively, you can use nvm to install a specific version of Node.js.
If nvm has not been installed before, install it first, then install the Node.js version you want with this command:
nvm install <version>
Setting Up Backend Projects
Node.js is frequently preferred in backend projects thanks to its event-driven and non-blocking I/O model.
This approach allows managing many simultaneous connections in a lightweight and efficient way.
For example, if you are developing a RESTful API with Node.js:
- Can create server side logic,
- Can perform database operations,
- You can return data to the client.
To facilitate the process when setting up a backend project, you can use frameworks such as Express.js or Koa.js.
These frameworks provide a ready-made structure for you to organize your code and manage HTTP requests/responses.
Managing Multiple Node.js Versions with NVM
Different projects may require different Node.js versions. That's why managing multiple Node.js versions is very important.
NVM (Node Version Manager) is a popular tool that allows you to easily manage multiple Node.js versions on a single machine.
For example, let's say you are working on two different projects:
- One needs Node.js 14.x version,
- The other one requires Node.js 16.x.
Thanks to NVM, you can easily switch between these versions and work without affecting the system's default Node.js version.
To change the version, you can use the following command:
nvm use <version>
Replace <version> with the version number you want to use.
This command will switch the Node.js version to the version you specify, so you can work on the project you want without affecting your other projects.
Common Errors and Debugging
Permission Issues on Global Installations
When you try to install a package globally with npm, you may receive an error if you do not have the necessary permissions.
This is because global packages are installed in system directories, and writing to these directories requires root access.
Why does it happen?
This error occurs when npm tries to write to a directory that requires root access, but the user running the command does not have this permission.
How to fix?
You can use one of the following methods to solve this problem:
-
Run the command with
sudo:sudo npm install -g <paket-adi> -
Give ownership of the npm directory to the current user:
sudo chown -R $USER:$(whoami) /usr/local/{lib/node_modules,bin,share/man} \&& sudo chown -R $USER:$(whoami) ~/.npm
- For global installations, use a package manager such as nvm or yarn that does not require root access.
NVM Not Found After Installation (Profile Not Installed)
Error: If the nvm command cannot be found after NVM installation, it is usually because the NVM script is not added to the shell profile.
Why does it happen?
After installation, NVM is not added to the PATH variable of the system.
In order to use NVM, the script must be sourced in the shell profile (e.g. ~/.bashrc or ~/.zshrc).
How to fix?
Add the following line to your shell profile file:
export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Then restart your terminal or apply the changes by running this command:
source ~/.nvm/nvm.sh
Comparison of Installation Methods
| Method | Description | Pros | Cons | Example Command |
|---|---|---|---|---|
| Apt | Installs Node.js from Ubuntu repository | Easy installation, no additional configuration required | Limited version control, not always the most current version | sudo apt install nodejs |
| NVM | Installs and manages multiple Node.js versions | Easy version migration, multi-version support | Requires additional setup, may be complicated for beginners | nvm install node |
| NodeSource PPA | Installs Node.js from third-party repository | Access to the latest versions, easy installation | May not be as secure as other methods, need to add third party repository | sudo apt update && sudo apt install nodejs |
| Binary | Builds from precompiled binaries | Quick installation, no dependencies required | May not be compatible with all systems, manual update required | sudo apt update && sudo apt install nodejs |
| Source | Installs Node.js from source code | Full control over setup, customizable | Time consuming, requires technical knowledge, manual update required | sudo apt update && sudo apt install nodejs |
Result
There are a few different ways to install Node.js on your Ubuntu server, and each has its own advantages.
Whether you use the package from the default repository, choose nvm or NodeSource PPA; The important thing is to determine the method that best suits the needs of the project.

