9

I want to use Rails with Ruby 2.1.0, but it's using Ruby 1.9.3 (the system's version).

I'm using rbenv to manage my Ruby versions. My steps were something along the lines of:

$ rbenv install 2.1.0
$ rbenv global 2.1.0
$ sudo gem install rails -v 4.0.2
$ rbenv rehash
$ rbenv versions
  system
* 2.1.0 (set by /home/dennis/.rbenv/version)
$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
$ rails new app && cd app
$ rails server

Rails is using is Ruby 1.9.3 (x86_64-linux), according to localhost:3000/rails/info/properties. A log message from rails server gives: INFO ruby 1.9.3 (2012-04-20) [x86_64-linux].

I think Rails is using the system version of Ruby because the versions match.

$ rbenv local system
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ rbenv local --unset  # Unset local Ruby version, go back to 2.1.0

Some things I've tried with no luck:

  • setting the local and shell versions of Ruby to 2.1.0 with rbenv
  • rehash rbenv shims (rbenv says to do this after installing executables)
  • made new Rails projects after switching to 2.1.0 just in case I made the projects while using 1.9.3
  • putting 2.1.0 in a .ruby-version file in the root of my project (suggested by @Agis)
  • restarting terminal session and starting a login bash shell (suggested by @Russel)
  • specifying the desired Ruby version in the project's Gemfile (suggested by @rlecaro2)

FWIW, I'm using Ubuntu 13.10 with the fish shell.

Dennis
  • 56,821
  • 26
  • 143
  • 139
  • 1
    Check with `which rails` and `which ruby` that you're actually using the rbenv shims – Baldrick Jan 15 '14 at 15:45
  • `which rails` => `/usr/local/bin/rails`, `which ruby` => `/home/dennis/.rbenv/shims/ruby` – Dennis Jan 15 '14 at 15:47
  • Did You used RVM for installation? – Abhinay Jan 15 '14 at 15:51
  • @Abhinay No, I don't even have RVM installed. I used [rbenv](https://github.com/sstephenson/rbenv) with [ruby-build](https://github.com/sstephenson/ruby-build) to build & install Ruby 2.1.0. – Dennis Jan 15 '14 at 15:54
  • @Dennis Mate, sorry to say but If you really want to Learn Rails with " no issues with installation ".Simply go with rvm otherwise these things will occur frequently. Even http://stackoverflow.com/users/497756/mpapis has suggested the same..Enjoy !!.If you say then I'll post all the steps below for you here. – Abhinay Jan 15 '14 at 16:00
  • With rvm you can handle more than 2 or 3 versions of ruby at the same time. – Abhinay Jan 15 '14 at 16:03
  • @Dennis On my installation, the rails use is `~/.rbenv/shims/rails`. Check the .rbenv/shims directory is first in PATH `echo $PATH` – Baldrick Jan 15 '14 at 16:04
  • @Baldrick Yes, `~/.rbenv/shims` is the first path in my $PATH. – Dennis Jan 15 '14 at 16:06
  • @Abhinay I'll give RVM a try if I can't resolve this issue with rbenv. – Dennis Jan 15 '14 at 16:08
  • @Dennis So it means `~/.rbenv/shims/rails` is missing. `gem` should also be a shims, and `rbenv rehash` should have created the rails shim after installing the rails gem. Something went wrong, I don't know what. Sorry. – Baldrick Jan 15 '14 at 16:13
  • @Baldrick thanks for the help! That I'm missing a path is definitely progress. I tried `rbenv rehash` again followed by `which -a rails` but it still only shows `/usr/local/bin/rails`. – Dennis Jan 15 '14 at 16:23

3 Answers3

9

Sounds silly, but did you restart terminal session?

Otherwise try and type

 /bin/bash --login
