Skip to Content
DocsDevelopmentNode.js / npm / pnpm

Node.js / npm / pnpm

This article covers how to install the Node.js runtime and use popular package managers on Ubuntu 26.04.

nvm (Node Version Manager) lets you install and switch between multiple Node.js versions. It’s the most flexible approach:

Install nvm

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

Reload your shell configuration:

source ~/.bashrc

Using nvm

# List available LTS versions nvm ls-remote --lts # Install the latest LTS version nvm install --lts # Install a specific version nvm install 24 nvm install 22 # Switch versions nvm use 24 # Set default version nvm alias default 24 # List installed versions nvm ls # Uninstall a version nvm uninstall 20

Option 2: Install via fnm

fnm (Fast Node Manager) is a Rust-based Node.js version manager that’s faster:

# Install fnm curl -fsSL https://fnm.vercel.app/install | bash source ~/.bashrc # Install Node.js fnm install --lts fnm install 24 # Switch versions fnm use 24 fnm default 24 # List installed versions fnm list

Add auto-switching support to ~/.bashrc or ~/.zshrc:

eval "$(fnm env --use-on-cd)"

This will automatically switch versions when entering a directory with a .node-version file.

Option 3: Install via apt

If you only need a single version, you can use the NodeSource official repository:

# Add the NodeSource repository (current LTS Node.js 24 example) curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash - # Install Node.js (includes npm) sudo apt install nodejs -y # Verify node --version npm --version

npm Package Manager

npm is installed alongside Node.js and is the default package manager:

Basic Usage

# Initialize a project npm init -y # Install a dependency npm install express # Install a dev dependency npm install --save-dev typescript # Install a tool globally npm install -g nodemon # Run scripts npm run dev npm start # Update dependencies npm update npm outdated

Configure China Mirror

npm config set registry https://registry.npmmirror.com

Verify the configuration:

npm config get registry

pnpm

pnpm uses hard links and content-addressable storage, saving disk space and providing faster installs:

Install pnpm

# Install via npm npm install -g pnpm # Or standalone installation curl -fsSL https://get.pnpm.io/install.sh | sh -

Using pnpm

# Initialize a project pnpm init # Install dependencies pnpm add express pnpm add -D typescript # Install all dependencies pnpm install # Run scripts pnpm dev pnpm start # Update dependencies pnpm update # Manage global packages pnpm add -g nodemon

Configure Mirror

pnpm config set registry https://registry.npmmirror.com

pnpm Workspaces (Monorepo)

Create pnpm-workspace.yaml in the project root:

packages: - 'packages/*' - 'apps/*'
# Install a dependency in a specific package pnpm --filter @myapp/web add react # Run a command across all packages pnpm -r run build

Yarn

Yarn is another popular package manager:

Install Yarn

# Enable via corepack # Note: as of Node.js 25, corepack is no longer bundled with official distributions and must be installed separately # (Node.js 24 LTS and earlier still bundle corepack, so you can skip the next line) npm install -g corepack corepack enable corepack prepare yarn@stable --activate # Or install via npm npm install -g yarn

Using Yarn

# Initialize a project yarn init -2 # Install dependencies yarn add express yarn add --dev typescript # Install all dependencies yarn install # Run scripts yarn dev

Corepack

Corepack is a Node.js tool for managing package managers, ensuring projects use the specified version of pnpm or yarn. As of Node.js 25 it is no longer bundled with official distributions and must be installed first with npm install -g corepack; Node.js 24 LTS and earlier still bundle it:

# Node.js 25+ needs corepack installed first (skip on 24 LTS and earlier) npm install -g corepack # Enable corepack corepack enable # Specify the package manager in package.json # "packageManager": "pnpm@9.15.0"

This ensures team members automatically use the correct package manager version after cloning the project.

FAQ

Global Install Permission Issues

Node.js installed via nvm or fnm doesn’t require sudo privileges. If installed via apt, you may encounter permission issues:

# Change the npm global install directory mkdir -p ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc

.nvmrc File

Create a .nvmrc file in the project root to specify the Node.js version:

echo "24" > .nvmrc nvm use # Automatically reads .nvmrc

Choosing the right Node.js version manager and package manager can significantly improve your frontend and backend JavaScript development experience.

Last updated on