Lewati ke konten
Kembali ke Blog

Cara Install Node.js di Ubuntu Linux dengan NVM

· · 2 menit baca

Node.js adalah runtime JavaScript yang essential untuk web development modern. Mari pelajari cara install dengan benar di Ubuntu.

Mengapa Menggunakan NVM

NVM vs Direct Install

NVM advantages:
- Multiple Node versions
- Easy version switching
- No sudo required
- Per-project versions
- Clean uninstall

Prerequisites

# Update system
sudo apt update && sudo apt upgrade -y

sudo apt install curl build-essential

Installing NVM

Download NVM Script

# Download and install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Reload shell

source ~/.bashrc

Verify Installation

# Check NVM version
nvm --version

Output: 0.39.0

Installing Node.js

Install Latest LTS

# Install LTS version (recommended)
nvm install --lts

Verify installation

node --version npm --version

Install Specific Version

# Install specific version
nvm install 20.10.0
nvm install 18.19.0

List installed versions

nvm ls

Switch Between Versions

# Use specific version
nvm use 20.10.0

Set default version

nvm alias default 20.10.0

Per-Project Node Version

Using .nvmrc

# Create .nvmrc file
echo "20.10.0" > .nvmrc

Auto-use when entering directory

nvm use

Auto-Switch Script

# Add to ~/.bashrc
autoload -U add-zsh-hook
load-nvmrc() {
  if [[ -f .nvmrc && -r .nvmrc ]]; then
    nvm use
  fi
}
add-zsh-hook chpwd load-nvmrc

NPM Configuration

Set NPM Defaults

# Set author info
npm config set init-author-name "Your Name"
npm config set init-author-email "[email protected]"
npm config set init-license "MIT"

Global Packages Location

# Check global packages location
npm root -g

List global packages

npm list -g --depth=0

Common Issues

Permission Errors

# Fix npm permissions (if using sudo install)
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH

Command Not Found

# Reload shell configuration
source ~/.bashrc
# or
source ~/.zshrc

Kesimpulan

Install Node.js dengan NVM adalah best practice untuk development. Memungkinkan manage multiple versions dan per-project configuration dengan mudah.

Ditulis oleh

Hendra Wijaya

Tinggalkan Komentar

Email tidak akan ditampilkan.