Specifying rails version to use when creating a new application - ruby-on-rails

I have two versions of rails (2.1.0 and 2.2.2) installed in my computer.
When I create a new application, is it possible to specify that I want to use the older (2.1.0) version?

I found here an undocumented option to create a new application using an older version of Rails.
rails _2.1.0_ new myapp

Here is the command which I use normally:
rails _version_ new application_name
for example rails _7.0.4_ new my_app
Here is the list of all available rails versions so far:
http://rubygems.org/gems/rails/versions

I was having some trouble using rails _version_ new application_name (the resulting project was still generated for the newest version of Rails installed.)
After a bit of digging I found an article by Michael Trojanek with an alternative approach. This works by creating a folder with a Gemfile specifying the desired version of Rails and then using bundle exec rails... so that Bundler takes care of running the appropriate version of rails. e.g. to make a new Rails 4.2.9 projects the steps are:
mkdir myapp
cd myapp
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '4.2.9'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update

As rightly pointed out by #mikej for Rails 5.0.0 or above, you should be following these steps:
Create a directory for your application along with a Gemfile to specify your desired Rails version and let bundler install the dependent gems:
$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install
Check that the correct version of rails has been installed: $ bundle exec rails -v
Now create your application, let Rails create a new Gemfile (or rather overwrite the existing one by using the --force flag) and instead of installing the bundle (--skip-bundle) update it manually:
$ bundle exec rails new . --force --skip-bundle
If you check the entry for rails in Gemfile, it should be like this:
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
You should update it to the exact version needed for the application:
gem 'rails', '5.0.0.1'
Now, the final step:
$ bundle update

There are two ways to achieve this:
one as suggested in accepted answer:
gem install rails -v 2.1.0 #only when the gem has not been installed in the desired ruby version you are using, so that you don't get error on next step
rails _2.1.0_ new my_app
and alternative method is to create gemfile with desired rails version before initializing rails project
mkdir my_app
cd my_app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '2.1.0'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
I have written about this in details in my article

You can generate the skeleton with either version and require the one you want in config/environment.rb:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION
or use the "rails" command form the version you want anyway.

You should also take a look at "freezing" your Rails gems into the app. This helps a lot with deployment, specially in shared hosting environments.
Just change the RAILS_GEM_VERSION variable in config/environment.rb and issue the freeze rake task:
rake rails:freeze:gems

Please watch out which version of ruby you are using with Rails.
The command for making a new project for a specific version of Rail may not work for you. I had some issues about it. And the problem was the ruby version I have default which is 3.0.0. This version did not work with Rails 5. Then I installed ruby 2.7.5 and switched to it as default. Only then I was able to make projects both for Rails 5 and 7.
If you want the same environment with ruby 2.7.5
rvm install ruby-2.7.5
switch to this version as default
rvm --default use 2.7.5
install bundler and webpacker
gem install bundler
gem install webpacker
install lastest rails (which is 7)
gem install rails
test it
rails new test_app_6
cd test_app_6
rails s
check for localhost 3000
http://localhost:3000
then stop the server (control + c) and install Rails 5
gem install rails -v 5.2.6
test it
rails _5.2.6_ new test_app_5
cd test_app_5
rails s
check for localhost 3000
http://localhost:3000
You're set!

Related

Getting started with rails 6: how to start a blank new rails 6 application

