Deploy ruby 2.0 and rails 4.0 on EC2 with beanstalk - amazon-ec2

I deployed my rails 4 app on EC2 with beanstalk. Since there is no ruby 2.0 and rails 4.0 container available currently, I just installed ruby 2.0 and rails 4.0 on the instance follow with this post: Installing Ruby 2.0 and Rails 4.0.0beta on AWS EC2
But still got the error from passenger:
Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0 (Bundler::RubyVersionMismatch)
I cd into /var/app/current, run
$ ruby -v
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-linux]
Is there anything I missed?
Thanks
Some info updates:
$ which bundle
/usr/local/rvm/gems/ruby-2.0.0-p195/bin/bundle
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.0.3
- RUBY VERSION: 2.0.0 (2013-05-14 patchlevel 195) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-2.0.0-p195
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.0.0-p195/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-2.0.0-p195/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/rvm/gems/ruby-2.0.0-p195
- /usr/local/rvm/gems/ruby-2.0.0-p195#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/

Elastic Beanstalk starts passenger using the script at /etc/init.d/passenger, and I don't think it gets the same environment variables as your bash user. To see what I mean, go to /etc/init.d/passenger, and output the environment variables to a log file as follows:
# /etc/init.d/passenger
. /opt/elasticbeanstalk/support/envvars
env > /var/log/debug-rvm.log
Then, execute
$> sudo service passenger restart
$> cat /var/log/debug-rvm.log
to see all of the environment variables that are available at the point where the passenger processes are started.
The startup script also gives us some clues about how these variables get initialized. In particular, I learnt that you can edit /opt/elasticbeanstalk/support/envvars.d/appenv to add your own custom environment variables. You might be able to edit the PATH variable and load rvm using this file.
I added an answer to the question you referenced, which might be useful for your purposes.
Alternative
Alternatively, you can symlink /usr/bin/ruby to ruby 2.0, but I think getting that right can be quite tricky, because you are bypassing rvm. Hope this helps.
$> ls -l /usr/bin/ruby
lrwxrwxrwx 1 root root 16 Jun 7 14:57 /usr/bin/ruby -> /usr/bin/ruby1.9

Is your passenger config pointing to ruby 1.8 or ruby 2.0?
Also, do you have the pre-release version of passenger that works with ruby 2.0?
Try running
gem install passenger -pre and passenger-install-apache2-module without sudo and make sure the passenger config lines point to 2.0 and not 1.8 or 1.9

Related

Find where a specific Ruby version is installed

