1

I'm quite newbie in ruby and ruby on rails and I'd like some clarification, if possible.

I'm currently having rails 4.2.6 installed on my development environment in which I have built a few projects. Now, for my new projects I'd like to use Rails 5, so I assume that if I type the gem install rails command will get me the latest rails verion and probably set it as default, so every time I want to create a new project by rails new my_new_project_name this project will have the latest version (currently v5).

My question is will my gem list contain both rails versions then or is it going to cause any conflicts and issues to my old porjects? so, if I want to work back on any of my projects which has the "old" version, won't affect of any changes, right? As far as I understand its the bundler who picks the version of each gem, right?

If thats the case, I assume same thing applies and for every other gem that I use for each project, right?

ltdev
  • 4,037
  • 20
  • 69
  • 129

3 Answers3

2

You will have all different versions. BUT you will need to add the version number for all gems to your gemfile

For example enter image description here

and in the gemfile you state:

gem 'remotipart', '1.2.1'
Fallenhero
  • 1,563
  • 1
  • 8
  • 17
2

Best thing to do when using different versions of a same gem is to use either rbenv or RVM to create different gemsets for each project. This way You can be sure that your project won't load by mistake another version .

I personally use RVM so i am going to let You know how to use it.

1) install RVM from here https://rvm.io

2 ) make sure You are loading RVM in your .bashrc or .bash_profile files from your home directory ( /home/your_username) . You can use this code:

export PATH="$PATH:$HOME/.rvm/bin"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

3) take a look at this page https://rvm.io/workflow/projects and choose how You want to set RVM for your project . i personally use .rvmrc because i am old school...but those are not recommended anymore because they need trusting and are slower. As a alternative You can use .ruby-version and .ruby-gemset files. But You can use .versions.conf also If You want. Let's say for now we choose to use .ruby-version and .ruby-gemset files.

4) cd into your project and run this command `rvm --ruby-version use 2.3.3@project1 --create' . This will generate those two files în your project . And everytime You will cd into this project RVM will pick-up the gemset automatically.

5) Do the step 4) for the second project also but replace 'project1' with 'project2' .

6) Now You can edit the Gemfiles of these two project and put the version of rails that You desire.

7) to install each project You need to cd into that directory and run command 'gem install bundler' ( only first time) and then You can safely do 'bundle install'

8) repeat 7) for second project.

9) You are all Done. Now You have different version of same gem in two different gemsets .

To also answer the other questions:

1) having în the same gemset multiple versions of same gem can lead to conflicts especially when doing 'rails new project ' from terminal since this doesn't require a specific version . I suggest to create different gemset before installing a new version of a existing gem on your machine. For example for this particular case we can do this

rvm use 2.3.3@rails5 --create

gem install bundler

gem install rails -v 5.O.0.1

And now we can safely do 'rails new project' . This way the new version of rails is inside a new gemset .

I Hope this will help You :)

Rada Bogdan
  • 342
  • 3
  • 9
  • I'm using RVM too and had read a bit about gemfiles, however I thought this was not required, since bundler added on Rails.One thing I haven't completely understand is that with this process you create the rvm-project and then inside this the rails-project folder, right? so basically you have a project folder nested inside another folder, correct? or when you do `rails new project` you can set the gemsets too? – ltdev Dec 01 '16 at 08:23
  • First of all, gemsets don't create a folder in the current directory. If you create a gemset (2.3.3@project1) , it will create a directory in /home/username/.rvm/gems/2.3.3@project1 where all gems used in your project will reside. So there is no nesting of folders, gemsets are located in different folder. However, you are totally right, if your goal is only to run "rails new project" with other version, then you don't really need RVM for running this command, you can totally do this with Bundler ( check out this blog: http://rakeroutes.com/blog/how-to-use-bundler-instead-of-rvm-gemsets/ ) – Rada Bogdan Dec 01 '16 at 13:14
  • However RVM helps you in a different situation. Let;s suppose we have in Gemfile `gem 'rails' , '5.0.0' ` and in a different project we have `gem 'rails', '~> 5.0.0' ` , The first project will match against version 5.0.0, because it is fixed, however the second project will not load 5.0.0, but 5.0.0.1 , because the matching is different, So if you have both 5.0.0 and 5.0.0.1 in same gemset , and you use only bundler, you will get unexpected result. RVM fixes this problem but completely separating this versions in different gemsets so there is no conflict – Rada Bogdan Dec 01 '16 at 13:14
  • You can find more about this differences here http://stackoverflow.com/a/15586667/2067375 . This is a great answer that i hope will help you – Rada Bogdan Dec 01 '16 at 13:16
  • Ok, so for using gemsets for my (new) projects I have to follow the steps you describe on your answer, but how do I set gemsets to my "old" projects then? I guess the gems on my list now are in some kind "global" scope, right? Btw I 've checked all the gemfiles of my projects and they are all look like this: `gem 'rails', '4.2.6'`, `gem 'haml', '~> 4.0', '>= 4.0.7'`, `gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'`, `gem 'devise', '~> 4.2'` etc etc, so only rails gem match specific version, not that I did this intentionally, I was just copying it from ribygems.org – ltdev Dec 01 '16 at 14:00
  • Yes, currently your gems are all installed inside the "default" ( or global) gemset . You just need to repeat same steps starting from 5) for all other projects . Its very easy . – Rada Bogdan Dec 01 '16 at 14:20
0

I solved this kind of dependence issues by moving all my developments to docker. So now I have a unique environment for each project trough use of Dockerfile.

I also employ Makefile and compose.yml to automatize by ci. Hope that help.

alectrico
  • 15
  • 6