require: cannot load such file -- mysql2 - ruby-on-rails

I know there are few questions like this, but non of them seems is solving my problem.
I have a script that I want to run - my_script.rb
I am using ruby-2.0.0-p353 and rubygems 2.2.2
I have installed it via rvm. When switch to use my custom-made gemset , gem env says:
RubyGems Environment:
- RUBYGEMS VERSION: 2.2.2
- RUBY VERSION: 2.0.0 (2013-11-22 patchlevel 353) [x86_64-linux]
I use in my_script.rb:
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'mysql2'
I have installed the gems via bundler and the gem list is saying:
Installing i18n (0.6.9)
Installing multi_json (1.8.2)
Installing activesupport (3.2.8)
Installing builder (3.0.4)
Installing activemodel (3.2.8)
Installing activerecord (3.2.8)
Installing treetop (1.4.15)
Installing mime (0.2.0)
Installing mysql2 (0.3.14)
Using bundler (1.3.5)
Your bundle is complete!
It was installed into ./vendor/bundle
Note: my_script.rb and /vendor/bundle are in the same directory
And now the question:
Then why it is saying:
kernel_require.rb:55:in `require': cannot load such file -- mysql2 (LoadError)
when I try to run my script with ./my_script
UPDATE:
An update to myself :)
If this error occurs, try running a script with bundle exec as this was the primary intention of installing gems in bundle :)
I have been running the script the same way as I have used to on my server, but so I have forgot the most obvious and logical way - to run it with bundle exec ./my_script.rb as this was the primary goal for this ;)

You have installed them via bundlers, so you should let bundler require the gems. Add the following to the top of your script:
require 'bundler'
Bundler.require
Hope it helps.

Related

Rails bundler won't bundle install

I have been fighting this thing for days. I'm on rbenv and I have multiple rubies installed trying to get one stupid decade old app running. When I do "bundle install" I get an error that looks like it's trying to install the current version of rake. Instead of using the version installed with Rails 3....see below
fonso#mybox:~/my-dev$ rails -v
Rails 3.0.0
fonso#mybox:~/my-dev$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
fonso#mybox:~/my-dev$ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.0)
actionpack (3.0.0)
activemodel (3.0.0)
activerecord (3.0.0)
activeresource (3.0.0)
activesupport (3.0.0)
arel (1.0.1)
bigdecimal (1.1.0)
builder (2.1.2)
bundler (1.0.22) <-----------Note bundler is installed
erubis (2.6.6)
i18n (0.4.2)
io-console (0.3)
json (1.5.5)
mail (2.2.20)
mime-types (1.25.1)
minitest (2.5.1)
polyglot (0.3.5)
rack (1.2.8)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.0)
railties (3.0.0)
rake (0.9.2.2) <--------Note rake is installed
rdoc (3.9.5)
thor (0.14.6)
treetop (1.4.15)
tzinfo (0.3.60)
Here is my Gemfile...
source 'http://rubygems.org'
gem 'rails', '3.0.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'will_paginate', '~> 2.3.17'
gem 'soap4r-middleware', '~> 0.8.6'
gem 'soap4r', '~> 1.5.6'
gem 'prawn-labels', '~> 0.11.3.0'
gem 'nokogiri-plist', '~> 0.3.0'
gem 'rails_sql_views', '~> 0.8.0'
# gem 'prototype_legacy_helper', '0.0.0', :git => 'https://github.com/rails/prototype_legacy_helper.git'
so Ruby and rails are installed...BUT when I do "bundle install" on my Rails 3 app I get...
fonso#mybox:~/my-dev$ bundle install
Fetching source index for http://rubygems.org/
Installing rake (13.0.3)
Gem::InstallError: rake requires Ruby version >= 2.2.
An error occured while installing rake (13.0.3), and Bundler cannot continue.
Make sure that `gem install rake -v '13.0.3'` succeeds before bundling.
fonso#mybox:~/my-dev$ bundler --version
rbenv: bundler: command not found
Why does it say bundler not found yet I can run Bundle install?
Why can I not get this rails 3 app working?
Any info will help my sanity. Thank you.
UPDATE:
After following Paul D solution I ran bundle install again...but got this error...
fonso#mybox:~/my-dev$ bundle install
Fetching gem metadata from http://rubygems.org/.............
Fetching gem metadata from http://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "bundler":
In Gemfile:
rails (= 3.0.0) was resolved to 3.0.0, which depends on
bundler (~> 1.0.0)
Current Bundler version:
bundler (1.17.3)
This Gemfile requires a different version of Bundler.
Perhaps you need to update Bundler by running `gem install bundler`?
Could not find gem 'bundler (~> 1.0.0)', which is required by gem 'rails (= 3.0.0)', in any of the sources.
#thefonso, I manage multiple decade old apps running stable on Ruby 1.9.3-p551 daily. Here are a few things to ensure:
Make sure you're running latest release of Ruby 1.9 which is 1.9.3-p551. It needs to be installed with SSL/TLS patches as per below:
# Make sure you have latest OpenSSL
sudo apt-get purge -y libssl-dev
sudo apt-get install -y libssl1.0-dev
# Now install Ruby 1.9.3-p551
rbenv install --force --patch 1.9.3-p551 < <(curl -sSL https://git.io/JOtSv)
rbenv global 1.9.3-p551 # (optional)
rbenv rehash
# Now install Bundler 1.17.3
gem install bundler -v 1.17.3 --no-rdoc --no-ri
# Add require 'openssl' to .../versions/1.9.3-p551/bin/bundle file right under require 'rubygems'
# This command below will do it automatically for you as long as you provide it the correct file path
sed "/^require .*/a require 'openssl'" -i "PATH_TO_RBENV_DIR_HERE/versions/1.9.3-p551/bin/bundle"
# That's it! Run commands below to ensure you have patched Ruby 1.9.3-p551
rbenv rehash
curl -Lks 'https://git.io/rg-ssl' | ruby
Now that's out of the way, you can try installing rake 10.5.0
gem install rake -v 10.5.0 --no-ri --no-rdoc
A Gemfile.lock would avoid this problem. If you had one from the last time you ran this code, it would still have the appropriate versions for Rails 3 and Ruby 1.9. Bundler would follow the Gemfile.lock. Unfortunately back then it was not the practice to commit Gemfile.lock.
Since you don't have a Gemfile.lock you're in dependency hell. Bundler is trying to resolve the dependencies using each gem's own gemspec. It doesn't matter which gems you have installed. Where versions are specified it will use that. Where they are not, it will try to use the latest version. Most gems aren't thinking about Ruby 1.9 any more, or old versions of gems didn't have good dependency specifications, so there's going to be problems.
For example, Rails 3.0.0's rails.gemspec depends on railties 3.0.0. railties 3.0.0's railties.gemspec depends on rake >= 0.8.4. There's no upper bound, so Bundler grabs the latest rake and it doesn't work with Ruby 1.9.
To solve this you will need to add more versioned dependencies to your Gemfile. For example, rake 11 should work with Ruby 1.9. Try adding this to your Gemfile.
gem 'rake', '~> 11'
Or you can be super conservative and specify the exact versions you have installed.
gem 'rake', '0.9.2.2'
Run bundle. This should get you past rake and onto the next dependency problem. Add a version restriction for that and bundle. Continue until there's no more dependency issues. Check in the Gemfile.lock.

Rails gem is absent after Capistrano deploy

I'm trying to deploy my application on Ubuntu server. I installed Nginx/Phusion Passenger and deployed my application with Capistrano, it works (I can open URL and look at project). I use RVM and only one version of Ruby I installed before (ruby 2.0.0p643). But when I go to my project folder and try to execute 'rails c production' I get the following error:
The program 'rails' can be found in the following packages:
* ruby-railties-3.2
* ruby-railties-4.0
Try: sudo apt-get install <selected package>
I don't understand what does mean this error - I uploaded my Rails app, it works, I see rendered HTML page, but this error tells me something other :). Also, if I execute 'gem list' I get the following list:
*** LOCAL GEMS ***
bigdecimal (1.2.0)
bundler (1.9.9)
bundler-unload (1.0.2)
executable-hooks (1.3.2)
gem-wrappers (1.2.7)
io-console (0.4.2)
json (1.7.7)
minitest (4.3.2)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
rubygems-bundler (1.4.4)
rvm (1.11.3.9)
test-unit (2.0.0.0)
I don't see 'rails' in this list, but as I said before my application works!!! Please, tell me, what I did wrong. Thanks in advance!
You need to use,
bundle exec rails c production
This is because your are using rvm which bundles the gems inside namespace (inside /vendor on production) which won't be available outside the bundle context.
Note:
Gems can be installed outside the bundler context with Capistrano (http://paulgoscicki.com/archives/2011/10/tell-bundler-to-install-gems-globally-when-using-capistrano/)

To what value PATH variable must be set for using Rspec with Ruby on Rails?

I am having problem with setting PATH variable in Ruby on Rails. Constraints are: I have to use Ruby 1.8.7 and Rails 2.2.2 and Rubygems 1.3.5. These are the outputs of commands:
hemant#hemant-laptop:~$ which gem
/usr/bin/gem
hemant#hemant-laptop:~$ which ruby
/usr/local/bin/ruby
hemant#hemant-laptop:~$ which rails
/var/lib/gems/1.8/bin/rails
hemant#hemant-laptop:~$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /var/lib/gems/1.8/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /var/lib/gems/1.8
- /home/hemant/.gem/ruby/1.8
I have added these lines into my .bashrc file:
export PATH=$PATH:/var/lib/gems/1.8/bin
export PATH=$PATH:/usr/bin/gem
When I echo PATH variable:
hemant#hemant-laptop:~$ echo $PATH
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:bin:/usr/games:/var/lib/gems/1.8/bin:/usr/bin/gem
Real problem is that when I run script/server in my rails app directory, It errors:
./script/../config/../vendor/rails/railties/lib/rails/vendor_gem_source_index.rb:1:in 'require': no such file to load -- rubygems (LoadError)
When I googled it, I got to know that it is due to flaw in PATH variable configuration, but I can't understand what value I should give to it. The above two export statements are just hit and trials but they didn't solve the problem.
Can someone please guide me what value PATH must be set to and how one can deduce the value from outputs of various commands.
[EDIT]
Running gem list gives this:
actionmailer (2.2.2)
actionpack (2.2.2)
activerecord (2.2.2)
activeresource (2.2.2)
activesupport (2.2.2)
chronic (0.10.2)
diff-lcs (1.2.4)
image_science (1.2.1)
libv8 (3.16.14.3)
mysql (2.9.1)
rack (1.5.2)
rails (2.2.2)
rake (0.8.7)
ref (1.0.5)
rspec (1.2.9)
rspec-core (2.14.6)
rspec-expectations (2.14.0)
rspec-mocks (2.14.4)
rspec-rails (1.2.9)
rubygems-update (2.1.10)
therubyracer (0.12.0)
ZenTest (4.3.0)
To extend your path with new paths which contain executables you need to specify the directory. For the gem command you now pointing to the executable itself.
Try this:
export PATH=$PATH:/usr/bin:/usr/local/bin:/var/lib/gems/1.8/bin
This should include all executables in your path. Make sure to reload your .bashrc before trying again.
source ~/.bashrc
One other solution to solve your problems:
Spin-up a virtual machine which is old enough to have the old stuff. Probably ubuntu 12.04 is old enough.

Rails messed up cant' find gems

I don't know what's going on with my installation of ruby / rails under OSX Mountain Lion.
I start with a black rails application, run bundle and listing the installed bundles with bundle show I get this:
[..]
* sqlite3 (1.3.7)
* thor (0.17.0)
* tilt (1.3.3)
* treetop (1.4.12)
[..]
As soon as I run rake db:migrate I get the following error:
Could not find thor-0.17.0 in any of the sources
Run `bundle install` to install missing gems.
But, as shown, thor-0.17 is installed. If I force it:
sudo gem install --version 0.17 thor
Successfully installed thor-0.17.0
1 gem installed
Installing ri documentation for thor-0.17.0...
Installing RDoc documentation for thor-0.17.0...
And run the migration again, I keep having dependency errors (in this case "Could not find formtastic-2.2.1 in any of the sources") for gems that bundle says are already installed. Any help? Thanks
EDIT
Gemfile: http://pastebin.com/WSz5dLrm
Gemfile.lock: http://pastebin.com/2kuFz1kK
So after using your Gemfile I got the following error :
Bundler could not find compatible versions for gem "railties": In
Gemfile:
rails (= 3.2.8) ruby depends on
railties (= 3.2.8) ruby
sass-rails (~> 3.2.3) ruby depends on
railties (3.2.11)
Bundler could not find compatible versions for gem "actionpack": In
snapshot (Gemfile.lock):
actionpack (3.2.11)
In Gemfile:
meta_search (>= 1.1.0.pre) ruby depends on
actionpack (~> 3.1.0.alpha) ruby
Running bundle update will rebuild your snapshot from scratch, using
only the gems in your Gemfile, which may resolve the conflict.
Using rails with the version 3.2.11 fixed the bundle.
Rails 3.2.11 fixes a huge security threat anyway, so update !

Error trying to run rails server

I am trying to get a basic Rails application to run on my Mac OS X 10.6.5. I created a new app called demo (rails new demo), then went into the demo directory and tried to start the app with rails server.
Here is the error message I received:
"/Users/dpetrovi/.gem/ruby/1.8/gems/sqlite3-ruby-1.3.2/lib/sqlite3/sqlite3_native.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
Abort trap"
I checked bundle install in the demo folder:
Using rake (0.8.7)
Using abstract (1.0.0)
Using activesupport (3.0.3)
Using builder (2.1.2)
Using i18n (0.5.0)
Using activemodel (3.0.3)
Using erubis (2.6.6)
Using rack (1.2.1)
Using rack-mount (0.6.13)
Using rack-test (0.5.6)
Using tzinfo (0.3.23)
Using actionpack (3.0.3)
Using mime-types (1.16)
Using polyglot (0.3.1)
Using treetop (1.4.9)
Using mail (2.2.13)
Using actionmailer (3.0.3)
Using arel (2.0.6)
Using activerecord (3.0.3)
Using activeresource (3.0.3)
Using bundler (1.0.7)
Using thor (0.14.6)
Using railties (3.0.3)
Using rails (3.0.3)
Using sqlite3-ruby (1.3.2)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Ruby, RubyGems, and sqlite3 were installed using MacPorts. Then I used gem to try to install the sqlite3-ruby interface. (sudo gem install sqlite3-ruby). Here is where I first noticed something could be off:
Successfully installed sqlite3-ruby-1.3.2
1 gem installed
Installing ri documentation for sqlite3-ruby-1.3.2...
No definition for libversion
Enclosing class/module 'mSqlite3' for class Statement not known
Installing RDoc documentation for sqlite3-ruby-1.3.2...
No definition for libversion
Enclosing class/module 'mSqlite3' for class Statement not known
I had rails running well on my system a few months ago, so I figured maybe I had some duplicates and it was trying to use the wrong one. I ran: for cmd in ruby irb gem rake; do which $cmd; done and got:
/opt/local/bin/ruby
/opt/local/bin/irb
/opt/local/bin/gem
/opt/local/bin/rake
Checking where sqlite3 also gets me: "/opt/local/bin/sqlite3" so they all seem to be in the right place. Obviously /opt/local/bin is in my system path.
If I check gems server, it shows that I have installed sqlite3-ruby 1.3.2 gem. Not sure what the problem could be?
I am using ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]. Macports claims this is the latest (although ive seen 1.9.1)
One more thing-- in irb, I tried to check which version of sqlite3 my sqlite3-ruby is bound to, but I can only get this far:
:irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'sqlite3'
/Users/dpetrovi/.gem/ruby/1.8/gems/sqlite3-ruby-1.3.2/lib/sqlite3/sqlite3_native.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
Abort trap
Any suggestions? Im hoping I overlooked something obvious. Thanks
Have you upgraded to Snow Leopard recently? If so, you need to reinstall MacPorts and all the ports installed with it.
I recommend using RVM instead MacPorts installed ruby.
Definitely use RVM as mentioned above and then try this in your app's directory:
rm -rf .bundle && bundle install
i had the same problem. here is how i resolved it.
removed the gem sqlite3-ruby
did bundle install
reinstalled the gem
recreated the rails app
now webrick works fine.

Resources