Search code examples
ruby-on-railsruby-on-rails-4rbenv

How do I know if I'm using rbenv


After reading about Ruby gems and having no idea what rbenv or RVM was, I figured I should probably have one of the two.

I tried installing rbenv using Homebrew however it told me I had already installed rbenv. I always seem to have problems adding gems and usually end up using the sudo command to get it to work (which is a horrible idea I assume).

I discovered I had these problems when I was trying to add the braintree API gem and got an error whenever I tried to start the server.

Commands I tried:

gem install "braintree"
bundle install
sudo gem install "braintree"

The error received from running rails server was:

"/config/initializers/braintree.rb:2:in `<top (required)="">': uninitialized constant Braintree::Configuratio (NameError)"

Solution

  • To take it from the top, rbenv and RVM are Ruby version managers. This means that you can have multiple versions of Ruby installed on your computer at once and select which one you would like to use. I have used both and personally like rbenv's approach.

    With that said I think you need to remove the quotes from around the name of the gem you are installing.

    Also, in your Gemfile do you have the braintree gem listed? It should be as simple as gem 'braintree'.

    If you have multiple versions of Ruby installed or even if you just have the system Ruby and a version installed with RVM or rbenv you may be starting your Rails server with the wrong Ruby version (ie it is missing the gem). You can see if it is using rbenv by typing which ruby and it should print something out with .rbenv/ whatever. If not you need to set rbenv as your current ruby. You can do that like rbenv global 2.1.1 where 2.1.1 is the version of ruby you installed with rbenv. If you haven't installed a version with rbenv you can use ruby-build and do rbenv install 2.1.1 or whatever version you want. Then when you launch your Rails server prefixing the command with bundle exec.

    You can set a local ruby-version for your directory by executing rbenv local 2.1.1 once again where 2.1.1 is the version you want.

    Reading the rbenv docs will go a long way.