On Mac, I used to have rbenv as Ruby version manager but recently switched to asdf. I want to create a Ruby on Rails 7.0.4 but it uses an outdated version and I'd like to locate it.
$ rails new app
"Rails 7 requires Ruby 2.7.0 or newer.
You're running
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin"
$ which ruby
/Users/kawsay/.asdf/shims/ruby
$ ruby -v
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [arm64-darwin21]
$ irb
irb(main):001:0> RUBY_VERSION
=> "3.0.5"
$ asdf list ruby
2.7.4
*3.0.5
$ brew info ruby
==> ruby: stable 3.2.0 (bottled), HEAD [keg-only]
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 3.2.33
- RUBY VERSION: 3.0.5 (2022-11-24 patchlevel 211) [arm64-darwin21]
- INSTALLATION DIRECTORY: /Users/kawsay/.gem
- USER INSTALLATION DIRECTORY: /Users/kawsay/.gem/ruby/3.0.0
- RUBY EXECUTABLE: /Users/kawsay/.asdf/installs/ruby/3.0.5/bin/ruby
- GIT EXECUTABLE: /usr/bin/git
- EXECUTABLE DIRECTORY: /Users/kawsay/.gem/bin
- SPEC CACHE DIRECTORY: /Users/kawsay/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/kawsay/.asdf/installs/ruby/3.0.5/etc
- RUBYGEMS PLATFORMS:
- ruby
- arm64-darwin-21
- GEM PATHS:
- /Users/kawsay/.gem
- /Users/kawsay/.gem/ruby/3.0.0
- /Users/kawsay/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/kawsay/.asdf/installs/ruby/3.0.5/bin
- /Users/kawsay/.asdf/shims
- /opt/homebrew/opt/asdf/libexec/bin
- /Users/kawsay/.cargo/bin
- /opt/homebrew/opt/openssl#2.8/bin
- /opt/homebrew/opt/python#3.8/bin/python3
- /opt/homebrew/bin
- /opt/homebrew/sbin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
$ which rbenv
rbenv not found
$ which rvm
rbenv not found
How can I find where ruby 2.6.8p205 is installed ?
You have an issue where you have system Ruby, brew Ruby, asdf Ruby, and rbenv Ruby. In short, your system is confused about where to find things.
The first step is to remove brew's Ruby installation:
brew uninstall ruby
The second step is to remove rbenv completely. (or use the official docs)
The third step is to make sure that system Ruby does not have the Rails gem installed. Start a shell without loading your profile so that you can bypass asdf and any other configuration:
env -i bash --norc --noprofile
Then confirm it's pointing at system Ruby: (your version will differ slightly depending on your version of macOS)
$ ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
Then uninstall the Rails gem:
gem uninstall rails
But while you're at it you might want to remove all the gems you installed using system Ruby: (ignore any errors here)
gem uninstall -aIx
Now close that shell session and switch back to your default shell. Make sure that your shell profile is cleared of anything related to rbenv and that you have no GEM_* environment variables being set. (assuming you use ZSH, check every ~/.zsh* file and ~/.gemrc) Then start a new shell session to load the cleaned profiles.
Then make sure asdf has a properly groomed environment:
asdf reshim
Now when you run gem env you should see output like this:
RubyGems Environment:
- RUBYGEMS VERSION: 3.4.1
- RUBY VERSION: 3.2.0 (2022-12-25 patchlevel 0) [arm64-darwin22]
- INSTALLATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
- USER INSTALLATION DIRECTORY: /Users/foo/.gem/ruby/3.2.0
- RUBY EXECUTABLE: /Users/foo/.asdf/installs/ruby/3.2.0/bin/ruby
- GIT EXECUTABLE: /opt/homebrew/bin/git
- EXECUTABLE DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/bin
- SPEC CACHE DIRECTORY: /Users/foo/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/etc
- RUBYGEMS PLATFORMS:
- ruby
- arm64-darwin-22
- GEM PATHS:
- /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
- /Users/foo/.gem/ruby/3.2.0
Note the difference between INSTALLATION DIRECTORY and EXECUTABLE DIRECTORY in this output and yours. You need to have output that looks like this, otherwise that means that asdf is not installed properly and you should restart the installation of asdf from the beginning.
Afterwards, re-run gem install rails and you should be able to complete rails new app successfully.
If not, there is a shortcut to run commands with asdf for any given command:
asdf exec gem install rails
asdf exec rails new app
asdf exec rails server
This should bypass everything else and use asdf exclusively.

Ruby version and project not matching

When I run, bundle I see the following:
Your Ruby version is 2.5.0, but your Gemfile specified
2.4.0.pre.dev
my Gemfile looks like:
source 'https://rubygems.org'
ruby '2.4.0-dev'
gem 'sinatra', :github => 'sinatra/sinatra'
This is my gem env:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.6.12
- RUBY VERSION: 2.5.0 (2017-07-31 patchlevel -1) [x86_64-darwin16]
- INSTALLATION DIRECTORY: /Users/johndoe/.rbenv/versions/2.4.0-dev/lib/ruby/gems/2.5.0
- USER INSTALLATION DIRECTORY: /Users/johndoe/.gem/ruby/2.5.0
- RUBY EXECUTABLE: /Users/johndoe/.rbenv/versions/2.4.0-dev/bin/ruby
- EXECUTABLE DIRECTORY: /Users/johndoe/.rbenv/versions/2.4.0-dev/bin
- SPEC CACHE DIRECTORY: /Users/johndoe/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/johndoe/.rbenv/versions/2.4.0-dev/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-16
- GEM PATHS:
- /Users/johndoe/.rbenv/versions/2.4.0-dev/lib/ruby/gems/2.5.0
- /Users/johndoe/.gem/ruby/2.5.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "-n/usr/local/bin"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/johndoe/.rbenv/versions/2.4.0-dev/bin
- /Users/johndoe/.rbenv/libexec
- /Users/johndoe/.nvm/versions/node/v8.1.4/bin
- /usr/local/sbin
- /Library/Frameworks/Python.framework/Versions/3.4/bin
- /Users/johndoe/.rbenv/shims
- /Users/johndoe/google-cloud-sdk/bin
- /Users/johndoe/Library/Python/2.7/bin
- /Users/johndoe/.rbenv/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
When I type rbenv verions this is what I see:
system
2.0.0-p247
2.3.1
* 2.4.0-dev (set by /Users/johndoe/code/projects/blog-server/.ruby-version)
With Rbenv I am specifying the ruby version I want, and I am matching this in my project's Gemfile. However, whenever I run bundle or bundle exec gem uninstall sinatra I see the above error message about my ruby version and my gemfile ruby version not matching. I also see that my Ruby version is 2.5.0, and I have no idea how that got set. Can someone help me resolve this error?
Your Ruby version is 2.5.0, but your Gemfile specified
2.4.0.pre.dev
In my bash profile, I have:
export PATH=$HOME/.rbenv/bin:$PATH
....
eval "$(rbenv init -)"
In short, I just want the shell that runs my ruby process for my blog project to use ruby version 2.4.0-dev, and I am struggling to see understand what's blocking me and how to resolve it.
UPDATE:
I also noticed something odd. My bundler version continues to point to ruby 2.5.0p-1.
## Environment
Bundler 1.15.3
Rubygems 2.6.12
Ruby 2.5.0p-1 (2017-07-31 revision 59454) [x86_64-darwin16]
Git 2.11.0
Platform x86_64-darwin-16
OpenSSL OpenSSL 1.0.2k 26 Jan 2017
```
## Gemfile
### Gemfile
```ruby
source 'https://rubygems.org'
ruby '2.4.0-dev'
gem 'sinatra', '2.0.0'
```
### Gemfile.lock
```
<No /Users/johndoe/code/projects/blog-server/Gemfile.lock found>
In a project folder run:
rbenv local 2.4.0-dev
It should do the trick. You just need to specify local ruby version for this particular project. And your global version is set to 2.5.0
The best option here, I think, is to install Ruby Version Manager (rvm).
RVM helps every ruby developer by doing exactly what you need; manage ruby version automatically as you need.
Here's the link, hope it helps;
https://rvm.io/
Cheers

