Skip to content

Instantly share code, notes, and snippets.

@kerasai
Created February 27, 2025 13:54
Show Gist options
  • Save kerasai/75a5d1c3051a103742cc4dbe75941e36 to your computer and use it in GitHub Desktop.
Save kerasai/75a5d1c3051a103742cc4dbe75941e36 to your computer and use it in GitHub Desktop.
Install NodeJS using NVM

README

Download, set executable, then run using desired NodeJS version and NVM version as arguments.

chmod +x install-nodejs-nvm.sh
install-nodejs-nvm.sh 22 0.40.1

To add to a .lando.yml file:

services:
  appserver:
    build_as_root:
      - scripts/lando/install-nodejs-nvm.sh
#!/bin/bash
set -e
# Configuration
NODE_VERSION=${1:-22} # Use first argument as Node.js version or default to 22
NVM_VERSION=${2:-0.40.1} # Use second argument as NVM version or default to 0.40.1
echo "==> Installing Node.js ${NODE_VERSION} using NVM ${NVM_VERSION}"
# Define NVM directory in /opt
export NVM_DIR="/opt/nvm"
mkdir -p $NVM_DIR
chmod 755 $NVM_DIR
# Download and run nvm installation script with custom directory
echo "==> Downloading and installing NVM to ${NVM_DIR}..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh | bash
## Set up NVM in current shell
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
## Install and use the specified Node.js version
echo "==> Installing Node.js ${NODE_VERSION}..."
nvm install $NODE_VERSION
nvm use $NODE_VERSION
nvm alias default $NODE_VERSION
# Verify installation
echo "==> Verifying installation..."
node --version
npm --version
echo "==> Installation completed successfully!"
# Create symlinks in /usr/local/bin (which is in the system PATH)
NODE_PATH=$(dirname $(dirname $(which node)))/bin/node
NPM_PATH=$(dirname $(dirname $(which npm)))/bin/npm
NPX_PATH=$(dirname $(dirname $(which node)))/bin/npx
ln -sf $NODE_PATH /usr/local/bin/node
ln -sf $NPM_PATH /usr/local/bin/npm
ln -sf $NPX_PATH /usr/local/bin/npx
echo "==> Symbolic links created:"
ls -la /usr/local/bin/node
ls -la /usr/local/bin/npm
ls -la /usr/local/bin/npx
echo "==> Node.js ${NODE_VERSION} has been successfully installed using NVM ${NVM_VERSION}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment