How to Install Node.js on Ubuntu and Update npm to the Latest Version

Node.js has become an essential tool for full-stack JavaScript development, powering everything from command-line utilities to enterprise-grade web applications and APIs. Its event-driven, non-blocking I/O model and vast ecosystem of packages make it a productive choice for developers working on all parts of the stack.

As of 2023, Node.js is used by over 20 million websites, including many high-traffic sites like Netflix, Uber, and LinkedIn. npm, the default package manager for Node.js, hosts over 1.8 million packages, with billions of downloads per week. Here is a chart showing the explosive growth of npm packages over the past decade:

Year Number of npm packages
2012 15,000
2015 250,000
2018 750,000
2020 1,300,000
2023 1,800,000+

Source: modulecounts.com

To leverage the power and productivity of Node.js, the first step is getting it installed on your development machine. While Ubuntu users can install Node.js from the default apt repositories, the version there is often outdated. At the time of writing, Ubuntu 20.04‘s default repositories only offer Node.js v10.19, which reached end-of-life in April 2021.

For the best development experience, it‘s recommended to install a newer version of Node.js, either the latest long-term support (LTS) release or the cutting-edge current release. There are two main ways to do this on Ubuntu: using the Node Version Manager (nvm) or adding the NodeSource package repository.

Option 1: Install Node.js using the Node Version Manager (nvm)

The Node Version Manager (nvm) is a tool that allows you to install and manage multiple versions of Node.js on the same system. This is incredibly useful for developers who work on a variety of Node.js projects, each with potentially different version requirements.

I‘ve been using nvm for years in my work as a full-stack developer and consultant. It‘s saved me countless hours of headache when juggling different versions for client projects and open source contributions. It‘s also great for testing how a codebase runs on different Node.js releases without having to modify your system-wide installation.

Step 1: Install nvm

To install or update nvm, use the following curl command to download and run the installation script:

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

This will clone the nvm repository to ~/.nvm and add the necessary lines to your shell initialization file (~/.bashrc, ~/.zshrc, etc.) to load nvm whenever you open a new terminal.

nvm installation output

After the installation, close your current terminal and open a new one for the changes to take effect. You can verify nvm is working by running:

nvm --version

You should see the version number output, something like 0.39.3.

Step 2: Install Node.js with nvm

With nvm installed, you can now easily install and use different versions of Node.js. To install the latest LTS version, run:

nvm install --lts

At the time of writing, this installs Node.js v18.16.0 (codenamed "Hydrogen"). You can also specify an exact version to install:

nvm install 16.17.0

nvm will download the specified version, compile it if necessary, and set it as the active version for your current shell session. It also installs a compatible version of npm.

To confirm the installation, check the versions of Node.js and npm:

node --version
npm --version

Checking Node.js and npm versions

You‘ll see the version numbers corresponding to the version you installed.

Step 3: Switch between Node.js versions

One of the key benefits of using nvm is the ability to easily switch between different Node.js versions on a per-shell basis. To see all the versions you have installed, run:

nvm ls

The output will list your installed versions, with the current active version highlighted and marked with an arrow:

$ nvm ls
->     v16.17.0
       v18.16.0
default -> 18.16.0 (-> v18.16.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.16.0) (default)
stable -> 18.16 (-> v18.16.0) (default)
lts/* -> lts/hydrogen (-> v18.16.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.17.0
lts/hydrogen -> v18.16.0

To switch to a different installed version, simply use nvm use:

nvm use 16.17.0

This changes the active Node.js version for the current shell session. You can verify the change with node --version.

To set a default Node.js version that will be used in any new shell, run:

nvm alias default 18.16.0

Replace 18.16.0 with the version you want as the default.

Option 2: Install Node.js via the NodeSource repository

If you prefer to have a single, system-wide installation of Node.js, using the NodeSource package repository is a good alternative to nvm. This method is well-suited for production servers or development machines dedicated to a single Node.js stack.

Step 1: Add the NodeSource repository

First, import the NodeSource package signing key to your system so apt will trust the new repository:

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg

Next, add an apt repository list file for the NodeSource packages. This example adds the repository for Node.js 18.x:

echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/nodesource.list

To install a different major version of Node.js, change 18.x to the desired major version, like 16.x or 14.x. The $(lsb_release -cs) part automatically expands to the codename of your Ubuntu release, like jammy, focal, or bionic.

Step 2: Install Node.js and npm

After adding the NodeSource repository, update your package cache and install Node.js:

sudo apt update
sudo apt install nodejs

This will install the latest Node.js version from the repository you added, along with a compatible npm version. To verify the installation, check the installed versions:

node --version
npm --version

Verifying Node.js and npm versions

You‘re all set! This Node.js installation will be available system-wide and automatically managed by Ubuntu‘s apt package manager.

Updating npm

Whether you used nvm or installed from the NodeSource repository, it‘s a good idea to periodically update npm to the latest version. npm gets updates more frequently than Node.js itself, with bug fixes, performance improvements, and new features.

To upgrade npm, simply run:

npm install -g npm@latest

The -g flag tells npm to install the update globally, so it‘s available system-wide. You can also specify an exact version instead of @latest.

Updating npm

After the update, run npm --version to verify you have the expected npm version.

Additional setup for productive Node.js development

With Node.js and npm ready to go, there are a few more tools I recommend for a well-rounded Node.js development setup on Ubuntu.

Version control with Git

Git is an indispensable tool for managing your code and collaborating with other developers. It integrates with popular code hosting platforms like GitHub, GitLab, and Bitbucket. Install Git on Ubuntu with:

sudo apt install git

Set your name and email address for commit messages with:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Code editor or IDE

A good code editor with JavaScript and Node.js intelligence makes writing code faster and catches errors early. Some excellent open-source, cross-platform options are:

  • Visual Studio Code: A full-featured IDE with exceptional JavaScript and TypeScript support, debugging, and a huge extension ecosystem.
  • Atom: A "hackable" text editor with a clean interface and plenty of customization options.
  • Sublime Text: A sophisticated text editor known for its speed and powerful keyboard shortcuts.

Install your chosen editor and spend some time configuring it for your needs. Look for extensions that add Node.js-specific features like code linting, debugging, and npm integration.

Terminal customization

As a full-stack developer, you‘ll spend a lot of time in the terminal. Customizing your shell with colors, Git status indicators, and handy aliases can boost your productivity. Some popular options are:

  • Oh My Zsh: A framework for managing your Zsh configuration with themes and plugins.
  • Starship: A fast, minimalist prompt for any shell that shows contextual information.
  • Bash-it: A set of Bash scripts to customize your terminal experience, similar to Oh My Zsh.

Check out the installation instructions for your preferred setup and explore the available themes and customization options.

Continuous learning and community involvement

JavaScript and Node.js development are constantly evolving fields. To stay up-to-date and continually improve your skills, I recommend:

Remember, the best way to solidify your Node.js skills is through hands-on practice. Challenge yourself to build projects, experiment with new libraries and tools, and don‘t be afraid to ask for help when you get stuck. The Node.js community is known for being welcoming and supportive of developers of all skill levels.

Conclusion

In this guide, we‘ve covered multiple ways to install Node.js and npm on Ubuntu, from using the flexible Node Version Manager (nvm) to adding the NodeSource package repository for a system-wide installation. You‘ve also learned how to update npm, set up additional tools for Node.js development, and tap into the wealth of learning resources available.

With a properly configured Node.js development environment and a commitment to continuous learning, you‘re well on your way to building great things with JavaScript and Node.js. Whether you‘re creating command-line tools, web applications, or API services, Node.js provides a powerful and flexible foundation.

I hope this guide has been helpful for getting started with Node.js on Ubuntu. Feel free to bookmark it for future reference, and don‘t hesitate to reach out if you have any questions or run into issues. Happy coding!

Similar Posts