Ruby gem installed, but unable to require it

I've installed a number of gems, however when i go to 'require' them i get "no such file to load error":
~/Documents/Projects/Ruby Scripts/Domain » ./whois.rb
/System//Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in 'require': cannot load such file -- whois (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from ./whois.rb:3:in `<main>'
I have installed the gem and it appears to have installed sucessfully:
~ » gem install whois
Fetching: whois-4.0.1.gem (100%)
Successfully installed whois-4.0.1
Parsing documentation for whois-4.0.1
Installing ri documentation for whois-4.0.1
Done installing documentation for whois after 0 seconds
1 gem installed
Running gem list shows the gem installed as a local gem:
*** LOCAL GEMS ***
activesupport (5.1.0)
addressable (2.5.1)
...
whois (4.0.1)
Running gem env shows the gem path that i would expect as i am using rbenv:
RubyGems Environment:
- RUBYGEMS VERSION: 2.6.11
- RUBY VERSION: 2.4.1 (2017-03-22 patchlevel 111) [x86_64-darwin16]
- INSTALLATION DIRECTORY: /Users/perfektion/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0
- USER INSTALLATION DIRECTORY: /Users/perfektion/.gem/ruby/2.4.0
- RUBY EXECUTABLE: /Users/perfektion/.rbenv/versions/2.4.1/bin/ruby
- EXECUTABLE DIRECTORY: /Users/perfektion/.rbenv/versions/2.4.1/bin
- SPEC CACHE DIRECTORY: /Users/perfektion/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/perfektion/.rbenv/versions/2.4.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-16
- GEM PATHS:
- /Users/perfektion/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0
- /Users/perfektion/.gem/ruby/2.4.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/perfektion/.rbenv/versions/2.4.1/bin
- /usr/local/Cellar/rbenv/1.1.0/libexec
- /Users/perfektion/.rbenv/shims
- /Users/perfektion/.rbenv/bin
- /Users/perfektion/.rbenv/shims
- /Users/perfektion/.rbenv/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
Checking the gem path for the gem whois and i can see it located there.
Strangely though irb seems to be able to locate the gem:
~/ » irb
irb(main):001:0> require 'whois'
=> true
irb(main):002:0> whois = Whois::Client.new
=> #<Whois::Client:0x007fbc121074c8 #timeout=10, #settings={}>
I have been troubleshooting this for about 24 hours now and i feel like i am losing my mind. This issue has only become apparent in the past 24-48 hours. Prior to that the scripts i was playing with were working fine.
Potentially Useful Info
Running OSX Sierra 10.12.4
I am using rbenv and ruby-build
Using Ruby 2.4.1 set globally
PATH = /Users/perfektion/.rbenv/shims:/Users/perfektion/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Re-installed rbenv, ruby-build, ruby and removed all gems and re-installed.
I feel like it is something stupid, but can anyone see what i am doing wrong here that would be causing ruby to not be able to find these gems?
Thank you!
Update
Not sure why i didn't pick this up earlier, but in the top of my script i have:
#!/usr/bin/ruby
I am not sure why this worked before and it isn't now.
Thanks for the help everyone :)
Whenever you try to run the file it's looking for the gem in the Ruby 2.0 directory. Whenever you do gem env it's showing 2.4.1 as your ruby version.
Keep in mind that with rbenv you can either set a local, global, or shell level version of ruby to run: https://github.com/rbenv/rbenv#rbenv-local
You can also create a .ruby-version file in a directory, and rbenv will look for that and use that version if it exists. Something is off between which version of ruby you are using to install the gem and which one is being used to run your whois script.
when you type ./whois.rb you call the ruby system.
try use ruby ./whois.rb
It will call the RVM/rbenv loaded ruby system
Because ruby is trying to fetch the library from another path that is not the same as the one you installed the wois gem with rbenv. (What could be some conflict with rvm and rbenv, I do not know.)
Try to create a new folder, set ruby 2.4 locally with local rbenv <version> and then try to make require again.

