Last active
April 11, 2025 12:26
-
-
Save aqkhan/38cde7b914d15ea960deadffe9b27d15 to your computer and use it in GitHub Desktop.
Install NVM, Nodejs, and pm2 on ubuntu.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to install nvm | |
install_nvm() { | |
echo "Installing NVM..." | |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash | |
# Source nvm script to make nvm command available in the current session | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" | |
} | |
# Function to install the latest node LTS | |
install_node_lts() { | |
echo "Installing latest Node.js LTS..." | |
nvm install --lts | |
nvm use --lts | |
} | |
# Function to install pm2 | |
install_pm2() { | |
echo "Installing PM2..." | |
npm install -g pm2 | |
} | |
# Check if nvm is installed | |
if ! command -v nvm &> /dev/null | |
then | |
install_nvm | |
# Source nvm script to make nvm command available in the current session | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" | |
else | |
echo "NVM is already installed." | |
fi | |
# Check if the latest Node.js LTS version is installed | |
NODE_LTS_VERSION=$(nvm ls-remote --lts | tail -1 | awk '{print $1}') | |
if ! nvm ls | grep -q "$NODE_LTS_VERSION" | |
then | |
install_node_lts | |
else | |
echo "The latest Node.js LTS version is already installed." | |
fi | |
# Check if pm2 is installed | |
if ! command -v pm2 &> /dev/null | |
then | |
install_pm2 | |
else | |
echo "PM2 is already installed." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment