Error on Rails S | Rails Server - ruby-on-rails

I'm a newbie in rails. I installed Rails(4) and Ruby using RVM.
I get an error whenever I run rails s or rails server on a different terminal.
It says
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
It only works on the first terminal I used to install rails on.
The left terminal is the one where i installed rails on

It looks like you havent added the rvm path in bashrc or bash_profile
/home/username/.bashrc
/home/username/.bash_profile
Add
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
and
source .bashrc

Your terminal likely isn't set to be a login terminal, so RVM isn't being setup correctly on new terminal instances.
See https://rvm.io/integration/gnome-terminal for how to configure your terminal as a login terminal.

Are you using gemsets? You can think of each gemset as an independent, compartmentalized "environment" that houses your rails installations.
If you've already installed rails under a gemset, are you making sure your new terminal window's environment is using the gemset that you installed rails on? When you open a new terminal window, it won't know which gemset to use, unless you configure it to use some default gemset. You can use the gemset you created by doing:
$ rvm use <YOUR_GEMSETNAME>
If you don't know/remember what gemsets you created, you can list them with the following command. Try using this command on the terminal that worked. It'll indicate which gemset you're using (then you can use that gemset name with the use command above):
$ rvm gemset list_all
This Rails tutorial does a good job of giving step by step instructions on installing ruby and rails with rvm properly. Just remember that with every new terminal that you open, you have to make sure you're using that gemset.

Related

Ruby on ubuntu 12.04

Yesterday I installed ruby and rails using rvm in ubuntu 12.04 and it is working fine and also created a sample application.But today when i run
ruby -v
it is showing like
The program 'ruby' can be found in the following packages:
ruby1.8
ruby1.9.1
Try: apt-get install
You probably forgot the latest step when installing RVM. It is mentioned at the end of the install process but easily overlooked. It boils down to this:
Make sure you have the following lines at the bottom of ~/.bashrc:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
source "$HOME/.rvm/scripts/rvm"
This will load RVM right after you open a new shell. Without it, RVM is not 'activated' and when you type ruby it will use the version installed through your OS package manager and not the version installed through RVM.
Run rvm list. If it shows rubies then everything is ok and go to step 2.(if any problems then try reinstall rvm)(if it shows empty rubies list, then install ruby using rvm again(rvm install 2.1.1)
Specify current rvm ruby by one of the following ways:
Specify ruby version manually by running rvm use 2.1.1 for ruby2.1.1. This way you have to call this command every time you open a terminal (see next ways if this doesn't suit you)
Set default ruby version. See here.
Use .rvmrc file in any directory(see here how) to make rvm change ruby version when you open this directory.
Probably that is your case.
In addition to zwipple's answer, if you can also load rvm for once using following command:
source ~/.rvm/scripts/rvm
ruby -v
=> #your ruby version

Install rails from gem

I installed ruby and next rails as a gem with this command
gem install rails -include-dependencies
It installed successfully but even after restarting my command window i couldnt run commands like:
rails
or
rails s
My question is what do i have to add to my path so that rails is recognized in my command windwo?
----Update----
I ran gem list but rails isnt listed,
so i will reune the gem install rails command
If you are using rvm you need to use the gemset of RVM in which you have installed your rails gem.
cmd for the same is
rvm gemset list
will list the gemset persent in your version manager, later select the gemset with following command:
rvm gemset use [gemset_name]
hope am redirecting you in the right direction.
Try creating a project with:
rails new whatever
Go into the project:
cd whatever
Install dependencies:
bundle install
Start server:
rails s
If you use a unix based OS, you may need to get the path of the rails executable into your $PATH e.g. by e.g. exec $SHELL -l.
Apologies if you know about this already, but why don't you use a pre-packaged installer like RailsInstaller?
Are you using rbenv?
If so, try running this in your terminal:
rbenv rehash
Since you are using windows, you might not have added Ruby's bin directory in path :
To add path in windows :
Right click My Computer >> properties >> Adavced System Settings
Then edit the path variable in User Variable and just add the path of your installed Ruby's bin directory.
Open a new command prompt to get the path changes.

The program 'rails' can be found in the following packages - issue with some app and RVM?

I just downloaded an app code from github and when I am trying to run the command rails s, I am getting this error -
The program 'rails' can be found in the following packages:
* rails
* ruby-railties-3.2
This is specific to this app only, can someone tell me how to fix this ? I am using RVM
In case anyone else has this problem, but the above doesn't work. Try running:
/bin/bash --login
then run
rvm use 2.0.0 #or whatever your version number is...
Not really sure what exactly your problem is. If you use rvm and have installed Rails with it, you may have to use
rvm use 1.9.3
(version number may depend on your installation). Then you may or may have not a gemset with this. So:
bundle install
should install Rails gem in this gemset. Maybe use
rvmsudo bundle install
or just manually install the Rails gem. But this would partially depend on the Rails version expected by the app. There are other options, but this should mostly work.
For setting a version permanently you can use:
rvm use 1.9.3 --default
For this to work you must have something like this:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
in your ~/.bash_profile.
In addition you can have something like a .rvm folder in your project where you can set rvm options on a project level if you need to switch between versins often (didn't use this myself, but there should be docs on the rvm homepage)
I have use /bin/bash --login for my ubuntu on raspberry pi after getting the same error. It's working right now.

Why am I seeing reference to a gemset of another project in my current project?

I recently created a new Rails project, and I believe I created a gemset for it before generating the app.
But when I encounter an error I get a message like this:
File /.rvm/gems/ruby-1.9.3-p194#project_1/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb
Even though I am in project_2. So in theory that should be ruby-1.9.3-p194#project_2 and not project_1.
What can be causing this and how can I fix it?
Thanks.
P.S. I am using RVM.
Edit 1:
I ran rvm gemset list and this is the output:
project_2$ rvm gemset list
gemsets for ruby-1.9.3-p194 (found in /Users/marcamillion/.rvm/gems/ruby-1.9.3-p194)
project_1
=> project_2
project_3
Edit 2:
I just ran gem install rails in my project_2 directory after I created the .rvmrc file for that project after verifying that it is indeed using the gemset for that project specifically. However, when I generate the error again, I am still seeing a reference to the gemset in project_1.
Edit 3:
The output for rvm current:
$ rvm current
ruby-1.9.3-p194#project_2
Edit 4:
Although, when I run rvm current in the same terminal window as my rails s I see:
$ rvm current
ruby-1.9.3-p194#project_1
So how do I set the gemset across all terminal windows for right now, and in the future?
You need to tell RVM which gemset to use. Try this:
rvm gemset use project_2
If that works, then you may wish to create a .rvmrc file in project_2's root directory so that it automatically switches to that gemset when you enter the directory:
cd /path/to/project_2
rvm --rvmrc --create 1.9.3#project_2
That'll create a .rvmrc in /path/to/project_2. cd out of the directory and back in, and it'll prompt you asking if you trust the .rvmrc file. By default, it'll only ask you the first time.
Edit:
It is important to note that when creating a new .rvmrc, you need to cd out of the project directory in all open terminal sessions (including rails s and rails console). Otherwise the app will continue using the gemset from project_1.

RVM can not change gemset

Before I installed MySQL and restart my computer, my RVM works well.
If I type
rvm gemset use rails
It will show:
Using ruby-1.9.3-p194 with gemset rails
and then I type:
rvm gemset name
It shows:
rails
which is correct.
However, after I restart my computer, something strange happens.
Firstly, the system cannot find the command 'rvm', so I modified by ~/.bash_profile:
export PATH=/usr/local/mysql/bin:/Users/hanxu/.rvm/bin/:$PATH
Above is the content of my .bash_profile
Then rvm works.
Then I type:
rvm gemset use rails
It seems running well and shows:
Using ruby-1.9.3-p194 with gemset rails
However, when I examine it by asking rvm gemset name, it turns to be:
/Users/hanxu/.rvm/gems/ruby-1.9.3-p194
which is my default gemset, rather than "rails".
No matter how I set gemset, it always change to the default setting.
Can anyone tell me what's the problam?
Are you using an .rvmrc file?
https://rvm.io/workflow/rvmrc/
Your RVM installation is most likely incomplete. Look for this string in your .profile / .bashrc / .zshrc or whatever else you might be using
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
This command checks for existence of $HOME/.rvm/scripts/rvm and, if found, loads it into the shell. This effectively loads the RVM. So, if you don't find this command, add it and open a new terminal window, RVM should be there.
rvm has a command to fix sourcing:
rvm get stable --auto
the auto switch will update your *rc files, then it should be enough to open new terminal and it should be all fine.
Use this command to switch gemset.
rvm use <ruby version>#<gemset name> --create
This command will switch rvm to given gemset and create it, if it does not exist.
In my case, I needed to add the user to the rvm group before I could use rvm.

Resources