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?
You will have all different versions. BUT you will need to add the version number for all gems to your gemfile
For example
and in the gemfile you state:
gem 'remotipart', '1.2.1'
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 :)
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.
Related
following http://railsapps.github.io/installrubyonrails-mac.html , I encounter the following commands
rvm use ruby-2.1.3#rails4.1 --create
gem install rails # installs the latest rails version
rails -v # returns 4.1.6
however, I can also do the following, which would add rails version to 4.0.8
rvm use ruby-2.1.3#rails4.0 --create
gem install rails --version=4.0.8 # installs the latest rails version
rails -v # returns 4.0.8
What is the point of this? Somewhere in the text it's said that this method is to prevent global gem-set and instead install rails based on project-specific gemsets? What does that even mean?
And this is the instructions on how to create a new rails project
$ mkdir myapp
$ cd myapp
$ rvm use ruby-2.1.3#myapp --ruby-version --create
$ gem install rails
$ rails new .
why not just call rails new myapp? The text says it's to "create a project-specific gemset", but I have no idea what that means. Wouldn't this just install rails 4.1.6 (newest version) ? why not just install rails 4.1.6 globally in the first place then?
Imagine you are a Rails developer in a company that has been doing Rails apps for the last 4 years. You have apps on Rails 2, Rails 3, Rails 4 - as new versions come out, you upgrade your toolset because, why not? Each new version is better.
However, they are not downward compatible. The Rails 2 app will not work with Rails 4.1. What if you are asked to urgently debug the Rails 2 app while hacking on Rails 4 one? Uninstall global Rails, install Rails 2, make the hack, then uninstall Rails 2 and reinstall the new one again, just so you can run the tests for your one-line bug fix?
That's where gemsets come in. You have environment per-application so that each application can be run self-sufficiently, with no versioning conflicts.
If you don't envision such scenarios of version conflict on your machine (i.e. if you can only imagine working on one project), gemsets are completely irrelevant.
EDIT after some confusion still in comments :) Let's go step by step, see what happens exactly.
$ mkdir myapp
$ cd myapp
You're now in an empty directory.
$ rvm use ruby-2.1.3#myapp --ruby-version --create
rvm creates a new gemset, named ruby-2.1.3#myapp, which will be run with Ruby 2.1.3. As a consequence, you have a new directory at ~/.rvm/gems/ruby-2.1.3#myapp, where your gemset will be. You also have two new files in your previously empty myapp directory: .ruby-version (which contains a single line saying ruby-2.1.3) and .ruby-version (containing the line myapp). These two lines are read by rvm every time you enter the myapp directory: it sets the current Ruby and gemset for you.
$ gem install rails
Having recognised that the current gemset is now ruby-2.1.3#myapp, the gem install command will download the newest rails gem, as well as all its dependencies, and put them in your gemset directory (~/.rvm/gems/ruby-2.1.3#myapp/).
$ get install rails --version=4.0.8
If you try this, it will dutifully install Rails 4.0.8, but since you have a newer version in your gemset and your application is not specifically having any requirements, the newer one will take precedence. This is normally not what you want; and anyway, there is rarely a reason to develop a project to comply with two different versions of a library (unless you're developing a library or a plugin; that's a different story).
$ rails new .
rails is actually executing ~/.rvm/gems/ruby-2.1.3#myapp/bin/rails. If you were not in myapp directory, linked to the gemset, this command would have failed (if you didn't have Rails installed in the global environment), or executed globally installed Rails (if you did).
So, it's not really designed to have two versions of Rails simultaneously in the same project. But when you make another project, with another gemset (say, ruby-2.1.3#myotherapp), you could have a different version of Rails while you're there. The version automagically changes just based on which directory you cd into.
This is a beginner-level question.
I'm using Ubuntu 12.04
I copied a project (created on Rails 4 using the rails new command) from Dropbox to my local environment, where I have previously install Rails 4 and up-to-date Ruby and RVM, went to project's directory, typed rails server and got
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I ran gem install rails instead.
Will I have to run gem install rails on every project's directory? I thought the Rails install was a general and accessible on my whole environment.
The project was created using the same Ruby version, but on a MacOS X system.
The project is a static web brochure and has no database configuration.
Thank you in advance.
Make sure you're using the correct version of Ruby - the same version that you'd installed Rails into - with rvm list.
You likely have two of them (since you have such an issue) - the system Ruby and the RVM-installed Ruby. And likely RVM didn't engage and hook up the correct path to the rails executable, thus the error.
This should fix the issue:
rvm use whatever-ruby-you-had-installed-rails-into
Where whatever-ruby-you-had-installed-rails-into is a string like ruby-2.1.0-p0 taken from the rvm list output.
To make RVM retain Ruby version for the project.
echo whatever-ruby-you-had-installed-into >.ruby-version
in your project's path.
So after a few hours of testing, reproducing the problem, and reading (the other answers inclusive) I got the solution as follows:
Short answer: No. Rails needs to be installed only once.
Long answer: This problem occurred because of a default setting on Terminal that prevents the system from using RVM installations of ruby and rails. The solution is to integrate RVM with gnome-terminal as described in the RVM website.
With terminal window active, go to the menu at the top bar
Edit > Profile Preferences > Title and Command tab
Check the Run command as a login shell box
Restart Terminal and make sure your gemset and ruby version are set
rails server should now work as expected (you might be prompted to run bundle install before Rails can actually run fine, follow the promtp).
I am still learning to work with Ruby on Rails, so any inputs, clarifications, or additional information on the issue is more than welcome.
You don't have to install Rails on every project, but the gems that you need for that project.
With bundle install you install all the gems that you specify in Gemfile.
If you want to avoid reinstall the gems every time you change project, I suggest you to have a better look to RVM: it has got an opt called gemset (https://rvm.io/gemsets), if you use it you just need to switch your gemset:
rvm gemset use yourgemset
I hope it can help you.
I've tried looking around the internet for a solution to this but to no avail. Every single time I try to set up a new app on my local machine I run into a ridiculous amount of issues with RVM. I decided its finally time I learn to do this right.
Basically I have been given permission to a Rails project in Github and I want to get it running properly on my local machine.
Generally speaking, what I have been doing in the past is this:
$ git clone git#github.com/projectpath
$ ls projectpath
$ bundle install
$ rake db:setup
And cross my fingers. Sometimes it works, other times I have to use RVM to change rubies and redo bundle install. Today, everything is breaking and I keep running into problems like this:
$ rails -v
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 have a feeling that I am not setting up these apps correctly and also not using RVM as its supposed to be used - basically I believe that my fundamental understanding of RVM is wrong. I would very greatly appreciate it if someone could outline the proper way to do set up a new app using a remote Github repo, as well as explain the proper way to use RVM to get this new app running. Thanks in advance!
I think your life would be easier if you used RVM's gemsets. One gemset per rails application. This keeps everything nice and isolated from each other.
So... something like this...
# install bundler in the global gemset as you want it available everywhere.
$ rvm use #global
$ gem install bundler
# create a project specific rvmrc/gemset.
$ cd /folder/containing/your/github/repo
$ echo "rvm use #myapp" > .rvmrc
$ cd into/rails/root/for/your/app
$ bundle install
At this point you're installing the gems into the 'myapp' gemset, totally isolated from your other gems. There's a new file name for .rvmrc, but I can never remember what it is. .ruby-version or something.
You may also need to create the gemset before it can be used. See the RVM docs for how to make that happen automatically.
Do these cloned projects have files named
.ruby-version
and
.ruby-gemset
in the top level directory? Those files instruct rvm (and other versioning systems) to use a particular ruby version and gemset combo. It could be sometimes you cd into a cloned directory that references the name of a gemset you've already created, in which case bundle and rake work, and sometimes it references a gemset you don't have, in which case you get mysterious blowups.
TL;DR
Don't bother with gemsets; multiple versions of a gem may be installed concurrently.
When necessary, specify which version to execute using $ gem-based-binary _version_ args notation.
Use bundle exec when you have a Gemfile specifying the version.
gem install rails -v 3.2.13
rails _3.2.13_ new Project2
cd Project2
bundle exec rails server
UPDATE: 2015-06-04
I wrote this question three years ago. Partly, it was based on a false assumption, and partly the situation has changed since then. With appreciation to #indirect for his original answer, I want to call attention to #kelvin's newer (less upvoted) answer, summarized above.
My false assumption: Only a single version of a gem could be installed at a time, hence the need for gemsets to isolate the namespace. Not true. Multiple versions of a gem may be installed concurrently. The most recent one will be used when invoked from a command line, unless you have a Gemfile specifying the version constraints and invoke the command via bundle exec, or specify the version as its first argument.
See also How can I call an older version of a gem from the commandline? re: the underscore-version notation.
Original question:
I have multiple projects going on using different versions of Rails. I have a workflow (described below) for creating projects using specific versions of rails, and keeping the projects isolated from each other. I'd like to experiment with other workflows, in particular, using rbenv instead of RVM, but it's not clear how to do so.
QUESTION: What is the best current practice for creating multiple rails projects, each using a different version of rails, when making use of rbenv and bundler, as opposed to rbenv-gemset or rvm?
USE CASE: I have two rails projects, called ProjectA and ProjectB. ProjectA is developed using one version of rails ("RailsA"), whereas ProjectB uses a different version ("RailsB"). How do I manage having both versions installed?
THE GEMSETS APPROACH: When I first started with Rails development, I used RVM. In addition to supporting multiple, concurrent installations of ruby, RVM supports having multiple Named Gem Sets. Each project has its own independent collection of gems (including rails itself) called a gemset:
rvm gemset create RailsA
rvm gemset use RailsA
# RailsA. Note: My question is not version-specific.
gem install rails --version 3.0
rails new ProjectA
cd ProjectA
rvm --rvmrc use `rvm current`
vi Gemfile
bundle install
cd ..
## Now do the same for ProjectB
rvm gemset create RailsB
rvm gemset use RailsB
gem install rails --version 3.2
rails new ProjectB
cd ProjectB
rvm --rvmrc use `rvm current`
vi Gemfile
bundle install
Note: The very creation of the project folders should be done (IMHO) by a rails new command using the desired version of rails, since the skeleton files change from version to version. (Perhaps I should revisit this premise?)
THE BUNDLER APPROACH: I've been playing with using rbenv instead of RVM, but I don't understand the workflow as clearly. In the README.md, Sam Stephenson writes that "rbenv does not ... manage gemsets. Bundler is a better way to manage application dependencies." There is a plugin (rbenv-gemset) for getting the same results as rvm's gemsets, but Sam clearly favors using Bundler instead. Unfortunately, he doesn't elaborate on what the workflow would look like. Even the Bundler website doesn't explicitly connect all the dots of how to isolate one project from another. Several blogs and gists come to the rescue, suggesting the following ~/.bundle/config file:
---
BUNDLE_PATH: vendor/bundle
(BTW, I'm not sure what the "---" is about. The docs make no mention of it and it doesn't seem to make a difference.)
This effectively gives each rails project its own gemset, storing the gems in ProjectX/vendor/bundle/. In fact, rails itself will be (re-)installed there, making the project completely independent of the rest of my environment, once I run bundle install.
But the elephant in the room is the chicken-and-egg problem of creating the rails project folder in the first place!! In order to create the ProjectA folder using RailsA, I need to install rails (and its numerous dependencies) first. But when I want to create ProjectB, I must then switch to using RailsB. Without gemsets, I must do some serious upgrading/downgrading. Not cool.
A possible solution is simply not to worry about what version of rails I use to create the ProjectX folder. If I then use rails 3.0 to create a 3.2 project, I could just manually create the app/assets tree. But that just irks me. Ain't there a better way?
Most people solve this by installing the rails gem first via gem install rails. If you refuse to do that for some reason, you can opt out of the automatic bundling that Rails attempts to do for you. This will work completely regardless of your ruby management system.
mkdir myapp
cd myapp
echo "source :rubygems" > Gemfile
echo "gem 'rails', '3.2.2'" >> Gemfile
bundle install --path vendor/bundle
bundle exec rails new . --skip-bundle
When prompted, type "y" to replace your Gemfile with the default Rails one (or not, as you prefer). Then, once it's done:
bundle install
You're done, and you have boostrapped a new rails app with the version of your choice without installing the rails gem into rubygems.
Suppose you have rails 3.1.0 installed, but you want to create a new project using rails 3.2.13 which is not installed.
Let's say you want the new project to be in ~/projects/Project2.
gem install rails -v 3.2.13
cd ~/projects
rails _3.2.13_ new Project2
This will create the Gemfile for you, locked to the version of rails you specified on the command-line.
I deliberately omitted the idea of keeping a separate copy of gems for the new project, because that goes against the Bundler philosophy, which is to have all gems installed in one place. When you run rails, Bundler will pick the correct gem versions automatically from that central location. That means a project can share gems instead of installing a fresh copy for itself. (Note, however that each version of ruby you install will have its own gems. This is a good thing because native extensions likely won't work across ruby versions.)
You do have to be a bit more aware, because most commands, like rake, will load the newest version of rake that you have installed. You'll need to run bundle exec rake ... to make sure the correct version is loaded. Usually I'll run bundle exec for all commands except rails. You can create an alias to make it shorter (I use bex). To automate this with gem executables, you can use rbenv-binstubs, but you still have to be aware that running non-gem executables like ruby and irb won't automatically use the Gemfile.
Sidenote: rails new will run bundle install, which will check for the newest version of the dependencies. If you want bundler to try to use currently installed gems that satisfy the dependency requirements, you can skip the bundle install with rails new --skip-bundle, then run bundle check in the app dir.
Sidenote 2: suppose you want to use a ruby version for Project2 (e.g. 2.1.8) that's different from the default (e.g. 2.3.0). In that case, running gem install as specified above will install the gems under 2.3.0, which is a waste of time because you'll need to install the gems again under 2.1.8. To solve that problem, you can force the commands to use the preferred version via environment variable:
RBENV_VERSION=2.1.8 gem install rails -v 3.2.13
cd ~/projects
RBENV_VERSION=2.1.8 rails _3.2.13_ new Project2
echo 2.1.8 > Project2/.ruby-version
You could use rbenv shell to set the variable, but I only recommend that if you don't want rbenv to auto-switch based on .ruby-version files for the duration of that shell. It's very easy to forget that you have the variable set, and when you cd to a different project, it won't be using the version you expect.
There's a good recent post on exactly the topic of gemsets / bundler here http://rakeroutes.com/blog/how-to-use-bundler-instead-of-rvm-gemsets/ Good background you can apply to your rbenv setup.
There is my question: I wonder why using a RVM .gems (see http://beginrescueend.com/gemsets/initial/ to know what I'm tlaking about) in a Rails app while we use Gemfile to install our gems in our project?
I think that could be useful when deploying a project for the first time and ensure (eg.) bundler is installed before running (automatically?) a command like bundle install through the .rvmrc file.
I am right? Is there any use case I am missing?
In short, I want to know what is the interest of *.gems file?
Thanks in advance for all your help that will make me learn a lot ;)
Using .gems and Gemfile is rather explicit, You need only one of them, .gems file is more useful for small projects or even for your preferred gems, mostly when there is not much dependencies, in contrary Gemfile brings strict dependency management ensuring you will always get proper versions of gems (assuming Gemfile.lock is also used)
There is good support for generating .rvmrc in development version of rvm, it will detect if you have *.gems or Gemfile and include proper code like bundle install in .rvmrc:
rvm get head
cd /path/to/project
rvm --rvmrc --create --install 1.9.3#project
Review the new generated .rvmrc file and remove the parts that are not important for you.