Rails appears as not installed even though it is installed - ruby-on-rails

I'm new to rails. After finally getting my environment to work properly with RVM 2.0.0 rails 4.0.5, all of a sudden rails disappeared and I keep getting the message: Rails is not currently installed on this system...
When I quit terminal, reopen it, and type:
$ rails v
It shows 4.0.5
However, as soon as I change directory into my rails app and check the rails version I get the message that rails is not installed.
Any idea what's going here?

When you use rvm you have multiple versions of ruby installed. You select which one you want like this:
rvm use 2.0
or
rvm use 2.1
You can also specify a ruby version in a .ruby-version file in a directory. Then when you change to that directory in the terminal, RVM will switch versions for you.
Each version of ruby has its own set of installed gems. Rails is a gem.
Ergo, when you installed rails, you were in your default ruby version (probably the one build-in on your system). When you change directory to your rails application, RVM is kicking in to switch to the correct ruby version for you. But you don't have rails installed in that version.
So the solution is to switch to the directory for your rails app and run:
bundle install
This will install your gem bundle for your application, including the rails gems, and it will do it into the correct ruby version.

Related

In Rails, how do you have multiple versions of puma-dev running on different versions of Ruby via ruby-install?

I have many different Rails apps using puma-dev.
I have started a new Rails app using Ruby 3+ which works great on localhost, however when I visit the site via puma-dev I get an error. This is because I think puma-dev is using the wrong version of Ruby to start the app.
The puma dev logs show this error:
! Unable to load application: Bundler::RubyVersionMismatch: Your Ruby version is 2.6.6, but your Gemfile specified 3.1.2
/Users/abc/.gem/ruby/2.6.6/gems/bundler-2.2.28/lib/bundler/definition.rb:441:in `validate_ruby!': Your Ruby version is 2.6.6, but your Gemfile specified 3.1.2 (Bundler::RubyVersionMismatch)
I’m using ruby-install and chruby to select differernt Rubies.
How do I go about making puma-dev use the versions of ruby installed via ruby-install so that all my different apps can use differnt versions of Ruby?
It seems puma-dev is not fully compatible with chruby in all situations. But this can be fixed by creating a .pumaenv file in your Rails project folder:
# Load chruby
source /usr/local/share/chruby/chruby.sh
# Change to your desired Ruby version
chruby ruby-3.1.2
Please adjust according to where your chruby.sh file is installed on your system.
If you want to make the setup compatible with .ruby-version files, you could do this instead:
source /usr/local/share/chruby/chruby.sh
chruby $(cat .ruby-version)
For further discussion on this topic see this puma-dev issue:
https://github.com/puma/puma-dev/issues/53

Safe way to upgrade Ruby versions and gemsets in Rails applications

I want to upgrade our Ruby version and a bunch of gems on our production website that we are currently running. We use RVM to manage our Ruby versions and gemsets.
I know how to install a new Ruby version using RVM and install gems in a gemset. I also understand that if I run bundle install it will install my gems into the Ruby version I am running the command from.
Is there some way to pre-create a Ruby version with a gemset in it and then swap my production site to use this new Ruby version and gemset? I know this can be done manually, but is there anyway to do it with a gemfile?
Yes, you can specify the Ruby version in the Gemfile:
source 'https://rubygems.org'
ruby '2.1.3'
This allows you to set the version that would be used if you deploy to Heroku, for example. Your question does not detail your production environment, so it's not clear if it can use the Ruby version in the Gemfile.
Locally, the app will run against whichever Ruby version is active with RVM. The only way to know if it will work is to run the specs against the new Ruby version. Your question did not mention specs, but if you have a complete set of specs then this is the proper way to find out if the new Ruby version will work.
You can install new versions of Ruby and gem sets all day long without it affecting a running application. Once an application is running, it continues running under the same version of Ruby the entire time.
You can have RVM install new Rubies, manipulate gemsets, etc., as long as you don't remove the version of Ruby the application needs. If the binary disappears your app might crash/lock-up if the system needs to load something that has been deleted.
This totally depends on your setup. I am anwsering for a Passenger/Apache setup.
With Passenger and Apache, you can set my PassengerRuby in the Apache Vhost file so it uses that particular version of Ruby, I would set it to your current ruby path and restart Apache like so:
PassengerRuby <path-to-ruby>
Then you you can install your new ruby version and gems, once you install everything you can change the PassengerRuby to the new ruby path and restart Apache. This should migrate your Ruby version smoothly, since you can always fallback to your old Ruby version.

Can I use different version of rails for different projects directory in Windows?

Currently Rails 4.0.2 is installed in my Windows machine and I'm using it for several rails projects. But, I do want to use Rails 3.2.8 version for another project.
So, my question: Can I specify the version 3.2.8 on a single project and retain 4.0.2 in all the rest by ensuring all dependencies for 3.2.8 are installed?
Yes !
Install the rails 3.2.8 gem.
Then, you can specify which version of rails you want to use :
rails _3.2.18_ new mySuperSecretProject
In each project, you can use rails _xxx_ console, but it is easier do directly use the binstubs : bin/rails console
Yes you could do it as Intrepidd stated.
Another way to do this is by including a specific gem version in your application Gemfile and then do a bundle install. This will tell the application to use that particular version of rails over your system version
Rails can be installed as follows:
1) Specify the rails gem version in your project Gemfile
gem 'rails','3.2.8' OR gem 'rails', '4.0.2'
2) Try running bundle install. it will automatically install required rails version for you and will create a bundle unique for the project you are in.
3) For rails 4.0.2 project: if rails 3.2.8 is already installed, try running 'bundle update rails' to upgrade the current rails version and it will install all of its dependencies in the corresponding bundle.
If you want to run these projects in different ruby versions, then in Linux platforms RVM is the best option. But, in windows RVM will not work. A good option will be the Pik tool. Pik is a tool to manage multiple versions of ruby on Windows.
Please refer here for the same. Hope it helps :)

Ruby On Rails 3.2.13 - Using Different Gemsets For Rails Applications

I currently have several Rails applications running version 3.2.13 that use Ruby 1.9.3. I plan to eventually upgrade my current applications to Rails 4.0 then upgrade Ruby to 2.0. I need to time the conversion to Ruby 2.0 carefully since from my understanding Phusion Passenger will only handle one version of Ruby without some nose bleeding solutions which I do not plan to attempt to implement at this point.
I currently use one gemset for all my Rails applications. I would like to convert my Rails applications one by one from 3.2.13 to the current version of Rails 4.0 at some point. When I have tried to update one of my applications to a newer version of Rails I was told I had to do a update rails command.
What I want to do is to be able to run some of my applications using Rails 3.2.13 and others using Rails 4.0 until I am sure they will all run properly under Rails 4.0. Once I do this I will review the differences between Ruby 1.9.3 (if any) and upgrade all of them to use Rails 2.0. At this point I understand that I would just create the gemset for 2.0.0 or whatever the latest stable version of Ruby 2 is.
I have done web searches and checked the RVM website. I understand how the gemsets are assigned to the version of Ruby you want to run. However I'm not seeing anything about using gemsets with the same version of Ruby but using different versions of Rails or other combinations of gems on the same machine for different applications. Can this be done?
Any help would be appreciated.
Take a closer look! ;)
First, create a new gemset with a name corresponding to your project
https://rvm.io/gemsets/creating/
ex: rvm gemset create project_name
Then create/edit .rvmrc file in project directory: rvm use 1.9.3#project_name
One pitfall is that if you execute following commands
cd some_project
cd ../other_project
and other_project doesn't have gemset specified you'll stay in some_project gemset. To avoid that you can create .rvmrc file with default gemset in your "projects" directory (if you have one)
Project Folder
|___Gemfile
|__.ruby-gemset
|__.ruby-version
Gemfile with all the required gems mention in your project folder
.ruby-gemset should have gem set name like sample-gemset
.ruby-version file should have specific ruby version (e.g 2.0)
rvm gemset list
it will show gem set created with sample-gemset and do the
bundle install
which will install all the specific gems for this project under sample-gemset,
same you can do for other project to create other gem set, in this way you can maintain different rails/gems and ruby version for different projects with rvm.
Your Gemfile.lock nails down which version of every gem you use - you don't need to worry about gemsets.
bundle install will always install the correct versions and your app will always use the versions in Gemfile.lock, even if there are other versions floating around

Controlling ruby version used in Rails

I have an app using Rails 3.0.6 which I run on two machines. I started with ruby 1.8.7 and recently installed ruby 1.9. I swapped my link in '/usr/bin/ruby' to point to the 1.9 install. When I run 'ruby --version' I get the 1.9. However, in my rails app, when I check 'RUBY_PLATFORM' it shows rails is running with 1.8.7.
How do I tell Rails which ruby environment to use? Strangely, this is not the easiest thing to search for. Probably because I don't know enough about it to form a specific question.
https://rvm.io/rvm/install
Install RVM using these instructions, then you can manage and switch between different Ruby versions and gemsets very easily. Using RVM you can have both 1.8.7 and 1.9.x (or any other version) on your system without having to constantly manually change your path and ruby alias.
Older versions of RVM used the .rvmrc file to automatically switch the ruby within a directory. Newer versions use the .ruby-version file.
To switch between rubies, just type rvm use 2.0.0 or whatever version it is you want to use. For ongoing projects it's a good idea to add the .ruby-version file in the root of the directory that contains the version string you want to use, i.e.
2.0.0

Resources