nil
  • 2,238
  • 1
  • 19
  • 28
  • And then rerun ruby -v to check – nil Jan 15 '14 at 15:44
  • Just tried restarting the terminal and created a new Rails project, but still uses the older Ruby version. Trying `/bin/bash --login` also did not work, `ruby -v` gives `ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]` but `rails server` still says `ruby 1.9.3 (2012-04-20) [x86_64-linux`. – Dennis Jan 15 '14 at 15:51
  • So logging into your bash profile did seem to work... If I'm nost mistaken, your previous rails version was 1.9.3 and now is reading 2.1. Unless you have a ruby version specified in your rails project, if you ran 'rails s' with your updated ruby version, it should have started with that version. Is your server detached? Did you run it with -d when you started? Did you ever restart it? – nil Jan 15 '14 at 16:02
  • Correct, the default Ruby version on Ubuntu 13.10 is 1.9.3 and I installed 2.1.0 with rbenv and ruby-build. I didn't specify a specific Ruby version in my Rails project, it that specified somewhere by default? What does it mean for my server to be detached? (I'm running the Rails server locally on my development machine.) I did not run Rails with the `-d` option. I tried restarting the server several times by killing it with `ctrl+c` and then rerunning `rails server`. – Dennis Jan 15 '14 at 16:14
  • gotcha. I should have simplified and asked if you had restarted the rails server. running 'rails s -d' Will run the the server detached. So it will launch and run without being attached to the terminal session basically – nil Jan 15 '14 at 16:20
  • if you do that you will have to find the pid and kill it. So running like 'pgrep -l -f thin' would find a thin server, which is what I typically use. But now I'm getting way off topic. So if I think of anything I shall return – nil Jan 15 '14 at 16:21
  • Thanks for your help so far! Someone suggested that I'm missing a path in my $PATH (see comments below question), so I'm going to look into that some more. – Dennis Jan 15 '14 at 16:26
9

You didn't tell how you installed rbenv, but I think it is per-user installation (which is default). In this case you should install gems without using sudo. When you did sudo gem install rails, it was installed in system ruby, not rbenv's selected one.

Solution - install rails without sudo:

rbenv global 2.1.0
gem install rails
rbenv rehash
Dennis
  • 56,821
  • 26
  • 143
  • 139
ivalkeen
  • 1,411
  • 10
  • 6
  • I installed rbenv by cloning the repo to `~/.rbenv`, prepending `~/.rbenv/bin` to my $PATH, and following the instructions from `rbenv init` which involved adding a line to `~/.config/fish/config.fish` so that rbenv loads automatically. I then cloned the ruby-build repo to `~/.rbenv/plugins/ruby-build` and prepended `~/.rbenv/plugins/ruby-build/bin` to my $PATH. – Dennis Jan 15 '14 at 16:40
  • Yes, this is per-user (default) installation. Try installing rails without `sudo` as I suggested. – ivalkeen Jan 15 '14 at 16:44
  • I installed Rails as suggested (and verified Ruby version first), but Rails is still showing the old version. Previous to installing Rails with `gem install rails`, the command `gem which rails` would return "ERROR: Can't find ruby library file or shared library rails", but now it returns "/home/dennis/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails.rb". Perhaps I first need to uninstall the Rails gem that I installed with sudo, going to try `sudo gem uninstall rails` next. – Dennis Jan 15 '14 at 16:48
  • 1
    Solved! Your solution worked, but I had to run `rbenv rehash` in addition. I didn't have to uninstall Rails like I mentioned in my previous comment. – Dennis Jan 15 '14 at 16:52
  • Also, don't forget to call `rbenv rehash` – ivalkeen Jan 15 '14 at 16:52
  • I was running into pretty much the same problem trying to get jekyll working. The keys were installing jekyll again so that it would get installed in the ~/.rbenv directory and then running rbenv rehash so I could run the jekyll commands. And...the site is running. Woohoo! – Taylor714 Feb 02 '16 at 04:24
4

Try creating a .ruby-version file in the root of your project with the following contents:

2.1.0
Agis
  • 32,639
  • 3
  • 73
  • 81
  • I created `~/app/.ruby-version` with the contents `2.1.0` then did `rails server` but it still shows Ruby 1.9.3. – Dennis Jan 15 '14 at 15:38
  • What if you open irb? Still 19.3? – Agis Jan 15 '14 at 15:40
  • Running irb in my project directory, `RUBY_VERSION` gives `2.1.0`. – Dennis Jan 15 '14 at 15:41
  • The root folder fo your app isn't the app folder. You could also try specifying the ruby version in the gemfile: `ruby "2.1.0"` (restart terminal). – rlecaro2 Jan 15 '14 at 15:43
  • 1
    @rlecaro2 What is the root folder to my Rails app? My home directory? I tried specifying the Ruby version in the app's Gemfile. When I run `rails server` I get the error message "Your Ruby version is 1.9.3, but your Gemfile specified 2.1.0". I assume Rails is using `/usr/bin/ruby` (1.9.3) instead of `~/.rbenv/shims/ruby` (2.1.0). – Dennis Jan 15 '14 at 16:18