Rails app cant detect local gems - ruby-on-rails

This is a very odd one and I'm not sure how to proceed.
Basically, I am able to install gems and use them normally within any folder except those created with rails. It's very very odd. It's difficult to explain so maybe this console output will explain
passenger#rubyserve:~/railsapps$ ls
app test_application testing
passenger#rubyserve:~/railsapps$ rails -v
Rails 3.2.2
passenger#rubyserve:~/railsapps$
passenger#rubyserve:~/railsapps$
passenger#rubyserve:~/railsapps$ cd app/
passenger#rubyserve:~/railsapps/app$ ls -a
. app config.ru doc Gemfile.lock lib public README.rdoc test vendor
.. config db Gemfile .gitignore log Rakefile script tmp
passenger#rubyserve:~/railsapps/app$
passenger#rubyserve:~/railsapps/app$
passenger#rubyserve:~/railsapps/app$ rails -v
The program 'rails' is currently not installed. To run 'rails' please ask your administrator to install the package 'rails'
passenger#rubyserve:~/railsapps/app$
It makes bugger all sense. When outside my rails folder, I can call rails to make a new application but once within, rails isn't detected. This is the same for all gems including bundler.
My first thought was that somehow, rails had some specific gem folder hidden away, so I done my gem install from within the rails project, however this didn't work either.
Any thoughts?
My setup is ubuntu server 10.10 (I know, i know)
Ruby 1.9.2
gem 1.8.17
and I'm using the latest rvm from wayneeseguin.enter code here

This type of problem some time occurs i think u can save a lot of time by installing and using RVM
After installing RVM create gemset directory every time whenever you create separate project. e.g you create application blog first you create gemset directory with giving this command
rvm gemset create blog
then you should use this gemset directory and also ruby version as well e.g you have install ruby 1.9.2 then use this command
rvm use ruby-1.9.2#blog
after that go into directory of blog application and install this gem
gem install bundler
and also then bundle install. so all local gem will be installed into this blog directory under rvm and there will be no conflicts will be appear.But keep one thing in mind if your platform is ubuntu then this will work

Related

Does Rails need to be installed as a system gem?

In every setup describing a configuring an environment with rbenv and Bundler, the instructions are always to install bundle as a system gem, using gem install bundler. Often, they'll also recommend rbenv-bundler rbenv plugin, but the maintainers of rbenv discourage this.
What's not described is how to install Rails. Initializing a new Rails project creates a basic Gemfile for bundler. However, in order to initialize a Rails project, one needs to have Rails installed. It seems weird and even wrong to make a directory, write a basic Gemfile that includes Rails, run bundle install, and then initialize Rails to the current directory. In fact, I doubt that would even work well, if it worked at all.
So, does Rails need to be installed as a system gem with gem install rails? If so, how does one manage multiple versions of Rails, particularly with rbenv?
It totally makes sense to NOT install rails as system gem.
Without messing up rbenv or other ruby version manager you use, below are brief steps to create (initialize) a new Rails app from a directory with a Gemfile:
mkdir rails_app
cd rails_app
vi Gemfile # Edit it to include a rails version you need
bundle --path vendor # Wait for bundler to finish
bundle exec rails new ./
The last step would ask: Overwrite /path/to/rails_app/Gemfile? (enter "h" for help) [Ynaqdh]. Input y to get the default Rails Gemfile content.
Note: the above steps specify the local vendor directory (inside the rails app folder) to avoid installing gems to system global scope.
Answer is no, you don't install rails as system gem. Create a project folder, add .ruby-version file and add the ruby version you would like in this file i.e. 2.3.0. rbenv uses the version specified in this file and it won't be system's ruby.
Now you can do gem install bundler from this directory and create Gemfile and add your rails version. Now run bundle install and roll it on the tracks of RAILS.....
Force rails to vendor gems.
$ mkdir foo
$ cd foo
$ bundle config --local path vendor
$ rails new .

why is my rails version dependent on what directory I'm on

I'm trying to figure what version of rails I got installed.. If I run on it on my home directory:
cd ~
rails -v
I get
Rails 3.2.13
However if I run it on the home directory of a ruby on rails project I just cloned:
cd /path/to/RoR/project
rails -v
I get
git://github.com/seyhunak/twitter-bootstrap-rails.git (at bootstrap3) is not checked out.
Please run `bundle install`
I'm confused.. why would it point out a missing bootstrap gem when I'm only looking for the rails version?
Rails version is declared inside your Gemfile. If you navigate to folder containing Gemfile, RVM automatically switches to rails version specified in this folder (it does the same for Ruby version as well!).
Since you didn't run bundler, Gemfile.lock file is not valid and RVM cannot be sure it contains the right version of rails (some dependencies may require rails to be updated - for bundler rails is just a gem as any other). THat's why it asks you to run bundle.
Hope it makes sense. :)

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.

Calling 'rails new demo' created a folder named "new"?

I installed Ruby and Rails using apt-get in Ubuntu. Then when I test my installation, this thing happen.
When I call rails server inside a rails-created-folder, rails created a new folder called "server" for me, with a correct folders structure, including controller folder, app folder, gemlock file, etc.
How could it possibly happen? I will try reinstalling RoR, but has anyone encountered this?
Last time, I used RVM, but whenever I create a new app, I will have to waith for rails to redownload all the bundle file, but in this installation, I don't have to. Can you help me explaining it?
Thank you and best regards
As pointed out in the comments, it sounds like your rails executable is rails 2.
Try gem uninstall rails, select all versions.
Run rails -v. If this command works you've got a system version of rails that isn't being handled by RVM. BTW, this is why many rails devs are shifting from RVM toward rbenv + bundler.
If you still have rails after gem uninstall, run sudo gem uninstall rails. On RVM, sudo gets to your system gems. You might want to sudo gem uninstall everything, so you don't have this conflict in the future.
gem install rails, you should get version 3.2.8.
Try rails new my_app again. It should work. If this doesn't work, try the following:
Create a parent directory for your rails projects, like ~/rails. Then create a GEMFILE that looks like this:
source :rubygems
gem 'rails', '~>3.2.8'
Then inside ~/rails run bundle exec rails new app_name.
If that doesn't work... you've got a bigger system config problem of some kind I guess.

rbenv: Surviving without gemsets

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.

Resources