# How to Install Node.js on Linux Ubuntu


Installing Node.js on your Linux Ubuntu system is easier than you might think. Whether you're a developer or just starting with JavaScript, having Node.js set up correctly is essential. In this guide, I’ll walk you through the best ways to install Node.js on Ubuntu, ensuring you get the latest version and a smooth setup.

You’ll learn multiple methods, from using Ubuntu’s default repositories to more advanced options like Node Version Manager (NVM). This way, you can pick the method that fits your needs best. Let’s dive in and get your Node.js environment ready!

## Why Install Node.js on Ubuntu?

Node.js is a powerful JavaScript runtime that lets you run JavaScript code outside the browser. It’s widely used for building web servers, APIs, and real-time applications. Installing Node.js on Ubuntu gives you a stable platform for development and production.

Here’s why you should install Node.js on Ubuntu:

- Ubuntu is a popular Linux distribution, especially for servers and developers.
- Node.js supports many modern web frameworks like Express, Next.js, and more.
- You can run JavaScript on the server, enabling full-stack development.
- Ubuntu’s package management makes installation straightforward.

With Node.js installed, you can start building scalable and fast applications right away.

## Method 1: Installing Node.js Using Ubuntu’s Default Repository

Ubuntu’s default software repository includes Node.js, but it might not have the latest version. This method is simple and works well if you don’t need cutting-edge features.

### Steps to Install Node.js from Ubuntu Repository

1. **Update your package list:**

   ```bash
   sudo apt update
   ```

2. **Install Node.js:**

   ```bash
   sudo apt install nodejs
   ```

3. **Install npm (Node Package Manager):**

   ```bash
   sudo apt install npm
   ```

4. **Verify the installation:**

   ```bash
   node -v
   npm -v
   ```

### What You Get

- Node.js version might be older (e.g., 10.x or 12.x).
- Suitable for simple projects or learning purposes.
- Easy to install and maintain.

If you want the latest Node.js features, consider the next methods.

## Method 2: Installing Node.js Using NodeSource PPA

NodeSource provides an official repository with up-to-date Node.js versions. This method is popular among developers who want newer releases without compiling from source.

### How to Use NodeSource PPA

1. **Update your system:**

   ```bash
   sudo apt update
   sudo apt upgrade
   ```

2. **Download and run the NodeSource setup script:**

   For Node.js 18.x (LTS):

   ```bash
   curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
   ```

3. **Install Node.js:**

   ```bash
   sudo apt install -y nodejs
   ```

4. **Check versions:**

   ```bash
   node -v
   npm -v
   ```

### Benefits of Using NodeSource

- Access to the latest LTS and current Node.js versions.
- Automatically installs npm.
- Easy to update Node.js with system package manager.

This method balances ease and freshness of software.

## Method 3: Installing Node.js Using Node Version Manager (NVM)

If you want to manage multiple Node.js versions on the same machine, NVM is the best tool. It lets you switch between versions easily, which is great for testing or working on different projects.

### Installing NVM and Node.js

1. **Download and install NVM:**

   ```bash
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
   ```

2. **Load NVM into your shell session:**

   ```bash
   export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
   [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
   ```

3. **Install the latest Node.js version:**

   ```bash
   nvm install node
   ```

4. **Set default Node.js version:**

   ```bash
   nvm use node
   nvm alias default node
   ```

5. **Verify installation:**

   ```bash
   node -v
   npm -v
   ```

### Why Choose NVM?

- Manage multiple Node.js versions easily.
- Switch between versions with simple commands.
- No need for sudo permissions.
- Great for developers working on diverse projects.

## How to Verify Your Node.js Installation

After installing Node.js, it’s important to confirm everything works correctly.

### Simple Checks

- Run `node -v` to see the installed Node.js version.
- Run `npm -v` to check the Node Package Manager version.
- Test Node.js by running a simple script:

  ```bash
  node -e "console.log('Node.js is working!')"
  ```

If you see the message printed, your installation is successful.

## Updating Node.js on Ubuntu

Keeping Node.js updated ensures you have the latest features and security patches.

### Updating Node.js Installed via NodeSource or Ubuntu Repository

- Run:

  ```bash
  sudo apt update
  sudo apt upgrade nodejs
  ```

### Updating Node.js Installed via NVM

- List available versions:

  ```bash
  nvm ls-remote
  ```

- Install a new version:

  ```bash
  nvm install <version>
  ```

- Switch to the new version:

  ```bash
  nvm use <version>
  nvm alias default <version>
  ```

## Troubleshooting Common Installation Issues

Sometimes, you might face issues during installation. Here are some common problems and fixes:

- **Node command not found:** Make sure the installation path is added to your system PATH.
- **Permission errors:** Avoid using `sudo` with NVM; use NVM as a regular user.
- **Old Node.js version:** Remove old versions before installing new ones.
- **npm errors:** Clear npm cache with `npm cache clean --force`.

If problems persist, checking official Node.js and Ubuntu forums can help.

## Useful Tips for Using Node.js on Ubuntu

- Use `npm` or `yarn` to manage packages efficiently.
- Keep your Node.js environment isolated using tools like Docker or virtual environments.
- Regularly update your packages to avoid security risks.
- Explore popular frameworks like Express.js to build web apps quickly.

## Conclusion

Installing Node.js on Ubuntu is straightforward with several methods available. You can use Ubuntu’s default repository for simplicity, NodeSource for the latest stable versions, or NVM for managing multiple versions. Each method suits different needs, whether you’re a beginner or a seasoned developer.

Once installed, you can start building powerful JavaScript applications on your Linux system. Remember to verify your installation and keep Node.js updated for the best experience. With this guide, you’re ready to harness the full power of Node.js on Ubuntu!

### FAQs

#### How do I check which version of Node.js is installed on Ubuntu?

Run `node -v` in the terminal. This command shows the current Node.js version installed on your system.

#### Can I install multiple Node.js versions on Ubuntu?

Yes, using Node Version Manager (NVM) allows you to install and switch between multiple Node.js versions easily.

#### Is npm installed automatically with Node.js?

When you install Node.js via NodeSource or NVM, npm is installed automatically. For Ubuntu’s default repository, you may need to install it separately.

#### How do I uninstall Node.js from Ubuntu?

You can remove Node.js using `sudo apt remove nodejs` if installed via apt. For NVM, use `nvm uninstall <version>`.

#### What is the recommended Node.js version for production on Ubuntu?

The Long-Term Support (LTS) version, such as Node.js 18.x, is recommended for production due to stability and support.
