How to Install Node.js and NPM on Ubuntu 24.04
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. To learn how to use cloud server on Ubuntu in right way click here.
Prerequisites Copy link
-
System (or a cloud server at affordable price) running in Ubuntu 24.04
-
Root access or user with
sudoprivileges
Installing Node.js and npm from the Default Ubuntu Repository Copy link
-
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-
Node.js is normally available from Ubuntu's default repository. Install it by running the following command:
sudo apt install nodejs npm -y
Installing Node.js and npm via the NodeSource Repository Copy link
-
Add the NodeSource repository for Node.js:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash - 
-
Use the following command to install Node.js after adding the NodeSource repository:
sudo apt install nodejs -yVerifying the Node.js and npm Installation Copy link
Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command.
node -v
npm version
Installing Specific Node.js Versions with NVM Copy link
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.
-
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
-
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- 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"
-
Run the command below so changes will take effect.
source ~/.bashrc
-
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
-
Switch to a specific version of Node.js that is installed, using the command below.
nvm use 16
Managing Node.js Projects Copy link
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.
-
Launch the terminal, navigate to the project creation path, and make a folder named after the project you are creating.
mkdir my_project
-
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 filepackage.jsonwill result from this.
npm init
- Install any required dependencies, such as
nodemonandexpress. Thepackage-lock.jsonfile and thenode_modulesfolder will be created as a result.
npm i express nodemon
- To initialize git in the project, use the
git initcommand. This will include the file.gitignore.
git init
- Make a file called
Readme.mdthat will have all of the project's information.
touch Readme.md
- Make a file with the
.envextension that will hold the project's credentials and sensitive data.
touch process.env
- To launch the program, create a file with the name
app.jsorindex.js.
touch app.js
- Make two folders:
Public(which contains resources and static files) andsrc(which contains controllers, models, routes, and views).
mkdir Public src
- 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
Best Practices for Node JS Project Structure Copy link
-
For production systems, set up logging and monitoring with tools like Datadog or New Relic.
-
Plan routine maintenance activities including performance reviews, security audits, and dependency updates.
-
Put in place a backup plan for important configurations and data.
-
Check for security flaws in your dependencies and code on a regular basis.
Troubleshooting Common Issues Copy link
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:
-
When attempting to install Node.js or
npmglobally (i.e., usingsudo), users get permission-related issues that prevent them from finishing the installation process. -
After installing
nvm, the command is not recognized. The errornvm Command Not Foundwill be encountered. Make sure that the shell's configuration file (.bashrc,.bash_profile,.zshrc, etc.) hasnvmsourced, and then the command source~/.bashrchas been use to reload it. -
The
npmversion is out of date or does not correspond with the Node.js version after installing Node.js. Usenvm install <version>to install a particular Node.js version, which will include the matchingnpmversion, and manually updatenpmby runningnpm install -g npm.
Conclusion Copy link
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.
Frequently Asked Questions (FAQ) Copy link
How do I install Node.js on Ubuntu? Copy link
You have two main options:
-
For Stability (System Default): Run sudo apt update followed by sudo apt install nodejs npm. This installs the version maintained by Ubuntu (usually an LTS release).
-
For Developers (Recommended): Use NVM (Node Version Manager). It allows you to install multiple versions and switch between them easily without sudo.
How to check if Node.js is installed in Ubuntu? Copy link
Open your terminal and run the version command: node -v If installed, it will output the version number (e.g., v18.19.0). To check NPM, run npm -v.
What is npm i and npm install? Copy link
They are the same command. npm i is simply a standard alias (shorthand) for npm install. Both commands read your package.json file and install the listed dependencies into the node_modules folder.
How do I update Node.js on Ubuntu? Copy link
-
If using Apt: Run sudo apt update && sudo apt upgrade.
-
If using NVM: simply install the new version (e.g., nvm install 20) and switch to it (nvm use 20).
Where does Node.js install on Linux? Copy link
-
Apt installation: Binary is located at /usr/bin/node.
-
NVM installation: Binary is located inside your home directory, typically ~/.nvm/versions/node/v[version]/bin/node.
How do I fix "EACCES: permission denied" errors when installing global packages? Copy link
This happens if you installed Node via apt and try to install a global package without sudo.
-
Fix 1 (Best): Use NVM, which manages permissions for you.
-
Fix 2: Change the location of your global npm directory to a folder you own (like ~/.npm-global).