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

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee