Sign In
Sign In

How to Install Node.js and NPM on Ubuntu 24.04

How to Install Node.js and NPM on Ubuntu 24.04
JC Brian Refugia
Technical writer
Node.js
09.09.2024
Reading time: 6 min

The popular JavaScript runtime Node.js enables server-side programming with JavaScript. NPM, a package manager for Node.js projects, helps with dependency management. This guide will show how to install NPM and Node.js on Ubuntu 24.04.

Prerequisites

  • System running in Ubuntu 24.04

  • Root access or user with sudo privileges

Installing Node.js and npm from the Default Ubuntu Repository

  1. Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below: 

sudo apt update && sudo apt upgrade -y
  1. Node.js is normally available from Ubuntu's default repository. Install it by running the following command:

sudo apt install nodejs npm  -y

Image1

Installing Node.js and npm via the NodeSource Repository

  1. Add the NodeSource repository for Node.js: 

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash - 

7b6a7095 40cc 4db7 Acfb 32ea72359f17

Replace setup_20.x with the desired version. Different version can be found on nodesource.com.

  1. Use the following command to install Node.js after adding the NodeSource repository:

sudo apt install nodejs -y

Verifying the Node.js and npm Installation

Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command.

node -v

Image11

npm version

Image17

Installing Specific Node.js Versions with NVM 

With the help of the robust utility Node Version Manager (NVM), devops may easily manage several Node.js versions on a single machine. This is very helpful when switching between several project needs.

  1. To install NVM, download and run the installation script from the NVM repository using the following command:

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

Image13

  1. After running the scripts, source the user profile and add NVM to the shell session. Add the following lines to the user's home directory (~/.bashrc, ~/.zshrc, or the corresponding shell profile script). Create it using nano editor:

nano ~/.bashrc
  1. 3. Add the following content:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Image10

  1. Run the command below so changes will take effect.

source ~/.bashrc

Image8

  1. With NVM installed, install the specific versions of Node.js. In this case, to install Node.js version 16, run the command below:

nvm install 16

Image2

  1. Switch to a specific version of Node.js that is installed, using the command below.

nvm use 16

Image19

Managing Node.js Projects

Several essential procedures and best practices are involved in managing Node.js projects in order to ensure the effectiveness, maintainability, and scalability of the application.

This is a tutorial to help to efficiently manage your Node.js projects.

  1. Launch the terminal, navigate to the project creation path, and make a folder named after the project you are creating.

mkdir my_project

Image20

  1. Initiate the Node project by running the command npm init. Provide the required details (marked in red). All of the answers in this example will be default. The file package.json will result from this.

npm init

Image14

  1. Install any required dependencies, such as nodemon and express. The package-lock.json file and the node_modules folder will be created as a result.
npm i express nodemon

Image5

  1. To initialize git in the project, use the git init command. This will include the file .gitignore.
git init

Image6

  1. Make a file called Readme.md that will have all of the project's information.
touch Readme.md

Image9

  1. Make a file with the .env extension that will hold the project's credentials and sensitive data.
touch process.env

Image7

  1. To launch the program, create a file with the name app.js or index.js.
touch app.js

Image4

  1. Make two folders: Public (which contains resources and static files) and src (which contains controllers, models, routes, and views).
mkdir Public src

Image15

  1. Check each and every folder and file that was generated. This is how a typical structure might look like. For the NODE JS application, it is best practice to establish a project structure, divide files based on their function, and place those files in the appropriate directories. To make it simple to confirm the existence and logic of any given file or folder, unify the application's naming conventions and include business logic in the controllers folder, for instance.
ls -lrt

Image18

Best Practices for Node JS Project Structure

  1. For production systems, set up logging and monitoring with tools like Datadog or New Relic.

  2. Plan routine maintenance activities including performance reviews, security audits, and dependency updates.

  3. Put in place a backup plan for important configurations and data.

  4. Check for security flaws in your dependencies and code on a regular basis.

Troubleshooting Common Issues

There are some frequent problems that a user could run into when installing npm and Node.js. These troubleshooting instructions should help you to address the majority of typical problems that arise when installing npm and Node.js. The steps for troubleshooting these issues are listed below:

  1. When attempting to install Node.js or npm globally (i.e., using sudo), users get permission-related issues that prevent them from finishing the installation process.

  2. After installing nvm, the command is not recognized. The error nvm Command Not Found will be encountered. Make sure that the shell's configuration file (.bashrc, .bash_profile, .zshrc, etc.) has nvm sourced, and then the command source ~/.bashrc has been use to reload it.

  3. The npm version is out of date or does not correspond with the Node.js version after installing Node.js. Use nvm install <version> to install a particular Node.js version, which will include the matching npm version, and manually update npm by running npm install -g npm

Conclusion

In conclusion, an important initial step in creating new web applications and utilizing server-side JavaScript is installing Node.js and npm. Although installing software is usually simple, there are a few frequent problems that can arise, such as permissions conflicts, environment setup problems, or version mismatches. One can successfully overcome these problems by configuring npm to be compatible with your network environment, modifying system settings for global installations, and managing Node.js versions with tools like nvm.

Do not forget to update npm and Node.js frequently to take advantage of the newest features and security updates. It will have a strong base for developing and implementing Node.js-powered, scalable applications with correct setup and troubleshooting.

Node.js
09.09.2024
Reading time: 6 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us