> For the complete documentation index, see [llms.txt](https://docs.ititanhosting.ro/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ititanhosting.ro/docs/english/vps/linux/how-to-install-nodejs-on-ubuntu.md).

# How to install Node.js on Ubuntu

Node.js lets you run JavaScript on the server side. It is the foundation for countless modern tools and applications. Whether you are running a web app, a Discord bot, or an API server, you will probably need Node.js at some point.

This guide covers two ways to install it. Pick the one that fits your needs.

### Method 1: Using the Ubuntu repository (easiest)

This is the quickest way, but the version might be a bit old:

```bash
sudo apt update
sudo apt install nodejs npm -y
```

Check the version:

```bash
node --version
npm --version
```

If the version looks acceptable for what you need, you are done. Move on to building your project.

### Method 2: Using NodeSource (recommended for most people)

NodeSource maintains up-to-date repositories with more current Node.js versions. This is the better choice if you need a recent release.

First, install the NodeSource setup script. Replace `22.x` with the version you want (20.x, 22.x, or 23.x):

```bash
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
```

The script adds the NodeSource repository and updates your package list automatically. Then install:

```bash
sudo apt install nodejs -y
```

That is it. Node.js and npm are both included in this package:

```bash
node --version
npm --version
```

### Method 3: Using NVM (best for developers who switch versions)

NVM (Node Version Manager) lets you install and switch between multiple Node.js versions on the same machine. This is the way to go if you work on several projects that require different Node versions.

Install NVM:

```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
```

Reload your shell:

```bash
source ~/.bashrc
```

Now you can install any Node.js version:

```bash
# Install the latest LTS version
nvm install --lts

# Or a specific version
nvm install 20
nvm install 22

# Switch between versions
nvm use 20
nvm use 22

# Set a default version
nvm alias default 22
```

Check the active version:

```bash
node --version
```

### Verifying npm works

Create a quick test to make sure everything is functional:

```bash
mkdir ~/node-test && cd ~/node-test
npm init -y
npx cowsay "Node.js is working"
```

If you see a cow saying something, installation is complete.

### Installing Yarn (optional)

Some projects use Yarn instead of npm. Install it globally:

```bash
npm install -g yarn
yarn --version
```

### Setting up a simple project

Here is a minimal web server to confirm Node.js can serve traffic:

```bash
mkdir ~/my-app && cd ~/my-app
npm init -y
npm install express
```

Create `index.js`:

```javascript
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('ITITAN Hosting VPS running Node.js!');
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});
```

Run it:

```bash
node index.js
```

Visit `http://your-server-ip:3000` in your browser. You should see the message.

### Managing Node.js processes

For production, you will want something that keeps your app running even if it crashes. PM2 is the most popular choice:

```bash
npm install -g pm2
pm2 start index.js
pm2 save
pm2 startup
```

This keeps your application alive through crashes and restarts.

### Common issues

**"node: command not found" after NVM install.** Log out and back in, or run `source ~/.bashrc`.

**Permission errors with global npm installs.** If you installed Node.js from Ubuntu repos, use `npm install -g` without sudo. If you get EACCES errors, switch to NVM instead.

**Port 3000 is not accessible.** Make sure UFW allows the port:

```bash
sudo ufw allow 3000/tcp
```

### Summary

| Method       | Best for                               |
| ------------ | -------------------------------------- |
| Ubuntu repos | Quick setup, don't need latest version |
| NodeSource   | Stable recent version, one install     |
| NVM          | Switching versions, multiple projects  |
