Skip to main content

Installing Node.js on Ubuntu

Node.js is a powerful and efficient JavaScript runtime built on Chrome's V8 JavaScript engine. It enables developers to use JavaScript for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. This revolutionary technology has become a staple in the toolkit of modern web developers, powering applications like Netflix, LinkedIn, and Walmart.

Ubuntu, a popular Linux distribution, provides an excellent environment for Node.js development due to its stability, versatility, and vast package ecosystem.

In this tutorial, we'll walk through the process of installing Node.js on Ubuntu, ensuring you have all the necessary tools to begin building your applications. We'll cover multiple installation methods, how to manage versions, and best practices for maintaining your Node.js environment.

Prerequisites

Before we dive into the installation process, make sure you have:.

  • A fresh installation of Ubuntu (we'll be using version 20.04 LTS for this guide).
  • Access to a user account with sudo privileges.
  • A basic understanding of the terminal and command-line operations.

Updating the System

It's crucial to start with an up-to-date system. Open your terminal and execute the following commands to update your package list and upgrade existing packages:.

sudo apt update
sudo apt upgrade

Installation Methods

Using NodeSource Repository

NodeSource provides a repository that contains the latest versions of Node.js. Here's how to install Node.js from the NodeSource repository:.

  1. Visit the NodeSource GitHub repository to find the installation script for your desired Node.js version and Ubuntu release.

  2. Run the provided script using curl to add the NodeSource repository to your system's software sources list:.

```bash
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
```

Replace `16.x` with the version you wish to install.
  1. Install Node.js and the package manager npm:.
```bash
sudo apt install nodejs
```

Optionally, install `npm` (which is usually included with Node.js) with:.


```bash
sudo apt install npm
```
  1. Verify the installation:.
```bash
node -v
npm -v
```

You should see the installed versions of Node.js and npm output to the terminal.

Using NVM (Node Version Manager)

NVM is a tool that allows you to install and manage multiple Node.js versions. Here's how to install Node.js using NVM:.

  1. Install NVM by running the install script from the project's GitHub page:.
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
```
  1. Close and reopen your terminal, or source your profile file with source ~/.profile (or source ~/.bashrc for Bash users).

  2. Install the latest version of Node.js:.

```bash
nvm install node
```
  1. Set the default Node.js version:.
```bash
nvm alias default node
```
  1. Verify the installation:.
```bash
node -v
npm -v
```

Using the Official OS Repositories

Ubuntu's official repositories may not always have the latest Node.js version, but they provide a straightforward installation method:.

  1. Install Node.js and npm from the official repositories:.
```bash
sudo apt install nodejs npm
```
  1. Verify the installation:.
```bash
node -v
npm -v
```

Using Snap Packages

Snap is a software deployment and package management system developed by Canonical for operating systems that use the Linux kernel.

  1. Install Node.js via Snap:.
```bash
sudo snap install node --classic
```
  1. Verify the installation:.
```bash
node -v
npm -v
```

Managing Node.js Versions with NVM

If you've installed Node.js using NVM, you can easily switch between versions. Here's how:.

  • To list installed Node.js versions:.
```bash
nvm ls
```
  • To switch to a specific version:.
```bash
nvm use v14.17.0
```
  • To set a default Node.js version:.
```bash
nvm alias default v14.17.0
```

Configuring npm

npm, the Node.js package manager, can be configured to suit your needs. Here are some common configurations:.

  • To set the default registry (useful if you're using a private registry):.
```bash
npm config set registry https://registry.npmjs.org/


```
  • To change npm's package download directory:.
```bash
npm config set prefix /path/to/custom/location


```
  • To fix npm permission issues:.
```bash
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
```

Installing Global Packages

To install packages globally with npm, use the -g flag:.

npm install -g package-name

Installing packages globally allows you to use them from the command line anywhere on your system.

Setting Up a Node.js Project

To set up a new Node.js project, follow these steps:.

  1. Create a new directory for your project:.
```bash
mkdir my-node-project
cd my-node-project
```
  1. Initialize a new npm project:.
```bash
npm init -y
```

This command creates a `package.json` file with default values.
  1. Add dependencies to your project:.
```bash
npm install express
```

This installs the `express` package, a popular web framework for Node.js, and adds it to your `package.json` file.

Security Considerations

Security is paramount when developing applications. Here are some tips to keep your Node.js environment secure:.

  • Regularly update Node.js and npm packages to their latest versions.
  • Use npm audit to find and fix security vulnerabilities in your project dependencies.
  • Be cautious when installing packages and review their source and maintainers.

Troubleshooting Common Issues

  • Permission Issues: If you encounter permission errors with npm, ensure you have the correct ownership of npm directories or use sudo for package installations.
  • Outdated Versions: If the installed Node.js version is not the expected one, ensure you're using the correct PPA or NVM version.
  • Network Issues: If you have trouble downloading packages, check your internet connection and proxy settings.

Summmary

You've successfully learned how to install Node.js on Ubuntu using various methods. With Node.js and npm at your disposal, you're now equipped to start building robust JavaScript applications. Remember to keep your Node.js version up to date and to practice good security hygiene with your npm packages.

Further Reading

Appendix: Uninstalling Node.js

To remove Node.js and npm from your system, use the following commands based on your installation method:.

  • Using APT (NodeSource or Official Repositories):.
```bash
sudo apt remove nodejs npm
sudo apt autoremove


```
  • Using Snap:.
```bash
sudo snap remove node


```
  • Using NVM:.
```bash
nvm uninstall node
```

To completely remove NVM, delete the directories it created and remove the sourcing line from your profile file.

By following this tutorial, you should have a fully functional Node.js environment on your Ubuntu system.