Skip to content

Instantly share code, notes, and snippets.

@abhilash1in
Created April 24, 2018 17:06
Show Gist options
  • Save abhilash1in/638d928036c4a89421339f3ec2384129 to your computer and use it in GitHub Desktop.
Save abhilash1in/638d928036c4a89421339f3ec2384129 to your computer and use it in GitHub Desktop.
Install nvm and NodeJS using AWS EC2 user data script
#!/bin/bash
apt-get -y update
cat > /tmp/subscript.sh << EOF
# START UBUNTU USERSPACE
echo "Setting up NodeJS Environment"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.9/install.sh | bash
echo 'export NVM_DIR="/home/ubuntu/.nvm"' >> /home/ubuntu/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ubuntu/.bashrc
# Dot source the files to ensure that variables are available within the current shell
. /home/ubuntu/.nvm/nvm.sh
. /home/ubuntu/.profile
. /home/ubuntu/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm install --lts
nvm ls
EOF
chown ubuntu:ubuntu /tmp/subscript.sh && chmod a+x /tmp/subscript.sh
sleep 1; su - ubuntu -c "/tmp/subscript.sh"
@abhilashshettigar
Copy link

thanks a lot saved much time

@nycjay01
Copy link

nycjay01 commented Sep 8, 2020

This does not work via ec2 user data

@AdieOlami
Copy link

This does not work via ec2 user data

I believe it is because you are running CentosOS and not Ubuntu.

@joseafilho
Copy link

Very top! Thanks.

@artyomliou
Copy link

Notice that the "$NVM_DIR" in below line should be replaced to "$NVM_DIR" like:
echo '[ -s "\$NVM_DIR/nvm.sh" ] && . "\$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ubuntu/.bashrc

@danielerota
Copy link

This doesn't work on ec2 ubuntu 20.
Lines
echo 'export NVM_DIR="/home/ubuntu/.nvm"' >> /home/ubuntu/.bashrc echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ubuntu/.bashrc
return error:
Cannot create /home/ubuntu/.bashrc: Permission denied

@jhanxd123
Copy link

Thank you so much! This works for me in Ubuntu 22.04 through EC2 User Data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment