NodeJS not installed successfully in AWS EC2 inside User-data
Asked Answered
T

5

8

I've tried to install NodeJS with nvm in AWS EC2 linux as follow inside user-data:

#!/bin/bash

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
source ~/.bashrc
nvm install 7

After instance is successfully created and I've entered and check inside my ec2 instance, there is no nodejs and nvm installed when I typed like node --version or nvm --version.

[ec2-user@ip-0-0-0-0 ~]$ node --version
-bash: node: command not found
[ec2-user@ip-0-0-0-0 ~]$ nvm --version
-bash: nvm: command not found

and when I've checked in instance's log, found following error message.

[   16.310115] cloud-init[3300]: => Downloading nvm as script to '/.nvm'
[   17.053885] cloud-init[3300]: => Profile not found. Tried  (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
[   17.076402] cloud-init[3300]: => Create one of them and run this script again
[   17.087459] cloud-init[3300]: => Create it (touch ) and run this script again
[   17.092307] cloud-init[3300]: OR
[   17.100669] cloud-init[3300]: => Append the following lines to the correct file yourself:
[   17.117606] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
[   17.124904] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[   17.161419] cloud-init[3300]: => Close and reopen your terminal to start using nvm or run the following to use it now:
[   17.177964] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
[   17.185400] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
Turkic answered 29/1, 2019 at 7:25 Comment(0)
A
12

As explained by the logs, the install.sh script is trying to locate a profile, which it could not found. (remember that the script provided in user-data is run as root, so $HOME is /root.

The solution is to either ensure the profile file will exist before installation, either to manually change the path after the installation, as suggested in the log message.

Solution 1 (untested)

#!/bin/bash

touch ~/.bashrc # this ensure the bashrc file is created
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
source ~/.bashrc
nvm install 7

Solution 2 (tested)

#!/bin/bash

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 7

(when run from user-data, $HOME is /) I tested the above in an interactive session on Amazon Linux.

$ ssh [email protected]
Warning: Permanently added 'ec2-18-202-174-164.eu-west-1.compute.amazonaws.com,18.202.174.164' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
3 package(s) needed for security, out of 3 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-30-44 ~]$ sudo bash
[root@ip-172-31-30-44 ec2-user]# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10250  100 10250    0     0  10250      0  0:00:01 --:--:--  0:00:01 54521
=> Downloading nvm as script to '/root/.nvm'


=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[root@ip-172-31-30-44 ec2-user]#
[root@ip-172-31-30-44 ec2-user]# export NVM_DIR="$HOME/.nvm"
[root@ip-172-31-30-44 ec2-user]# [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[root@ip-172-31-30-44 ec2-user]# nvm install 7
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.10.1 (npm v4.2.0)
Creating default alias: default -> 7 (-> v7.10.1)
[root@ip-172-31-30-44 ec2-user]# node --version
v7.10.1

Note that the above will install nvm, node and npm for the root user. It will not add the correct ENV VAR in ec2-user's environment. To do so, login as ec2-user then either type

export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

or add this to ec2-user's .bashrc

The proof it works (login as ec2-user :

[ec2-user@ip-172-31-20-26 ~]$ export NVM_DIR="/.nvm"
[ec2-user@ip-172-31-20-26 ~]$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ec2-user@ip-172-31-20-26 ~]$ node --version && npm --version
v7.10.1
4.2.0

You can automate that in your user-data script :

cat <<EOF >> /home/ec2-user/.bashrc
export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
EOF
Amelina answered 29/1, 2019 at 8:39 Comment(6)
Found same as above error I've mentioned in ec2 logs. By the way, don't try to install manually because when I've installed manually, everything was working. But, when I've added those command in user-data box, it was not working at all.Turkic
I don't understand the above comment. Are you saying the proposed solution is not working ?Ahmed
I've added your solutions 2 codes inside EC2 user-data input, then it was not working as encountered same error above I've mentioned. But it was working, when I ssh to my instance and run it manually, that's I don't want to do. What I need is I need to install everything inside user-data only.Turkic
Installation works with solution 2 above : i.e node and nvm are installed for root user. If you connect with ec2-user@, node is not in your path, so it is normal that nvm or node returns not found as it was installed for root user. You'll have to add export NVM_DIR="$HOME/.nvm" [root@ip-172-31-30-44 ec2-user]# [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" to /home/ec2-user/.bashrc to have them accessible for ec2-userAhmed
I edited the answer above to show you how to configure your environment for ec2-user@Ahmed
yap, that's I needed to add cat <<EOF >> /home/ec2-user/.bashrc. Thanks much.Turkic
F
2

After spending several hours on an exercise, this worked for me.

#!/bin/bash
touch ~/.bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
source ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install --lts
Fouquiertinville answered 5/10, 2022 at 21:43 Comment(0)
A
1

If we are using amazon linux, try to install the nvm version 16

#!/bin/bash
sudo su
cd ~
amazon-linux-extras install nginx1 -y 
systemctl enable nginx
systemctl start nginx
touch ~/.bashrc
cat > /tmp/startup.sh << EOF
echo "Setting up NodeJS Environment"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bashrc
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bash_profile
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> /home/ec2-user/.bashrc
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bash_profile
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm install 16
nvm ls
EOF
chown ec2-user:ec2-user /tmp/startup.sh && chmod a+x /tmp/startup.sh
sleep 1; su - ec2-user -c "/tmp/startup.sh"
Alderete answered 19/8, 2022 at 9:6 Comment(0)
G
1

What worked for me is to set $HOME to '~', as it is empty when running the script but required by nvm installation.

export HOME=~

And then proceed with installing nvm normally

#!/bin/bash
export HOME=~
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18

The result is a nvm installation in the /root directory with root permissions

[ec2-user@ip-172-31-39-39 ~]$ sudo su
[root@ip-172-31-39-39 ec2-user]# ls -l /root/.nvm
total 152
drwxr-xr-x. 3 root root     32 Dec  8 18:16 alias
-rw-r--r--. 1 root root   2299 Dec  8 18:16 bash_completion
-rwxr-xr-x. 1 root root    371 Dec  8 18:16 nvm-exec
-rw-r--r--. 1 root root 143991 Dec  8 18:16 nvm.sh
drwxr-xr-x. 3 root root     18 Dec  8 18:16 versions
Gladine answered 8/12, 2023 at 18:28 Comment(0)
T
0

I have tried the other solutions that were mentioned in this post, but the only thing that helped me was installing the Node.js using the below commands

#!/bin/bash
curl -sL https://rpm.nodesource.com/setup_16.x | bash -
yum install -y nodejs
Therron answered 14/4, 2023 at 1:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.