How to install Node.js on Alpine Linux

Introduction

In the beginning, we'll talk a little bit about Node.js and Alpine Linux. Node.js is a tool that helps run programs written in JavaScript, and Alpine Linux is a type of operating system that is light and fast. We'll also discuss why it's a good idea to use lightweight systems like Alpine when working with containerized applications. Containerized applications are like small packages that include everything needed to run a program, and Alpine helps make these packages small and efficient.


Getting Alpine Linux Ready

First, we'll get Alpine Linux ready for our work. Imagine Alpine Linux as a toolbox, and the package manager (apk) is like the organizer inside it.

  1. Understanding the Organizer (apk): The package manager (apk) is the one that helps us keep things neat and tidy. It helps us manage the tools we need for our work.
  2. Updating the Toolbox List: apk update: Just like we update our to-do list, we update the list of tools we have in our toolbox. This helps us know about the latest and greatest tools available.
  3. Upgrading Tools: apk upgrade: Sometimes, the tools in our toolbox get better versions. Upgrading is like getting the newest and shiniest tools so our work can be even better.

Installing Node.js on Alpine Linux

  1. Update Package Repository

Before we install Node.js, it's like going to the store and making sure we have the latest list of items available. This step ensures we know about all the new things we can get.

apk update

If you encounter the message [/media/cdrom/apks] as shown in the image above, you need to add the community repository, which contains additional packages.

First let's check our Alpine release version:

cat /etc/alpine-release

This is my current alpine version which is: 3.18.4

Second let's add the community repository, which contains additional packages.

  • Open the /etc/apk/repositories file using a text editor:
vi /etc/apk/repositories

Enter the edit mode by pressing on the i button on your keyboard.

  • Make sure the file contains the following lines (uncomment them if necessary):
http://dl-cdn.alpinelinux.org/alpine/v3.18/main
http://dl-cdn.alpinelinux.org/alpine/v3.18/community

Please notice that I'm using 3.18 because my current version is 3.18.4

  • Save the changes and exit the text editor.

Press the esc then type :wq and hit Enter.

  • Now re-run the update command:
apk update

You should see something like this:

And you're good to go.

  1. Install Node.js and npm

Now, let's actually get Node.js and npm, the tools we need. Think of it like picking up a toolbox for building things on your computer.

If you want to install Node.js LTS version from the main repository:

apk add nodejs npm

This will install Node.js LTS version

If you want to install Node.js Current version from the community repository:

apk add nodejs-current

This will install Node.js Current version

Verifying the Installation

  1. Confirm Node.js Installation

Now that we've brought Node.js home from the store, let's make sure it's there and ready to work.

To check, we use a command that's like asking Node.js, "Are you here?" The command is node -v.

  1. Check npm Version

npm is like another tool we brought home. It helps Node.js do its job better. We want to make sure it's the right version.

To check, it's like asking npm, "What version are you?" The command is npm -v.


Conclusion

So, we've done it! Now you have Node.js on your Alpine Linux, and you're ready to make cool things on your computer.

Remember, Alpine Linux is like a lightweight toolbox, and Node.js is the cool set of tools inside it. By updating, upgrading, and adding the community repository, you've made sure your toolbox is up-to-date and full of possibilities.

Now, whenever you ask Node.js or npm, "Are you here?" and "What version are you?" with node -v and npm -v, they'll happily respond.

Enjoy building awesome stuff with Node.js on your fast and efficient Alpine Linux system! If you have any questions, feel free to ask. Happy coding!