web log free

How To Install Node Js For Ubuntu


How To Install Node Js For Ubuntu

So there I was, staring at my terminal like it was a foreign language test I hadn’t studied for. I’d just typed sudo apt install nodejs and waited—proudly, I might add—for the magic to happen. But when I ran node -v, it spat back a version so old it might as well have been a relic from the dial-up era. That’s when I realized: installing Node.js on Ubuntu is like ordering a gourmet burger—you can get the fast-food version, but you really want the hand-crafted one.

The Fast-Food Trap: Why apt Isn’t Your Friend

Ubuntu’s default repositories are like that friend who still uses a flip phone: reliable, but way behind the times. When you run sudo apt install nodejs, you’re getting a version that’s often years old. And not in a “vintage” way—more like a “this won’t run modern npm packages” way.

I learned this the hard way when my shiny new React app refused to build. Turns out, Node.js 8.x (yes, that’s what apt gave me) is basically the equivalent of trying to run a 4K video on a 2005 laptop. So, please, don’t make my mistake. Let’s do this properly.

Method 1: The NodeSource Way (My Go-To)

NodeSource is like the cool aunt who always brings the latest gadgets. They maintain a PPA (Personal Package Archive) that gives you the most recent stable Node.js releases. First, open your terminal—you know, that black box where you type spells?

Run this incantation: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -. Replace 20.x with whichever major version you want (like 18.x or 22.x if you’re feeling adventurous). This downloads the setup script and adds the repository to your system.

After it finishes—and trust me, it takes a few seconds—run sudo apt-get install -y nodejs. That -y flag just says “yes” to all prompts, because who has time for that? Now type node -v and bask in the glory of a modern version. You’re welcome.

A Quick Side Note on npm

Node.js comes with npm (Node Package Manager) pre-installed when you use NodeSource. But here’s the irony: npm is a JavaScript package manager written in JavaScript. It’s like a snake eating its own tail. And it updates faster than Node itself, so you might want to upgrade it separately: sudo npm install -g npm@latest. Your future self will thank you.

How to Install Node.js on Ubuntu Using the Package Manager
How to Install Node.js on Ubuntu Using the Package Manager

Method 2: NVM—The Swiss Army Knife for Developers

What if you need to juggle multiple Node.js versions? Maybe your work project runs on version 14, but your side project is on version 20. Enter NVM (Node Version Manager). It’s like having a time machine for your runtime environment. Seriously, you can switch between versions faster than you can say “dependency hell.”

First, install NVM by running: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash. This script adds a few lines to your .bashrc or .zshrc file—don’t panic, it’s just loading NVM every time you open a terminal. Then either restart your terminal or run source ~/.bashrc (or source ~/.zshrc if you’re a Zsh fanatic).

Now you can install any Node version with nvm install 20 and switch to it with nvm use 20. Want to test your code on an older version? nvm install 14 and boom—you’re in 2019 again. How cool is that? The only downside? You’ll become that person who writes “works on my machine” in every pull request.

Method 3: The Binary Tarball (For the Control Freaks)

If you’re the type who refuses to use any package manager and prefers to get your hands dirty, you can download the pre-compiled binary directly from nodejs.org. This is not for the faint of heart—but sometimes you just want total control, right?

How To Install Node.js on Ubuntu Linux - YouTube
How To Install Node.js on Ubuntu Linux - YouTube

Head to the Node.js downloads page, grab the Linux x64 tarball (like node-v20.11.0-linux-x64.tar.xz), and extract it with tar -xf. Then move it to /usr/local/ and add it to your PATH. Honestly, I do this only when I’m feeling masochistic—or when a colleague challenges me.

But hey, knowing how to do it manually means you understand what’s really happening under the hood. Plus, you get bragging rights at the next developer meetup. Just don’t forget to update the symlinks when you upgrade—or you’ll be debugging for hours.

A Final Cautionary Tale

After I learned my lesson with the apt disaster, I installed Node.js using NodeSource and felt like a coding god. Then I tried to install a package called “left-pad” and broke my entire project. True story. The point is: Node.js is just the beginning. Once you have it, you’ll dive into a world of npm packages, each with its own quirks. But that’s a story for another blog post.

So go ahead—pick a method, install Node.js, and start building something awesome. And if your terminal ever fights back, remember: you can always sudo rm -rf / and start over. (Please don’t do that—I’m joking. That’s the quickest way to break Ubuntu and your heart.)

You may also like