How to Install Node.js on Ubuntu 24.04

Node.js is a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to create scalable backend functionality using JavaScript, a language many are familiar with from front-end development. Installing Node.js on your Ubuntu system enables you to develop and run JavaScript applications outside the browser environment. This tutorial will guide you through three methods of installing Node.js on Ubuntu 24.04: using Ubuntu’s package manager (APT), Node Version Manager (NVM), and the official Node.js binary.

In this tutorial you will learn:

  • How to install Node.js using APT
  • How to install Node.js using NVM
  • How to install Node.js using the official Node.js binary
How to Install Node.js on Ubuntu 24.04
How to Install Node.js on Ubuntu 24.04
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 24.04
Software Node.js, NPM, NVM, wget
Other Internet connection
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Using Ubuntu Package Manager (APT)

This method uses the APT package manager to install Node.js from the Ubuntu default repositories. It is straightforward and integrates well with your system’s package management.

  1. Update your package index: Before installing new software, it’s a good practice to update your package index to ensure you get the latest versions available.
    $ sudo apt update

    This command synchronizes your local list of packages with the repository.

  2. Install Node.js: Install Node.js directly using APT.
    $ sudo apt install nodejs

    This command installs Node.js and any required dependencies.

  3. Verify the installation: Check the installed version of Node.js to confirm the installation was successful.
    $ node -v

    This command displays the version of Node.js that was installed, confirming the installation.

  4. Install NPM: npm (Node Package Manager) is not included in the default Node.js package from Ubuntu’s repositories. Install it separately.
    $ sudo apt install npm

    npm is essential for managing Node.js packages and dependencies.

Using Node Version Manager (NVM)

NVM allows you to install and manage multiple versions of Node.js. It’s particularly useful if you need to switch between different versions for various projects.

  1. Install NVM: Download and run the installation script for NVM.
    $ wget -q -O- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
    Download and run the installation script for NVM
    Download and run the installation script for NVM

    This command fetches and executes the install script from NVM’s GitHub repository.

  2. Source your shell: Apply changes to your current shell session.
    $ . ~/.bashrc
    $ nvm --version

    These commands reload your shell configuration and check the NVM version to confirm it was correctly installed.

  3. Install Node.js: Use NVM to install Node.js.
    $ nvm install node
    Use NVM to install Node.js
    Use NVM to install Node.js

    This command installs the latest version of Node.js.

  4. Check Node.js version: Verify the installed Node.js version.
    $ node -v

    This command shows the currently active Node.js version, confirming the installation.



Using the Official Node.js Binary

This method involves downloading the official Node.js binary from the Node.js website. It is useful if you need a specific version of Node.js that is not available in the Ubuntu repositories or via NVM.

  1. Select Node.js version: Visit the Node.js website to select the version you wish to install. Either LTS (Long Term Support) for stability or the current version for the latest features.
    Navigate to https://nodejs.org/ and choose your preferred version. Copy the download link for use in the next step.
  2. Download and extract Node.js: Create a directory for Node.js and extract the downloaded archive into it.
    $ sudo mkdir -p /usr/local/lib/nodejs
    $ wget -O- [Node.js version download link] | sudo tar xJ -C /usr/local/lib/nodejs

    Replace [Node.js version download link] with the actual URL you copied. This command downloads and extracts Node.js to the specified directory.

    Download and extract Node.js
    Download and extract Node.js
  3. Export PATH and source shell: Add Node.js to your system’s PATH and reload your shell configuration.
    $ echo 'export PATH=/usr/local/lib/nodejs/node-v[version]-linux-x64/bin:$PATH' >> ~/.bashrc
    $ . ~/.bashrc

    Replace [version] with the version of Node.js you installed. This step ensures the Node.js and npm executables are found by your shell.

  4. Check Node.js version: Confirm the installation by checking the Node.js version.
    $ node -v

    This command displays the version of Node.js that was installed, verifying the setup.

Conclusion

By following the steps outlined in this tutorial, you should now have Node.js successfully installed on your Ubuntu 24.04 system through one of the three methods: using APT, NVM, or the official Node.js binary. Each method has its advantages, depending on your specific needs and development environment. With Node.js installed, you’re ready to start building your JavaScript applications.

FAQ

1. What is Node.js used for?
Node.js is used for building scalable and efficient network applications. It’s particularly suited for developing server-side applications, real-time web applications, and for scripting and automation tasks.
2. Why choose Ubuntu 24.04 for Node.js development?
Ubuntu 24.04, being a long-term support (LTS) release, offers stability, extensive support, and compatibility, making it an ideal choice for development environments, including Node.js projects.
3. Can I install multiple versions of Node.js on Ubuntu 24.04?
Yes, by using Node Version Manager (NVM), you can install and manage multiple versions of Node.js on the same system, allowing you to switch between versions as needed for different projects.
4. How do I uninstall Node.js from Ubuntu 24.04?
To uninstall Node.js installed via APT, you can use the command `sudo apt remove nodejs`. If installed via NVM, use `nvm uninstall ` to remove a specific version.
5. How can I update Node.js to a newer version?
If you used APT, run `sudo apt update` and then `sudo apt upgrade nodejs`. With NVM, use `nvm install ` to install a new version and switch to it automatically.
6. What are the benefits of using NVM over APT for Node.js installation?
NVM offers more flexibility by allowing you to install multiple Node.js versions and switch between them easily. It’s ideal for testing applications across different Node.js versions.
7. Is Node.js free to use?
Yes, Node.js is open-source software and is free to use. It’s developed and maintained by the Node.js Foundation.
8. How do I set a default Node.js version with NVM?
After installing the desired version with NVM, you can set it as default using `nvm alias default ` command.
9. Can I run Node.js applications built on other operating systems on Ubuntu 24.04?
Yes, Node.js applications are generally cross-platform and can be run on any system where Node.js is installed, including Ubuntu 24.04.
10. How do I troubleshoot installation issues with Node.js on Ubuntu 24.04?
Check for common issues like internet connectivity, disk space, and permissions. Consult the Node.js community forums and the official documentation for specific error messages and troubleshooting tips.
11. How do I install specific versions of Node.js using APT?
APT typically provides the latest stable version. For specific versions, consider using NVM or manually downloading and installing from the Node.js website.
12. What are the hardware requirements for running Node.js on Ubuntu 24.04?
Node.js is lightweight and does not require powerful hardware. A basic setup with at least 1GB of RAM and a 1GHz CPU should suffice for development purposes.
13. How can I ensure my Node.js installation is secure?
Regularly update Node.js and its packages to their latest versions. Use tools like npm audit to identify and fix vulnerabilities within dependencies.
14. Can I use npm without Node.js?
No, npm is a package manager specifically for Node.js, and it requires Node.js to run.
15. How do I manage global vs. local package installations with npm?
Global packages are installed with `npm install -g` and are available system-wide. Local packages are installed in the project directory and are accessible only within that project.
16. How can I contribute to the Node.js project?
You can contribute by reporting bugs, suggesting features, or submitting pull requests to the Node.js GitHub repository. Contributions to the documentation are also welcome.