How to Install and Use Node.js on Arch Linux

How to Install and Use Node.js on Arch Linux

Node.js is a powerful, open-source JavaScript runtime built on Chrome’s V8 engine, enabling developers to run JavaScript on the server side. Whether you’re building a fast REST API, creating real-time chat applications, or managing backend services, Node.js is an essential tool in a modern developer’s toolkit.

Arch Linux, with its bleeding-edge packages and minimalist approach, offers an excellent environment for developers who like full control over their systems. In this guide, we’ll walk you through how to install and use Node.js on Arch Linux, covering multiple installation methods, package management with npm/yarn, and some basic usage.


Why Use Node.js on Arch Linux

There are several reasons why Node.js and Arch Linux are a perfect match:

  • Latest versions: Arch Linux’s rolling release model ensures that you’re always using the most recent stable versions of Node.js and its libraries.
  • Lightweight environment: Arch gives you full control over your system, letting you install only what you need.
  • Powerful tooling: Arch users are generally comfortable with command-line interfaces, making it easy to use Node.js-related tools.
  • AUR ecosystem: The Arch User Repository (AUR) contains community-maintained packages like nvm and yarn.

Checking for Node.js in Official Repositories

Arch Linux’s official repositories often contain the latest version of Node.js. Before installing, you might want to check what’s available:

pacman -Ss nodejs

You’ll likely see something like:

extra/nodejs 20.x.x-1 [installed]
    Evented I/O for V8 javascript

This means you can install Node.js directly using pacman.


Installing Node.js Using pacman

The easiest way to install Node.js on Arch Linux is using the pacman package manager:

sudo pacman -S nodejs npm

This will install both Node.js and npm, the default Node.js package manager.

Verifying the Installation

Once installed, check the versions:

node -v
npm -v

You should see version numbers like:

v20.12.1
10.2.4

This confirms that both Node.js and npm are installed and ready to use.


Installing Node.js Using n (Node Version Manager)

The n tool is a simple Node.js version manager that allows you to install and switch between Node versions easily. While it’s more commonly used on macOS and Ubuntu, it works fine on Arch too.

Step 1: Install prerequisites

sudo pacman -S curl gcc make

Step 2: Install n via npm

sudo npm install -g n

Step 3: Install Node.js using n

sudo n latest

Or to install a specific version:

sudo n 18.17.1

Step 4: Use the installed version

The n tool modifies the symlink for node, making it point to the installed version. You can switch versions like this:

sudo n

It will show a menu of installed versions you can select from.


Installing Node.js Using nvm (Node Version Manager)

nvm is another Node.js version manager that installs versions per user, avoiding the need for sudo.

Step 1: Install nvm from the AUR

Using an AUR helper like yay:

yay -S nvm

Step 2: Add nvm to your shell profile

Add the following to ~/.bashrc, ~/.zshrc, or the relevant shell config:

export NVM_DIR="$HOME/.nvm"
source /usr/share/nvm/init-nvm.sh

Then reload your shell:

source ~/.bashrc

Step 3: Install Node.js with nvm

nvm install node         # Install latest version
nvm install 18.17.1      # Install a specific version
nvm use 18.17.1          # Switch to a specific version

Step 4: Set a default version

nvm alias default 18.17.1

This ensures that version is loaded automatically on shell startup.


Managing Node Packages with npm and yarn

Once Node.js is installed, managing packages is typically done with npm or yarn.

Using npm

To install a global package:

npm install -g nodemon

To install local packages inside a project:

mkdir myapp && cd myapp
npm init -y
npm install express

This creates a package.json file and installs express as a local dependency.

Using yarn

Yarn is an alternative package manager that many developers prefer for its speed and reliability.

Install yarn from the official repo

sudo pacman -S yarn

Or via AUR:

yay -S yarn

Usage example

yarn init -y
yarn add express

Just like npm, this creates a project and installs dependencies.


Creating and Running a Simple Node.js Application

Let’s walk through creating a basic “Hello World” app using Node.js.

Step 1: Create the file

mkdir hello-node
cd hello-node
nano app.js

Paste the following code:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from Node.js on Arch Linux!\n');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Step 2: Run the application

node app.js

Then open your browser and go to http://localhost:3000 to see your app in action.


Keeping Node.js Updated

Using pacman

If you installed via pacman, simply update your system regularly:

sudo pacman -Syu

Using n or nvm

You can update using their built-in commands:

For n:

sudo n latest

For nvm:

nvm install node --reinstall-packages-from=node

This will install the latest version and re-link all your global packages.


Bonus: Running Node.js as a Service

If you’re developing a persistent app, consider using pm2 to manage it as a background service.

npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save

pm2 allows you to monitor, restart, and auto-start apps on boot.


Conclusion

Installing and using Node.js on Arch Linux is straightforward thanks to the multiple methods available, from the official pacman package to version managers like n and nvm. With tools like npm, yarn, and pm2, you have everything you need to start building scalable JavaScript applications directly on your Arch system.

Whether you’re a developer looking for the latest features or someone maintaining long-term projects with specific versions, Arch Linux gives you the flexibility and power you need for effective Node.js development.