6

searching for an answer to the question above in Stackoverflow I have always bumped into a similar - but not equal - question: whether it is possible to create a new rails app with an older version than the latest installed on the computer (one of the most popular posts of this kind is Specifying rails version to use when creating a new application). However, what I'm interested in is knowing if it's possible to run a command such as 'rails __2.1.0__ new myapp' even if that specific rails version is not yet installed on my computer so that when it runs it it automatically installs this rails version plus it creates all the starting files (in which the Gemfile contains all the compatible gems of that specific version already).

As an example... Right now I'm following the Rails Tutorial book by Michael Hartl and we are asked to use the ruby version 2.0.0, the rails version 4.0.8 and to include the following info into the Gemfile:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.8'

group :development do
  gem 'sqlite3', '1.3.8'
end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

It happens that I have by default ruby-2.1.2 and rails 4.1.4 so when I have wanted to follow Hartl's book I have had to create a new rails application (which sets up the Gemfile according to rails 4.1.4) and after that I have had to cd into the new app, run $ gem install rails --version 4.0.8 to install the version, substitute the default Gemfile that came with rails 4.1.4 for the above code, then run bundle install and bundle update.

It seems to work, but all in all it is a rather tedious and annoying solution. Couldn't this be solved, like I wrote at the beggining, with a 'rails ____2.1.0____ new myapp' in which version 2.1.0 (that I do not have installed) gets installed at that moment?

I'm sure there has to be an easier way to get started with a different rails version project, I just don't find it or try to solve it with the wrong commands. I'm sure the solution I implemented wasn't good enough either since whenever I try to create another rails app using a version that I have allegedly already installed (2.0.0) this is what I get from the Terminal:

Desktop$ rails _2.0.0_ new myapp
/Users/gaa/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/dependency.rb:313:in `to_specs': Could not find 'railties' (= 2.0.0) - did find: [railties-4.1.4,railties-4.1.1] (Gem::LoadError)
Checked in 'GEM_PATH=/Users/gaa/.rvm/gems/ruby-2.1.2:/Users/gaa/.rvm/gems/ruby-2.1.2@global', execute `gem env` for more information
from /Users/gaa/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/dependency.rb:322:in     `to_spec'
from /Users/gaa/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_gem.rb:53:in `gem'
from /Users/gaa/.rvm/gems/ruby-2.1.2/bin/rails:22:in `<main>'
from /Users/gaa/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
from /Users/gaa/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

I would really appreciate a helping hand.

Community
  • 1
  • 1
assensi
  • 352
  • 1
  • 6
  • 17
  • I would just follow the tutorial using Rails 4.14 - I don't think you should run into any major problems – JKillian Aug 08 '14 at 00:45
  • Also, you can run `gem install rails --version 4.0.8` before running `rails new` and then use the solution you linked to – JKillian Aug 08 '14 at 00:46
  • 1
    @JKillian there will be differences - not major ones, but there will be errors and problems (eg. http://stackoverflow.com/q/23124480/560215 which is for the same tutorial) and its best to use the tutorial with the version of Rails it was written for. – sevenseacat Aug 08 '14 at 01:51
  • @sevenseacat Good point, forgot about that change. Although I often think it's better not to follow a tutorial exactly word for word and to try to add a little individual flare... (but using a slightly different version of Rails probably isn't the best 'individual flare'!) – JKillian Aug 08 '14 at 02:21
  • 1
    @JKillian oh I totally agree. Substitute your own models, field names, etc. build your own app while still following the tutorial. Best way to ensure you actually understand the ideas presented instead of just copy+pasting code. – sevenseacat Aug 08 '14 at 02:35

2 Answers2

26

I think what you're after is this:

gem install rails -v 4.0.8
rails _4.0.8_ new myapp

That would generate a rails 4.0.8 app, and create a default Gemfile locked at 4.0.8. Less tedious.

Rails needs to be installed, to run the rails command. gem is the installer. Ruby needs to be installed, to run gem. Ruby evolves and fixes bugs, old projects may need older versions etc. You probably want a ruby version manager which gets its' own installation (chruby, rbenv, rvm) that should precede the gem install .... You can lock the ruby version for a project, in the Gemfile too e.g.:

source 'https://rubygems.org'

ruby '2.1.2'
JezC
  • 1,868
  • 17
  • 19
  • Ok, so I ran the first command you wrote (THIS TIME ON THE DESKTOP, NOT INSIDE THE NEW RAILS APP), and it seems like no matter where I run the second command now in my computer (e.g. Desktop, but also inside other folders) it will create the new rails app with the specified version 4.0.8 Some improvement here! But regarding the exact question I made I guess the answer is no, you cannot start a new rails app with a version that you have not previously installed on Desktop... Thanks a lot! – assensi Aug 08 '14 at 10:35
  • Another error that can easily occur to whoever found this post useful is that when you run **$ rails server** from the brand new rails app with the specific rails version it can happen that you may have this error: [link](http://stackoverflow.com/questions/22977051/error-launching-rails-server-undefined-method-configure) Pretty much, you have to be very careful and not to forget to change the ruby version that is compatible with the rails version making use of the command 'rvm list' and then 'rvm use __Ruby version__' _before_ you create the new rails app with the specific version of it. – assensi Aug 08 '14 at 16:20
  • Rails isn't a self-installer. :) – frandroid Aug 08 '14 at 17:10
0

There are multiple ways to achieve this, but not using the standard generator.

When running rails new myapp what's happening is that you're running executable of the (latest or specified) installed library. This executable bootstraps your project with version specific template, thus obviously, without this generator you cannot bootstrap those files (since all information about bootstrapping is IN the library). So the best way would be installing this version prior using method suggested by @JezC.

However you don't NEED this generator to start new rails app. You could just as well download the template from the web, make minor changes (it's only handful of places you need to replace) and run bundle install to get all necessary libraries. There are quite a few ways to do it:

  • Cloning pre-defined GitHub repository
  • Generate all templates on different machine and sync from there
  • ...or even creating app that streams those templates live (which shouldn't be too hard)

That being said, I cannot imagine any scenario in which this can be helpful, since when sharing project one can (and will) receive proper versions of both Rails and dependencies used in the project after bundle install'ing.

XLII
  • 1,172
  • 9
  • 18