Skip to content

Instantly share code, notes, and snippets.

@rbcmgs
Last active May 7, 2024 14:59
Show Gist options
  • Select an option

  • Save rbcmgs/179aaaac0f8acdbc1e1643082bca15fc to your computer and use it in GitHub Desktop.

Select an option

Save rbcmgs/179aaaac0f8acdbc1e1643082bca15fc to your computer and use it in GitHub Desktop.
A Bash script to automate the installation of Node.js and Node Version Manager (NVM) on an Ubuntu machine. This script simplifies the setup process for Node.js development environments.
#!/bin/bash
# Script to install NVM (Node Version Manager), Node.js, and NPM on Ubuntu
echo "Starting installation of NVM, Node.js, and NPM on Ubuntu 22.04..."
# Check that the user is not running as root
if [ "$(id -u)" -eq 0 ]; then
echo "This script should not be run as root or with sudo."
exit 1
fi
# Update system package index
sudo apt update
# Install curl and build-essential packages which are required for NVM and Node.js
sudo apt install -y curl build-essential
# Use GitHub API to get the latest release version of NVM from its repository
NVM_VERSION=$(curl -s https://api.github.com/repos/nvm-sh/nvm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
echo "Fetching the latest version of NVM: $NVM_VERSION"
# Download the NVM installation script from the project's GitHub repository
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/$NVM_VERSION/install.sh | bash
# Source the NVM script to ensure nvm command is available in the current session
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# Restart the terminal or source your shell profile manually to update environment
echo "NVM has been installed. Close and reopen your terminal OR run 'source ~/.bashrc' (or equivalent) to use it."
# Install the latest version of Node.js using NVM
nvm install node
echo "The latest version of Node.js and NPM have been installed."
# Check the installed versions of Node.js and NPM
node_version=$(node -v)
npm_version=$(npm -v)
echo "Node.js version $node_version and NPM version $npm_version installed successfully."
# Instructions for usage:
# 1. Save this script as install_node_nvm_ubuntu.sh
# 2. Make the script executable with: chmod +x install_node_nvm_ubuntu.sh
# 3. Run the script: ./install_node_nvm_ubuntu.sh
# Note:
# - This script should not be run as root/sudo as it may cause permission issues.
# - Ensure that you have administrative privileges on your system for packages installation and that you are connected to the internet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment