AskHandle

AskHandle Blog

Easy Node.js Installation with asdf

January 8, 2025Nina Kimes3 min read

Easy Node.js Installation with asdf

Node.js is a powerful tool that allows you to run JavaScript code outside of a web browser. It's widely used for building back-end services and applications. As developers, we often need to manage different versions of Node.js for different projects. This is where the asdf version manager comes into play. This article will guide you through the simple steps of installing Node.js using asdf.

What is asdf?

asdf is a tool that manages multiple versions of different programming languages and runtimes, including Node.js. It gives developers the flexibility to switch between versions easily, making it essential for anyone working on multiple projects with varying requirements.

One of the advantages of asdf is its ability to manage not only Node.js but also other languages like Ruby, Python, Go, and more. It acts as a universal version manager, and its simplicity is what makes it a popular choice among developers.

Prerequisites

Before getting started, ensure the following:

  1. Install asdf: You need to have asdf installed on your system. This installation typically requires git and curl. You can follow the installation instructions from the asdf GitHub repository.

  2. Dependencies: Make sure you have the dependencies required for building Node.js. On Ubuntu, for instance, you can usually install these by running:

    bash
    1sudo apt-get install -y build-essential libssl-dev

Installing Node.js Using asdf

Once you have asdf set up on your system, installing Node.js is straightforward.

Step 1: Add the Node.js Plugin

First, you need to add the Node.js plugin to asdf. Open your terminal and run the following command:

bash
1asdf plugin-add nodejs

This will enable asdf to manage Node.js installations.

Step 2: Import Node.js Release Keys

After adding the plugin, you need to import the Node.js release keys. These keys ensure the integrity of the Node.js binaries you will install. Run this command in your terminal:

bash
1bash ~/.asdf/plugins/nodejs/bin/import-release-gpg

This command downloads and imports the keys needed for verifying Node.js versions.

Step 3: Install Node.js

Now that everything is set up, you can install Node.js. To see the available versions, use the following command:

bash
1asdf list-all nodejs

You will see a list of all available Node.js versions. Choose a version you want to install. For example, if you want to install version 18.x, execute:

bash
1asdf install nodejs 18.0.0

You can replace 18.0.0 with any version number you prefer.

Step 4: Set the Global Version

To use the newly installed version across all your projects, set it as the global version. This can be done with the following command:

bash
1asdf global nodejs 18.0.0

Replace 18.0.0 with the version you installed.

Step 5: Verify the Installation

To ensure that Node.js was installed correctly, verify the version by running:

bash
1node -v

This command should display the version of Node.js you just installed.

Managing Multiple Versions

One of the key features of asdf is the ability to manage multiple versions. If you’re working on a project that requires a different version of Node.js, you can install it using the same process as before. Additionally, you can set a local version for a particular project using:

bash
1asdf local nodejs 14.0.0

This command will create a .tool-version file in your project directory, ensuring that whenever you switch to that directory, the specified Node.js version is used.