Skip to main content

Install, Update, and Remove Node.js on Ubuntu: A Comprehensive Guide

Node.js is a powerful and popular JavaScript runtime that allows you to build server-side applications and run JavaScript code outside of a web browser. If you're an Ubuntu user and want to harness the capabilities of Node.js for your projects, this guide will walk you through the process of installing, updating, and removing Node.js on your Ubuntu system using various methods.

Install, Update, and Remove Node.js on Ubuntu

Install Node.js on Ubuntu

Method 1: Using the Default Ubuntu Repository

The most straightforward way to install Node.js on Ubuntu is to use the default Ubuntu repository. This method provides a stable version of Node.js, but it may not always be the latest version.

  • Open your terminal by pressing Ctrl + Alt + T.
  • Update the package list to ensure you have the latest information about available packages:

sudo apt update
  • Install Node.js and npm (Node Package Manager) with the following command:
sudo apt install nodejs npm
  • Verify the installation by checking the Node.js and npm versions:
node -v
npm -v

Method 2: Using the NodeSource Repository

If you prefer to install the latest version of Node.js or need a specific version, you can use the NodeSource repository, which provides up-to-date packages.

  • First, install the required dependencies for adding a new repository:
sudo apt install curl software-properties-common
  • Add the NodeSource repository for Node.js:
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
  • Add the repository to your system:
echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/nodesource.list
  • Update the package list again:
sudo apt update
  • Install Node.js and npm from the NodeSource repository:
sudo apt install nodejs npm
  • Verify the installation:
node -v
npm -v

Method 3: Using NVM (Node Version Manager)

NVM is a tool that allows you to manage multiple Node.js versions on your system. It's useful when you need to work with different Node.js versions for different projects.

  • Install NVM by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  • Close and reopen your terminal, or run the following command to apply changes without restarting:
source ~/.bashrc
  • To install a specific Node.js version, use NVM. For example, to install Node.js 14, run:
nvm install 14
  • Set the default Node.js version:
nvm use 14
  • Verify the installation:
node -v
npm -v

Update Node.js on Ubuntu

Keeping Node.js up to date is crucial to access the latest features and security updates. Depending on the installation method you chose, here's how you can update Node.js:

Updating from the Default Repository

If you installed Node.js from the default Ubuntu repository, you can update it with the following commands:

sudo apt update
sudo apt upgrade nodejs

Updating from the NodeSource Repository

For Node.js installed from the NodeSource repository, use the following commands:

sudo apt update
sudo apt upgrade nodejs

Updating with NVM

If you used NVM to install Node.js, updating is simple:

  • Update NVM to the latest version:
nvm install --lts
  • Set the newly installed Node.js version as the default:
nvm use --lts
  • Verify the update:
node -v
npm -v

Remove Node.js on Ubuntu

If, for any reason, you need to remove Node.js from your Ubuntu system, you can do so using the following steps:

  • To uninstall Node.js and npm installed from the default Ubuntu repository, run:
sudo apt remove nodejs npm
  • If you installed Node.js from the NodeSource repository, use:
sudo apt remove nodejs npm
  1. If you used NVM to install Node.js, you can uninstall it as follows:

    a. First, deactivate NVM:

    nvm deactivate
    

    b. Then, uninstall NVM:

    rm -rf ~/.nvm
    
  2. Finally, remove any leftover configuration files:

sudo rm -rf /etc/apt/sources.list.d/nodesource.list
sudo rm -f /usr/share/keyrings/nodesource.gpg

Essential Node.js Commands on Ubuntu

Now that you have Node.js installed on your Ubuntu system, let's explore some essential Node.js commands that will help you in your development workflow:

1. Run a JavaScript File

To execute a JavaScript file using Node.js, open your terminal and run:

node filename.js

Replace filename.js with the name of your JavaScript file.

2. Interactive Node.js REPL

Node.js provides an interactive REPL (Read-Eval-Print Loop) environment for experimenting with JavaScript code. Simply type node in your terminal to enter the REPL:

node

You can now enter JavaScript code line by line and see the results immediately.

3. Install Node.js Packages

Node.js uses npm (Node Package Manager) to manage packages. To install a package, use:

npm install package-name

Replace package-name with the name of the package you want to install.

4. Create a New Node.js Project

To create a new Node.js project with a package.json file, navigate to your project folder in the terminal and run:

npm init

Follow the prompts to configure your project.

5. Start a Node.js Server

If you're building a web application, you can start a Node.js server using the http module. Here's a basic example:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js!\n');
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

Save this code to a file, and run it with node filename.js. Your server will be accessible at http://localhost:3000/.

6. Install Global Packages

To install a package globally (available system-wide), use the -g flag:

npm install -g package-name

Global packages are often command

-line tools or utilities that you want to use across different projects.

In this comprehensive guide, we've covered multiple methods for installing Node.js on Ubuntu, updating it to the latest version, and safely removing it from your system if needed. We've also introduced you to some essential Node.js commands that will be valuable in your development journey.

Whether you're a seasoned developer or just getting started with Node.js, having a well-maintained Node.js environment on your Ubuntu machine is essential for building robust and efficient server-side applications. So, go ahead and install Node.js, explore its capabilities, and start creating amazing projects on your Ubuntu system with confidence.

Comments

Popular posts from this blog