AWS EC2 Default Ruby Version on Login

457 views Asked by At

so I have my rails project hosted with AWS EC2 using the amazon linux server. by default, ruby -v returns ruby 2.0 but my project was designed on 2.3.3 (latest at the time). in order for me to change the ruby version using rbenv i have to run the following every time i log in via ssh

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ ~/.rbenv/bin/rbenv init
$ source ~/.bash_profile
$ eval "$(rbenv init -)"
$ type rbenv

since i have the global ruby version in rbenv set as 2.3.3 already. then i can run

$ ruby -v
#returns 2.3.3
$ rails s
#or any other rails commands

is there any way I can set it so I don't have to edit the .bash_profile every time?

what I get if I run the above code, then exit the ssh, and ssh back in

$ ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
$ rbenv global
2.3.3
$ rbenv local
2.3.3
$ rails s
-bash: rails: command not found
##### run above code and nothing else #####
$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
$ rails s
# starts rails server without issue
1

There are 1 answers

2
Tim Tonkonogov On

It should work without your ritual after every log in. I believe you don't quite understand what's going on there. Let's start it line by line.

# add init of rbenv on every login to the file .bash_profile to make available rbenv CLI
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
# call rbenv native initializer for first time only (it shows you what else you could do for rbenv)
$ ~/.rbenv/bin/rbenv init
# read and execute .bash_profile (string in echo in the first cmd)
$ source ~/.bash_profile
# as far I remember it isn't used anymore
$ eval "$(rbenv init -)"
# this is just a smoke test to check if rbenv works 
$ type rbenv

Once you completed it, you don't need to do it on the next logins, because it should be run automatically. If this doesn't work you could try to replace .bash_profile to .bashrc in the commands above.

In order to fix the version of ruby use this reference