ESC

Search on this blog

Weekly updates

Join our newsletter!

Do not worry we don't spam!

How to Install Node.js on Ubuntu – Linux Installation Guide - 2024


Node.js has been making waves as a robust and versatile backend technology powering everything from lightweight microservices to full-fledged web applications. Its event-driven, non-blocking I/O model along with speedy V8 JavaScript engine allow building highly scalable apps with lower server resource usage.

However, while experienced developers find it straightforward, beginners struggle with properly setting up Node.js development environments - especially on Linux distributions like Ubuntu. Installing specific Node versions, managing global npm packages, and configuring PATH variables accurately involve quite a few steps.

In this extensive Ubuntu Node.js installation guide, we will tackle all those challenges covering the nitty-gritties of getting started with Node development on Linux. Follow along for a smooth experience!
 


Prerequisites


Before diving into installation steps, let’s outline the prerequisites:

- Basic understanding of Linux administration 
- Root or sudo access on the Ubuntu machine  
- curl & git installed (for accessing resources)
- Text editor like Vim or Nano
- Uninterrupted internet connectivity

With that covered, boot up your Ubuntu device and make sure you have sudo privileges. We will use the terminal for most installation commands.

Step 1 - Install Node Version Manager (nvm)
The native apt package manager in Ubuntu offers outdated Node versions lacking long term support. Instead, we will use nvm (Node Version Manager) to set up multiple isolated NodeJS environments.

nvm offers many advantages like:

- Install many Node.js versions in parallel  
- Switch Node versions on the fly
- Use .nvmrc files to manage app version requirements
- Isolate global packages between Node versions

Let’s install nvm using cURL:


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

This fetches the install script and pipes execution to bash. Sourcing ~/.bashrc at the end ensures nvm is loaded properly on new terminal instances.

Verify nvm using:


command -v nvm
# nvm

Step 2 - Install Latest Node.js LTS
Now that NVM is operational, installing the latest Node.js LTS release takes just one command:


nvm install --lts


The --lts flag fetches the latest long term support version deemed production-ready for critical apps.

Check your installed list using:  


nvm ls

 

And verify the active version:



node -v 
# v16.15.0 - Latest LTS


Hurray! We have the latest stable Node.js version installed leveraging nvm isolation capabilities.

Step 3 - Manage npm Packages 
The next piece of the puzzle is streamlining global npm packages between Node versions and projects using nvm.

Install packages like express-generator globally under specific Node versions:


nvm use 16
npm install -g express-generator


This avoids cluttering the root directory or unintended version mismatches.

You can also use the .nvmrc file per project to lock the Node target:


cd my-app/ 
echo "16" > .nvmrc
nvm use


Now any developer simply running nvm use picks the project-specific Node.js automatically!

Step 4 - Set Up Node.js Environment Variables
The last piece is setting up environment variables like PATH correctly for running node executable and npm commands predictably across terminal sessions.

Add the following to your ~/.bashrc or ~/.profile:


export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

This takes care of automatically sourcing nvm when you open a new shell.

Now test loading a version like Node v16 and validating the runtime environment:


nvm use 16
node -e 'console.log(`Node version: ${process.version}`)'
# Node version: 16.15.0

Tip: Consider installing nvm via an installer script for fully autonomous setup without manual intervention.

Step 5 - Configure npm for Lower Privileges 
For improved security, configure npm to use a non-root user instead of sudo privileges which can be dangerous.

When installing global packages, use:



npm config set prefix '~/.npm-packages'

This redirects global packages to ~/.npm-packages instead of default root directories. Update the PATH variable accordingly:


export PATH=~/.npm-packages/bin:$PATH

Now npm can install binaries safely without elevated permissions while retaining global availability!

Conclusion
Phew, that was quite the Node.js installation adventure! Here's what we learned:

- Installed nvm for simplified Node.js version management
- Added multiple LTS versions and isolated environments 
- Global package control using nvm  
- Environment variable configuration 
- Secured npm globals with user prefixes

Following these steps closely guarantees a seamless Node development environment on Ubuntu ready for building amazing JavaScript applications!

The modular approaches using nvm and custom npm prefixes equip you to:

- Switch runtime versions quickly
- Test apps against new Node releases 
- Sustain multiple projects with competing package requirements
- Improve development and infrastructure security  

So there you have it - a comprehensive guide to install and configure Node.js for development on Ubuntu machines. Follow along, share if you found it helpful and let me know if you have any version management questions!

Unleashing the Programmer Within: A Guide to Improving Your Coding Skills
Prev Article
Unleashing the Programmer Within: A Guide to Improving Your Coding Skills
Next Article
Explained for Beginners: How Docker Containers Work
Explained for Beginners: How Docker Containers Work

Related to this topic: