RVM switching Rails Versions - ruby-on-rails

The situation is that my default gemset on my computer points to rails 5.0.0. However: I have an app that uses a really old rails version. Lets say for this example the app uses: rails 2.1.1.
Does my machine automatically switch to rails 2.1.1 when cd'ed into this app because rails 2.1.1 is specified in the Gemfile? Or: do I need to explicitly create a gemset in order for my app and my machine to do things the "rails 2.1.1 way" (as opposed to doing stuff the "rails 5.0.0 way which is the rails version in my default gemset)?
My concern is that perhaps if my machine thinks I am using rails 5.0.0 instead of rails 2.1.1 while developing inside the app: then rails commands such as generators might create files and do stuff "the rails 5.0.0 way" as opposed to the "rails 2.1.1" way.
Hopefully this makes sense. Here is what I would do in order to "explicitly" state via a Gemset that this app uses rails 2.1.1
Example:
I create a gemset that is to be specified for any app that uses rails 2.1.1
rvm gemset create rails_2_1_1
I specify the ruby version to use on this gemset
rvm use 2.2.1#rails_2_1_1
I then install that old version of rails onto this gemset:
gem install rails --version=2.1.1
Now this gemset uses rails 2.1.1.
Now at the root of my app I specify a .ruby-gemset file that tells rails: "Make sure you are doing stuff the rails 2.1.1 way and not the rails 5.0.0 way:
#.ruby-gemset
rails_2_1_1
I want to be sure that if another rails version is specified in an app's Gemfile than is in my default gemset: then developing within that app will do everything within the context of the rails version specified in the Gemfile as opposed to the rails version in the default gemset.

That's not a problem with RVM and Rails. Using the Gemfile is the best way to do this, IMHO! In your Gemfile, you can specify which ruby and which gemset within that ruby to use.
Set Default Ruby/Gemset --for system
First, lets establish that default ruby for the system on 5.0.0. This will allow any new/existing Rails projects to default to this ruby version (except for those projects that override with the Gemfile):
rvm use --default 5.0.0
..and of course, if you want it fixed to a specified gemset:
rvm use --default 5.0.0#my_default_gemset
Set Default Ruby/Gemset --for Rails specific App on Gemfile
Example 1
If you want to setup your rails app to utilize the RVM gemset 2.2.1#rails_2_1_1, similar to the RVM command below...
rvm use 2.2.1#rails_2_1_1
In your Gemfile, specify right below the source line the following two commented lines:
source 'https://rubygems.org'
#ruby=2.2.1
#ruby-gemset=rails_2_1_1
Now, when you cd into your rails' app directory, you should receive the following message, or similiar:
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does
that too, you can ignore these warnings with 'rvm rvmrc warning ignore
/my/rails/app/path/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore
allGemfiles'.
You can double check your results:
rvm list gemsets
ruby-2.2.1 [ x86_64 ]
ruby-2.2.1#global [ x86_64 ]
=> ruby-2.2.1#rails_2_1_1 [ x86_64 ]
ruby-5.0.0 [ x86_64 ]
ruby-5.0.0#global [ x86_64 ]
Example 2
Another example using ruby-2.0.0-p247#rails-4.0.0, example RVM command...
rvm use ruby-2.0.0-p247#rails-4.0.0
In your Gemfile, specify:
#ruby=2.0.0-p247
#ruby-gemset=rails-4.0.0

You can also create rvmrc file into your project directory then it will switch rvm automatically
Please follow the process to create rvmrc file.
Go to project directory
rvm use 2.2.1#rails_2_1_1
rvm --rvmrc --create 2.2.1#rails_2_1_1
I think it would help you.

You can create a .ruby-version file in your project's folder and RVM will automatically switch to the correct Ruby. Something like this:
$ echo '2.1.1' > .ruby-version
You just need to make sure you have that version installed, eg.:
$ rvm install ruby-2.1.1

First of all, I recommend against using .ruby-gemset file, because Gemfile already has everything you'll need. .ruby-version is obsolete too, because Ruby version can be specified inside Gemfile as well:
source 'https://rubygems.org'
ruby '2.3.1'
gem 'rails', '~> 4.2.7'
...
Now about Rails versions. As long as you run commands with bundle exec, these commands' versions will be exactly as stated in Gemfile.
There are also binstubs scripts inside projects bin/ folder. They're the most preferable way to run commands, because they're usually tweaked for project needs (for example running spring along with your command) and they ultimately run bundle exec anyways.
So the proper way to do things, is to run ./bin/rails, which can be tedious. That's why I recommend using helpers. For example, oh-my-zsh project has out-of-the-box rails plugin. First few lines (read below for explanation):
function _rails_command () {
if [ -e "bin/rails" ]; then
bin/rails $#
elif [ -e "script/rails" ]; then
ruby script/rails $#
elif [ -e "script/server" ]; then
ruby script/$#
else
command rails $#
fi
}
function _rake_command () {
if [ -e "bin/rake" ]; then
bin/rake $#
elif type bundle &> /dev/null && [ -e "Gemfile" ]; then
bundle exec rake $#
else
command rake $#
fi
}
alias rails='_rails_command'
compdef _rails_command=rails
alias rake='_rake_command'
compdef _rake_command=rake
When you run rails, this script checks if bin/rails exists (Rails 5 and 4 default) and runs it instead. If not, script checks for script/rails (Rails 3 default) and script/server (Rails 2 default). So basically you have all versions covered. Same is for rake.
With this plugin you never need to worry about extra commands. Always simply run rails. You just install it once and forget about it.

Related

How to delete a gem so that I don't need to prepend bundle exec for rails commands?

If I try to run tests I get
Gem::LoadError: You have already activated jruby-openssl 0.9.5, but your Gemfile requires jruby-openssl 0.8.5. Prepending bundle exec to your command may solve this.
I can do as indicated and prepend bundle exec to get around this.
This is happening because I have a newer version of the gem in another project.
However I am no longer using the other application with the newer version of the gem, so I was wondering:
How can I actually remove the newer version so I don't have to bundle exec before my rails commands?
You can delete a gem (or a specific version of a gem) by running
gem uninstall <gem_name>
If there's multiple versions then a prompt will ask which version to remove.
Use gem uninstall with the -v switch to specify a version to uninstall:
gem uninstall jruby-openssl -v 0.9.5
Depending on your OS and shell, you could create an alias or function for the command (I’m guessing this is using Rake).
For example I have this in my .bashrc
function bake {
bundle exec rake "$#"
}
So now I can type bake whatever and the command that is run is bundle exec rake whatever.
This usually happens when you are 'polluting' the global gemset using it for different projects.
A good way to isolate gemsets is to use rvm. Once installed, you can create a file per project named .ruby-version with a single line on it that identifies the gemset with the syntax <ruby version>#<your project>. From the console:
$> echo "2.1#myproject" > .ruby-version
$> cd .
This will create a gemset for your project and everytime you enter the root directory, the gemset will be specific to this project. Then you could have different gem versions for different projects and run rake (or any other command) just fine without messing dependencies.

Ruby 2.1.1, Could not find Railties

Working with this repo, which requires ruby 2.1.1 and rails 4.1. I am using RVM, and I have downloaded ruby 2.1.1, but I am getting this error when I type rails -v. I feel like I need to start completely over, but do you see anything below that may have a relatively simple solution?
When I type rails -v, I get this error:
/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/dependency.rb:298:in to_specs': Could not find 'railties' (>= 0) among 14 total gem(s) (Gem::LoadError)
When I type in ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
Bash_Profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
RVM Gemset List
gemsets for ruby-2.1.1 (found in /Users/me/.rvm/gems/ruby-2.1.1)
=> (default)
demo
global
myapp
rails
rails4.1
Try to run these commands:
rvm use 2.1.1#rails-devise --create
bundle install
rails -v
evan: but not when I generate my own app because it gives me an error before I can create it.
Lets discuss some basic concepts of RVM, that can help you:
Every project should contain its own gemset. Repo contains two special files in root folder:
.ruby-gemset - contains unique, project-specified name of gemset
.ruby-version - Ruby version number, to see list of available: rvm list
These files should be placed in root folder of every project on your computer.
When you open project from the linux Terminal, rvm scan for these two files, read them, and activates needed ruby version and loads necessary gems automatically.
Note: each rvm session is alive in current Terminal session only. You should reactivate it for each Terminal session separately.
If current gemset name is not specified, RVM uses default rvm gemset (unnamed gemset).
I uses default gemset for my default projects, when I need quickly create something temporary. But I noticed, that default gemsets of beginners reminds a dump of different unsystematic gems. Some of them can relate to same Gem modules, but with different versions.
Such kind of versioning, may leads to different strange errors and problems, what happened with you. If you want use a default gems set (I suppose you is a NOT rails-developer, based on your profile), open your project directory, and perform in terminal:
rvm gemset empty ''
rvm use #default --default
bundle install

can't install new Rails app; RVM 'global' gemset missing from main directory but visible in app sub-directories

When I try to create a new Rails app in the directory in which I keep all my Rails apps, I get the following error:
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
I've recently been working with an RVM tutorial, so I thought it might have something to do with a gemset I had created. I typed 'rvm gemset list' and found the following:
gemsets for system (found in /Users/rickthomas/.rvm/gems/system)
=> (default)
*
But the weird thing is, I cd'ed into the directory of one of the apps, and ran the same command, and found this:
gemsets for ruby-1.9.3-p429 (found in /Users/rickthomas/.rvm/gems/ruby-1.9.3-p429)
=> (default)
global
When I run the 'rails --version' command in the main directory, I get the message to run 'sudo gem install rails', but when I run the same command from within the app directory, I get this:
Rails 3.2.12
Kinda confused why I all of a sudden can't create a new Rails app, since the last one I created was this morning, a day after I finished the RVM tutorial, and didn't make any gemset changes since then.
It looks like your "system" ruby doesn't have the Rails gem installed. You probably don't want to be using the system ruby anyway. How about you try this:
rvm use 1.9.3 # switch to your Ruby 1.9.3 that the other app used
gem list # make sure rails is listed
gem install rails # (only if rails was not listed)
rails new myapp
There is nothing weird about the "weird thing" you observed. When you cd into a directory, RVM looks at files like .rvmrc, .ruby-version, and .ruby-gemset in that directory and it automatically changes your environment to match. You can run "rvm info" to see what kind of environment you are currently in (I usually focus on the GEM_HOME and GEM_PATH variables). In this case, cd'ing into your Rails app directory caused RVM to switch you to Ruby 1.9.3, and that's the Ruby where you had the rails gem installed. Seems normal to me.

How do you use multiple rails versions with rbenv?

Is it possible to use multiple versions of rails using rbenv (e.g. 2.3 and 3.1)? This was easy with gemsets in rvm, but I'm wondering what the best way is to do it now that I've switched to rbenv (also, I'm looking for a way to do it without rbenv-gemset).
not sure if you got an answer to this, but I thought I'd offer what I did and it seemed to work.
So once you get rbenv installed, and you use it to install a specific ruby version, you can install multiple versions of rails to for that ruby.
STEP 1. Install whatever version(s) of rails you want per ruby version
% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version 3.0.11
By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line, you're specifying which ruby rbenv should be concerned with.
Then following that with the "rbenv exec" command, you can install rails. Just use the version flag as in the example to specify which version you want. Not sure if you can install multiple versions in one shot, but I just run this command as many times as needed to install each version I want.
Note: This will all be managed within your rbenv directory, so it's perfectly safe and contained.
STEP 2. Build a new rails project by specifying the rails version you want.
% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project
STEP 3. Don't forget to go into that project and set the local rbenv ruby version.
% cd my_project
% rbenv local 1.9.2-p290
Now if you want to delete this project, just delete it as normal.
If you want to delete / manage a rails version from rbenv gems, you can use regular gem commands, just prefix your command line with:
% RBENV_VERSION=1.9.2-p290 rbenv exec gem {some command}
And of course, you can delete a complete ruby version and all its shims, etc that are managed within rbenv pretty easily. I like how self contained everything is.
Hope this helps.
For reference, this is a pretty good walk through of at least some of this stuff:
http://ascarter.net/2011/09/25/modern-ruby-development.html
There is a rbenv plugin called rbenv-gemset which should behave similar to the rvm gemset-command but since rbenv was never intended to work this way, I haven't tried it.
I usually manage Rails versions with Bundler as Nathan suggested in the comments of one of the other answers. I create a Gemfile with my desired Rails version, run bundle install, create the Rails application, let it replace the Gemfile and let Bundler take over:
mkdir my-rails-app
cd my-rails-app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '3.2.17'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
If you want more detail, I wrote an article on my blog about it.
Hope it helps!
If you have setup ruby using rbenv the following will work.
Installing rails, the latest version (7.x as of Oct 2022)
gem install rails -v 7.0.2.4
# Find exe
rbenv rehash
To create a rails project with the latest rails version,
rails new project_1
This will create a rails application with the latest version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Installing rails, 6.x.x.x version
Assuming we are going to install rails 6.0.4.8, then issue the following commands
gem install rails -v 6.0.4.8
rbenv rehash
Now, to create a rails project with 6.0.4.8 version (which is installed previously), specify the rails version along with the rails command.
rails _6.0.4.8_ new project_2
This will create a rails application with the 6.x version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Other notes
Similarly, we can manage any no of rails versions in any number of
projects.
rbenv rehash Installs shims for all Ruby executables known to
rbenv
In this approach, you don't need to set or modify any ruby
environment variables.
You don't need to modify Gemspec file by yourself.
The instructions work as of Oct 2022.

RVM finding system gems' executables

I have installed RVM along with ruby versions. However if I fire up the console and run the command rails server, bundle install, etc. I get this error
bash: /usr/bin/rails: /usr/bin/ruby1.8: bad interpreter: No such file or directory
But if I run rvm use 1.9.2 first, then everything is ok. I tried using `rvm use --default 1.9.2' but nothing changed. Does this mean it uses a different ruby from the ones in RVM? Thanks in advance!
Explanation of rubygems bin folders and PATH
Oh. You didn't have rails installed in your rvm ruby, but you did in your system ruby.
Individual gems, like rails can have a bin directory that will contain executable helper scripts. Your system default rubygems is making symlinks from your system /usr/bin/ dir into the gem's bin folder for these helper executables.
RVM provides a similar facility, except instead of polluting the system /usr/bin dir, it just appends its ~/.rvm/gems/#{rvm_gemset_string}/bin folder to the PATH environment variable.
Importing system Rubygems list into your new rvm rubies' gem directories
RVM by default will not import your gems from your system ruby installation into your rvm ruby installs. It makes a full clean fork of the entire ruby system including rubygems (the gem 'rubygems') and rubygems' gem list. When you rvm install 1.9.2 it's like you've made a completely new install of everything used with ruby.
If you'd like to get all your system ruby gems that you were previously using into your preferred rvm ruby, try this:
rvm use system
rvm gemset export system.gems
rvm use 1.9.2
rvm gemset import system.gems
#You'll now have all your system gems reinstalled to your new ruby version
Original Answer/ Edits from #Telemachus
Try moving the lines that source rvm to the end of your ~/.bash_profile or ~/.bashrc (whichever you have it in):
'[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function'
.
bash: /usr/bin/rails: /usr/bin/ruby1.8: bad interpreter: No such file ...
| | ^--------------------------------\
^ Bash, not rvm; ^/usr/bin/rails, not ~/.rvm/gems/*/bin/rails; |
Some ruby leftover from a previous install in the os
You have rails installed in /usr/bin, which is probably before the rvm ruby bin path in your bash echo $PATH variable, so it's finding the system rails install (/usr/bin/rails, a ruby script) which starts like this:
#! /usr/bin/ruby18
You've gotta make the conflict stop happening, the best of all possible ways is making sure that RVM's bin dir is at the beginning of your PATH. This happens in the #Load rvm environment script that you added to your ~/.bash_profile when installing rvm. If you installed rvm as a system library rather than just for your user, this will be different.
If you get to that case, ask #Telemachus.
You'll then need to ensure you've gotten the rails gem installed in your new rvm ruby as above.
Acceptance Test:
You'll find that when you've done rvm use 1.9.2, then which ruby will return something like ~/.rvm/rubies/1.9.2/bin/ruby, and which rails should return something like ~/.rvm/gems/*/bin/rails.
I just solved the same problem on Windows Vista.
My console was giving me this message:
$ rails -v
sh: /c/RailsInstaller/Ruby1.9.2/bin/rails: C:/Projects/railsinstaller/Stage/Ruby1.9.2 /bin/ruby.exe: bad interpreter: No such file or directory
I just edited the first line of this file:
C:\RailsInstaller\Ruby1.9.2\bin\rails
And made it point to the correct location for ruby.exe, on my system, like this:
#!C:\RailsInstaller\Ruby1.9.2\bin\ruby.exe
Et voilà, problem solved!
You need to run rvm use --default 1.9.2, not just rvm use --default.

Resources