How To Install Node Js On Ubuntu

How To Install Node Js On Ubuntu

Reading time1 min
#Node.js#Ubuntu#WebDevelopment#NodeSource#nvm#JavaScript

Mastering Node.js Installation on Ubuntu: A Step-by-Step Guide Beyond Basics

Node.js is pivotal for modern web development, powering everything from startups to large-scale enterprise backend services. Getting Node.js up and running correctly on Ubuntu isn’t just about following a quick fix; it means setting up a stable, maintainable environment that supports your projects smoothly for the long haul.

While many tutorials rush through the installation steps, this guide will walk you through the entire process systematically — addressing common pitfalls and best practices so you can avoid headaches down the road.


Why Care About Proper Node.js Installation?

Ubuntu, beloved for its stability and security, is a popular OS choice for developers. But installing Node.js on Ubuntu isn’t always straightforward:

  • Using outdated versions might break your app or cause security risks.
  • Mixing sources/packages can create version conflicts.
  • Ignoring environment setup can make updates or switching versions painful.

This guide ensures you don’t fall into these traps. By mastering this installation approach, you’ll have a robust foundation for any Node.js project.


Step 1: Removing Old Versions of Node.js

Before you start, let's ensure your system is clean from any old or conflicting Node.js versions:

sudo apt-get remove nodejs
sudo apt-get purge nodejs

Also check if there’s any global npm packages or configurations that might interfere:

npm list -g --depth=0

If you see anything suspicious and want a completely fresh start, consider cleaning npm’s cache:

npm cache clean --force

Step 2: Choosing the Right Installation Method

You have multiple ways to install Node.js on Ubuntu:

  1. Ubuntu’s default repositories — Fast and easy but often outdated.
  2. NodeSource official PPA (Recommended) — Reliable with up-to-date stable versions.
  3. Using nvm (Node Version Manager) — Ideal if you need multiple Node.js versions across projects.

For production servers where stability and consistent versioning matter, I recommend using NodeSource PPA. For personal/development use or switching versions frequently, use nvm.


Step 3: Installing Node.js Using NodeSource PPA

Let’s install the latest LTS (Long Term Support) version — as of writing, it’s Node 18.x.

  1. Update your package index:

    sudo apt-get update
    
  2. Download and run the NodeSource setup script:

    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    
  3. Install Node.js:

    sudo apt-get install -y nodejs
    
  4. Verify installation:

    node -v
    npm -v
    

You should see something like:

v18.15.0
9.5.1

This means you now have both node binary and npm package manager installed.


Step 4: Optional but Recommended – Build Tools

Some npm packages need additional build tools (like node-gyp) to compile native code modules.

Install these using:

sudo apt-get install -y build-essential python3

This step helps prevent strange errors when installing certain npm packages down the road.


Step 5: Installing Multiple Versions with nvm (Alternate Method)

If you are working on multiple projects requiring different Node.js versions, nvm is your friend.

  1. Download and run nvm install script:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
    
  2. Activate nvm by loading it into your shell session:

    If you're using bash:

    source ~/.bashrc
    

    Or if you use zsh:

    source ~/.zshrc
    
  3. Install a specific version of Node.js (e.g., latest LTS):

    nvm install --lts
    
  4. Set default node version per your preferences:

    nvm alias default lts/*
    
  5. Confirm node version:

    node -v
    

Advantages of nvm: easily switch between versions with commands like nvm use 16, without system-wide conflicts.


Troubleshooting Common Pitfalls

  • ‘node’ command not found after installation?

    Sometimes the binary is installed as nodejs instead of node. Create a symlink if needed:

    sudo ln -s /usr/bin/nodejs /usr/bin/node
    
  • Permission errors when installing global npm packages?

    Avoid using sudo with npm to prevent permission issues—configure npm to use a local directory for global installs (npm docs on fixing permissions) or use tools like Volta.

  • Version mismatch despite installing latest release?

    Clear your shell cache or restart terminal sessions to refresh path variables.


Verifying Your Setup With a Quick Test App

Let’s create a simple test script to ensure everything works fine:

  1. Create app.js file with this content:
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello from Node.js on Ubuntu!\n');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});
  1. Run the app:
node app.js
  1. Open http://localhost:3000/ in your browser—you should see “Hello from Node.js on Ubuntu!”.

This confirms your environment is ready for development!


Conclusion

Mastering the correct way to install Node.js on Ubuntu goes beyond hitting “apt install”. Using official PPAs or version managers like nvm sets a solid foundation tailored to your needs—whether production deployment or flexible local development.

A well-configured setup saves time, prevents frustrating bugs related to mismatched versions or permissions, and ultimately lets you focus on building awesome web applications instead of wrestling config issues.

Happy coding!


Feel free to drop questions below if you hit any bumps during setup!