A Step-by-Step Guide to Installing Node.js on Ubuntu 24.04 LTS
Need Node.js on Ubuntu 24.04 LTS? Skip the default repos and use NVM instead. It's the smart way to manage multiple Node versions for different projects. Here's how to get it set up right, step by step.
Jamie
LogicCore Digital
So you're setting up a development environment on Ubuntu 24.04 LTS and you need Node.js. You could grab it from Ubuntu's default repositories, but here's the thing - that's usually not the best move. Why? Because you'll likely end up needing different Node.js versions for different projects, and managing that with the system package manager is a pain.
Enter NVM (Node Version Manager). It's the tool that lets you install and switch between multiple Node.js versions effortlessly. Whether you're working on a legacy project that needs Node 18 or a shiny new one that requires the latest LTS, NVM makes it trivial. Plus, it keeps everything isolated from your system packages, which means fewer headaches down the road.
Let's walk through getting Node.js installed on Ubuntu 24.04 LTS the right way.
What You'll Need
Before we dive in, make sure you've got these basics covered:
- Ubuntu 24.04 LTS: Obviously, you need a working Ubuntu 24.04 LTS installation.
- Terminal Access: Open it with
Ctrl+Alt+Tor search for "Terminal" in your applications. - Sudo Privileges: Your user account needs administrator rights to install packages.
- Internet Connection: You'll be downloading NVM and Node.js, so a stable connection is essential.
- Basic Terminal Comfort: If you can navigate directories and run commands, you're good to go.
Step 1: Update Your Package List
Always a good practice - update your system's package list before installing anything new. This ensures you're working with the latest package information and dependencies.
Open your Terminal and run:
sudo apt updateEnter your password when prompted. This refreshes the list of available packages from your configured repositories.
Step 2: Install Build Tools
NVM sometimes needs to compile Node.js from source, especially if you're installing specific versions. To make that process smooth, we need the essential build tools.
Run this command to install build-essential (includes GCC, G++, make), libssl-dev (for SSL support), and curl (for downloading the NVM script):
sudo apt install build-essential libssl-dev curl -yThe -y flag automatically confirms the installation, so you won't be prompted to approve each package.
Step 3: Install NVM
NVM is a bash script that manages multiple Node.js versions. We'll download and run the official installation script directly.
Important: Check the NVM GitHub repository for the latest version number. As of writing, the command looks like this, but the version tag might have changed:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash(Replace v0.39.7 with the latest version tag from the NVM GitHub page if needed.)
This command uses curl to fetch the installation script and pipes it directly to bash for execution. The script installs NVM into ~/.nvm and automatically adds configuration lines to your shell profile (~/.bashrc, ~/.zshrc, etc.) so NVM loads every time you open a terminal.
Step 4: Load NVM in Your Current Session
The installation script modified your shell config, but those changes won't take effect until you reload it or open a new terminal window.
To apply the changes right now in your current session, run:
source ~/.bashrc(Use ~/.zshrc if you're using Zsh instead of Bash.)
Alternatively, just close and reopen your terminal window.
Verify NVM installed correctly:
command -v nvmIf you see nvm printed, you're golden. If nothing shows up or you get an error, double-check that the previous steps completed successfully, especially that the configuration was added to your ~/.bashrc file.
Step 5: Install Node.js with NVM
Now the fun part - actually installing Node.js. NVM makes this straightforward.
For most projects, you'll want the latest LTS (Long-Term Support) version. It's stable, well-tested, and widely supported:
nvm install --ltsNVM will download, compile (if needed), and install the latest LTS release. It also automatically installs the matching npm version, so you get both tools in one go.
Want to see all available versions? Run:
nvm ls-remoteThis spits out a long list of every Node.js version you can install.
If you need a specific version (maybe your project requires Node 20.10.0), install it like this:
nvm install 20.10.0(Replace 20.10.0 with whatever version you need.)
Step 6: Set Your Default Node.js Version
When you install a Node.js version with NVM, it becomes active in your current terminal session. But if you want that version to be the default for every new terminal you open, set an alias.
For the LTS version you just installed:
nvm alias default ltsOr if you installed a specific version and want that as your default:
nvm alias default 20.10.0Now every new terminal session will automatically use this default version. You can still switch versions temporarily in a session using nvm use <version> (e.g., nvm use 18.17.1).
Step 7: Verify Everything Works
Time to confirm Node.js and npm are installed and working correctly.
Check your Node.js version:
node -vYou should see something like v20.11.1 (the exact version depends on what you installed).
Check your npm version:
npm -vThis should show the corresponding npm version, like 10.2.4.
Run a quick test to make sure Node.js executes properly:
node -e "console.log('Hello from Node.js on Ubuntu 24.04!');"If you see "Hello from Node.js on Ubuntu 24.04!" printed, everything is working perfectly.
You're All Set
That's it! You've got Node.js and npm installed on Ubuntu 24.04 LTS using NVM. This setup gives you the flexibility to manage multiple Node.js versions, which is invaluable when you're juggling different projects with different requirements. You're ready to start building.
Helpful Resources
Want to dive deeper? These official resources are worth bookmarking:
- Node.js Official Site: https://nodejs.org/
- npm Documentation: https://www.npmjs.com/
- NVM GitHub Repository: https://github.com/nvm-sh/nvm
- Ubuntu Documentation: https://help.ubuntu.com/