I want to rebuild one of my old rails apps using the latest rails version available at github/rails/rails
How do I start off with the "rails new ...." ?
It seems this can be as easy as this:
$ mkdir example
$ cd example
$ echo "source 'https://rubygems.org'" >> Gemfile
$ echo "gem 'rails', git: 'https://github.com/rails/rails.git'" >> Gemfile
$ bundle install
Fetching https://github.com/rails/rails.git
Fetching gem metadata from https://rubygems.org/.............
Resolving dependencies...
Using rake 12.3.1
$ bundle exec rails new . --dev --force
exist
create README.md
create Rakefile
create .ruby-version
create config.ru
create .gitignore
force Gemfile
run git init from "."
$ bundle exec rails server -d
=> Booting Puma
=> Rails 6.0.0.alpha application starting in development
=> Run `rails server --help` for more startup options
UPDATE (2020):
It turns out, the stable branch has nearly all the goodies found in the HEAD, but actually usable in production. So since then, I've modified the solution and nowadays I use these steps:
##
# Install edge rails
#
cd /tmp
git clone -b 6-0-stable --single-branch https://github.com/rails/rails
cd /tmp/rails
LATEST_COMMIT=$(git rev-parse --short HEAD)
bundle update --bundler
bundle install
cd /tmp/rails/railties/exe
bundle exec rails --version
bundle exec rails new /tmp/your-app-name --dev --database=postgresql --force
cd /tmp/your-app-name
bundle exec rails --version # verify
# Without this, you'll get the error: The path does not exist.
cd /tmp/your-app-name
s1='gem .rails.,.*'
s2="gem 'rails', git: 'https:\/\/github.com\/rails\/rails.git', ref: '$LATEST_COMMIT'"
perl -pi.bak -e "s/$s1/$s2/" Gemfile
bundle install
Install rails 6 (6.0.0 as per latest update) in a directory with
gem install rails -v 6.0.0 .
After that you can run gem list rails to check installed versions of rails.
You should see something like below
rails (6.0.0)
or
rails (6.0.0, 5.2.3, 5.2.2)
if you have multiple versions installed.
now create your new app by
rails _6.0.0_ new newapp
EDIT:
before you will follow, check #Rajan Verma's comment
The latest version of rails available as of now is 6.1.0.alpha. You can start the new app with it like this:
rails new MyApp --skip-bundle
then go to Gemfile and change:
gem 'rails', '~> 5.2.3' # or whatever version you have
to:
gem 'rails', github: 'rails/rails'
or if you want to start with the latest stable version, (as of now) change it to:
gem 'rails', '~> 6.0.0.rc1'
Then and run bundle install
Here you can find more details about latest releases.

How to downgrade my rails version?

I am using rails version 4.2.0. How can I downgrade to version 3.2.19?
I tried the following:
I opened command prompt.
I typed gem uninstall rails
Some options came for rails version then I selected my current version and pressed entered.
Then typed gem install rails -v 3.2.19 for installing this version.
I went to my Site directory and typed rails new blog
When I opened the Gemfile of blog application I found again Rails version 4.2.0 is present there.
Do:
gem uninstall rails
gem uninstall railties
Followed by:
gem install rails -v 3.2.19
To check a rails version, directly do:
rails -v
Another workaround:
add following to your Gemfile:
gem 'rails', '3.2.19'
and then run:
bundle install
Or you dont have to downgrade. You can always create a new rails app with a specific version(if that version is already installed)
rails _3.2.19_ new myApp
That is not good approach to uninstall a global version of Rails. So just create a Rails app:
rails new app
then change Rails version in its Gemfile, and issue bundle install:
cd app
sed "s/'rails', '~> 4.2.0'/'rails', '~> 3.2.19'/" -i Gemfile
bundle install
Then, I believe, you should change all the dependent packages to older versions.

Bundle is switchng my rails version but I have no gem file. How to stop this?

I create a gemset with
$ rvm gemset create r3
$ rvm gemset use r3
$ gem install rails -v3.2.13
At this point
$ rails -v
now shows
$ Rails 3.2.13
but every time I do bundle with a project I've forked, I find that
rails -v
shows Rails 4.0.1 - which then gives issue with the project in question when running tests
(4.0.1 conflicts with 3.2.13).
My question is - if my Gemfile only has:
$ cat Gemfile
source 'https://rubygems.org'
gemspec
how is this happening? How can I make my command line ruby version stay at 3.2.13 and not switch to 4.0.1 Once it switches to 4.0.1 I seem to be stuck with that for that gemset and to create a 3.2.13 gemset I have to start over again.
Bundle works by finding the most up-to-date version of the gems that are compatible with the restrictions from the Gemfile. In this case, those restrictions are coming from the gemspec file, which presumably allows versions of Rails greater than 3.2.13. So it's picking the most up-to-date version of Rails allowed - which is 4.0.1. Your RVM configuration is not relevant here.
To lock your particular fork to Rails 3.2.13, just add the following:
gem 'rails', '3.2.13'
to the Gemfile in your fork. This will lock the local version to 3.2.13 when you run bundle.
if you wish to explicitly use different version of rails then the one that would be calculated from Gemfile then you need to use:
NOEXEC_DISABLE=1 rails ...
you can make it permanent for single shell session with:
export NOEXEC_DISABLE=1
rails ...
and to disable loading gems in versions calculated via Gemfile put it in you shell initialization file (like ~/.bash_profile or ~/.zlogin):
export NOEXEC_DISABLE=1
this happens because RVM installs gem rubygems-bundler which automatically analyzes your Gemfile when you run gem binaries and if a version specified via this file is available then it is loaded (even if it is specified only as dependency of your gems).

rails generate rspec:install gives no error, but manpage

OS: Linux Debian Wheezy
Rails version: 3.2.0
I want to configure RSpec to work with Rails.
I'm using this instruction: https://github.com/rspec/rspec-rails ('Installation' section).
When I try:
rails generate rspec:install
after successfully performing all the things described in the manual before,
I get:
Usage: rails new APP_PATH [options]
etc.
How to resolve this?
IMHO Relevant part of my Gemfile:
gem 'rails', '3.2.0'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '~> 2.0'
end
(I'm new to Rails and using M. Hartl manual to learn.)
(What I have tried already:
changing Rails version (4.0.0, then to 3.2.0);
changing rspec version (initially 2.8.0, then to ~> 2.0);
a few times 'bundle update', 'bundle install' and 'bundle update rspec-rails';
installing 'rspec' and 'rspec-rails' using 'gem install';
This is because when you install gem install rails after June 25, 2013 - it will install the rails 4. When you following the course, Hartl asks you to copy his Gemfile located in his GitHub repo which actually refers to Rails '3.2.14'.
To solve this problem, create a gemset using RVM.
rvm gemset create sample_app
rvm gemset use sample_app
Then create a Gemfile inside an empty folder.
mkdir sample
touch Gemfile
Copy the content form the Hartl's (Gemfile)[https://raw.github.com/railstutorial/sample_app_2nd_ed/master/Gemfile] to the Gemfile you just now created and run,
bundle install
Once all gems are installed, simply go outside the folder and delete it.
cd .. && rm -rf sample
Now create a new Rails project and skip the auto bundle install.
rails new sample --skip-unit-test --skip-bundle
Once Rails generated the files, go inside the folder and replace the Gemfile contents again with Hart's Gemfile.
Now run bundle install
Finally you can create the RVM file that will automatically switch to the project gemset.
rvm --create --ruby-version use ruby-2.0.0#sample-app
ruby-2.0.0 refers to my system's active Ruby version. You'll need to replace this accordingly.
This should work fine. Let me know if you have any further help.
Cheers!

how to specific rails version? i have different versions in different project

yulong#ubuntu:~$ rails -v
Rails 3.1.0
yulong#ubuntu:~$ cd four
yulong#ubuntu:~/four$ rails -v
Rails 3.1.0.rc8
yulong#ubuntu:~/four$ cd ..
yulong#ubuntu:~$ cd z
yulong#ubuntu:~/z$ rails -v
Rails 3.1.0.rc6
Use rvm on Linux/OSX and DevKit on Windows
For every project(folder) you can customize ruby and rails versions. Then rvm automatically will set all gems(libraries) for every folder accordingly to your settings.
If you just need to set Rails versions and not Ruby versions per project add the rails version to the Gemfile in each project folder
gem 'rails', '3.1.0'
and run
bundle install

Resources