Running ruby gems on Windows

I am on Windows 7 and want to use http://hospice.io/. This requires Ruby gem librarian so I installed ruby 2.0 using http://rubyinstaller.org/ then downloaded RubyGems 1.8.25 from http://rubyforge.org/frs/?group_id=126 and updated (gem update --system). I installed librarian.
ruby setup.rb
gem install librarian
Now I am in the folder with my Vagrant box and want to do command librarian-chef but I get
'librarian-chef' is not recognized as an internal or external command, operable program or batch file.
I tried it when running ruby devkit/msys with the same result. Any suggestions how to run it please? I would guess there is a problem with PATH but when gem install works why doesn't librarian-chef work too?
gem env:
RubyGems Environment:
- RUBYGEMS VERSION: 2.0.3
- RUBY VERSION: 2.0.0 (2013-02-24 patchlevel 0) [x64-mingw32]
- INSTALLATION DIRECTORY: C:/bin/ruby/lib/ruby/gems/2.0.0
- RUBY EXECUTABLE: C:/bin/ruby/bin/ruby.exe
- EXECUTABLE DIRECTORY: C:/bin/ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x64-mingw32
- GEM PATHS:
- C:/bin/ruby/lib/ruby/gems/2.0.0
- C:/Users/Andrew/.gem/ruby/2.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
Also, might want to add that I started development on a Windows machine back in ruby 1.8.7 and through ruby 1.9.3 and then switched to Ubuntu 12.04. My development time probably cut in half or maybe even a quarter. I don't know if this has changed with Ruby 2+
I encourage you to look at having a dual boot with Ubuntu, the small investment in setup will save you TONS of time in dealing with making windows act like linux and give you the added benefit of setting up your development environment exactly like it will be in production if you will be running something like unicorn/nginx.

Problem running gems in OS X

I'm running Snow Leopard, and installed a custom built Ruby according to the guide here: http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard . My ruby binary lives in usr/local/bin/ruby and my gems are installed in /usr/local/bin/gem . My gem env looks like so:
RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
I think I may have borked the install since all actions taked on gems give the error:
ERROR: While executing gem ... (Errno::EEXIST)
File exists - /usr/local/bin/ruby
How do you edit the environment variables for the gem environment?
And for those of you on OS X and using ruby AND gems, what did you use to get yourself up and running?
I'm thinking of just nuking everything and starting anew.
It looks like your installation of ruby into /usr/local/bin is not the ruby that you're getting, which probably means /usr/local/bin/ is not in your PATH. If you'd like to run that ruby, try editing your .bash_profile to add /usr/local/bin like this:
export PATH=/usr/local/bin:$PATH
Alternately, you can add the path to a file in /etc/paths.d, which is the preferred method for adding paths globally on an OS X system.
I've tried things about three different ways, each with varying results. I have systems with the default install (/usr/bin), hand-built /usr/local/bin and also Mac Ports in /opt/local/bin.
By far, the simplest is to just use the built-in binary, which on 10.6.2 is ruby 1.8.7. In that scheme, gems are installed in /Library/Ruby/Gems. Second simplest is MacPorts (sudo port install ruby), third is the totally from source method you're describing above. Certainly there are good reasons people install from source, but unless you're trying to run ruby 1.9 or another version, you're best off using the built-in ruby 1.8.7.
Does the command "gem env" not report any gem paths? Mine reports a number of gem paths along with teh version of rubygems etc. that yours does not appear to be doing.
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-10
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/steveweet/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
You can edit the environment variables associated with gem by specifying them in your shell startup files, (.bashrc for bash). The relevant variables are all listed in the environment page of the gem web site. You will want to pay particular attention to GEM_HOME and GEM_PATH. You can check the current settings of these by typing echo $GEM_HOME at a shell profile
I would suggest that your best action at this moment in time may be to go back to your default ruby installation as provided with OS/X and then download and install rvm (The Ruby version manager) and then watch Ryan Bates screencast

Resources