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.
System running in Ubuntu 24.04
Root access or user with sudo
privileges
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
Add the NodeSource repository for Node.js:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
Replace
setup_20.x
with the desired version. Different version can be found on nodesource.com.
Use the following command to install Node.js after adding the NodeSource repository:
sudo apt install nodejs -y
Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command.
node -v
npm version
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
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
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 file package.json
will result from this.
npm init
nodemon
and express
. The package-lock.json
file and the node_modules
folder will be created as a result.npm i express nodemon
git init
command. This will include the file .gitignore
.git init
Readme.md
that will have all of the project's information.touch Readme.md
.env
extension that will hold the project's credentials and sensitive data.touch process.env
app.js
or index.js
.touch app.js
Public
(which contains resources and static files) and src
(which contains controllers, models, routes, and views).mkdir Public src
ls -lrt
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.
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 npm
globally (i.e., using sudo
), users get permission-related issues that prevent them from finishing the installation process.
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.
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
.
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.