How to start rails console on my ubuntu server - ruby-on-rails

I'm debugging an issue with a rails app on my ubuntu server and i'm trying to start the rails console to run a command, but this is what happens when I try to start the console:
myuser#rails-server1:/var/www/myapp/current$ rails c
The program 'rails' can be found in the following packages:
* ruby-railties-3.2
* ruby-railties-4.0
Try: sudo apt-get install <selected package>
Rails is definitely installed because my app is running and the gem is installed in this dir:
/var/www/myapp/shared/bundle/ruby/2.0.0/gems
So how can i actually start the rails console?

bundle exec to the rescue:
$ bundle exec rails console

I had this same issue once.
In my case it had to do with RVM.
Add this to your ~/.bashrc
# This loads RVM into a shell session.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
This loads rvm each time you load a new terminal session.

looks likes rails isn't in your path, so you will need to specify a path to it (and generally speaking . isn't in your path on any sanely configured *nix box)
so try this in /var/www/myapp/current
RAILS_ENV=production ./bin/rails console
That should get you a console using the version of rails that was installed in your bundle and get you into the correct environment.

First, let's discover how did you install Ruby: by apt-get, rvm or rbenv:
open a shell and type this
# type rbenv | head -1
# type rvm | head -1
each command will return a "xxx is a function" or a "-bash: type: xxx: not found" (where "xxx" is "rbenv" or "rvm").
"xxx is a function" means that you had installed via xxx method.
If both commands returns "xxx: not found", then you had installed via apt-get and you will need to re-install via rbenv or rvm.
Now that you know what manager you had use, let's try to fix the problem.
If you installed via RVM, try this:
# \curl -sSL https://get.rvm.io | bash -s -- --ignore-dotfiles
# echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
If you installed via RBENV, try this:
# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
Close the terminal, re-open and try again your "rails c"
PS: If nothing of this works, try to install another manager (if you are using RVM, try to install via RBENV, and vice-versa).

Better would be to use bundler when running rails specific command as
"bundle exec RAILS_TASK" like in these case "bundle exec rails s". When using bundler, it will find executable file in their GEM_PATH which is set during installation of ruby.

Related

rvm command not found

I installed rvm with rails, from the official website of RVM, I specified the command suggested by the tutorial.
When the installation my system has rails 4.0.0, rvm 01/23/12, ruby 2.0.0, bundler gem 1.3.5 and 2.0.3
But I need to install ruby 1.9 to practice with a course that I am currently doing. "Rails for Zombies 2"
I try to execute the instruction rvm install 1.9.3 and the console returns this message:
rvm: command not found
Possible duplicate of: Ubuntu rvm setup issue
Your problem is that RVM is not loaded when you open a new terminal.
To solve this, run this command line: (if using login-shell)
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
Or this (if using non-login shell):
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bashrc
Or if you are using zsh (and/or oh-my-zsh):
echo "source $HOME/.rvm/scripts/rvm" >> ~/.zshrc
This will add the path to RVM to load at each Terminal instantiation. You must either close and reopen your terminals or simply call source ~/.bashrc (or ~/.bash_profile or ~/.zshrc).
I had this problem after installing zsh. I'm a domain user so my $PATH and $HOME are not as straight-forward. What worked for me was
echo "source /usr/share/rvm/scripts/rvm" >> ~/.zshrc
The latest installation needs the users to be added to the Group rvm and then need to login again.
Please note that closing the terminal and reopening is not enough; the user has to logout and log back to take the Group addition in effect.
Adding the user to the Group can be done by:
sudo usermod -a -G rvm <user>
The binaries can also be at different locations based on the method you followed during the installation.
I had them at /usr/share/rvm/
You can also look at /usr/local/rvm/scripts/rvm
Then you add this line to the end of ~/.bashrc
[[ -s /usr/share/rvm/scripts/rvm ]] && source /usr/share/rvm/scripts/rvm
Note: If you are using a shell other than bash you may need to add the path accordingly.
For instance, if you using zsh shell add the above lines to the ~/.zshrc file.

RVM switching to #global gemset instead of a specific one

When I enter my rails dir on server RVM switch to
/home/capistrano/.rvm/gems/ruby-1.9.2-p290#global/bin/bundle
although it should use ruby-1.9.2-p290#mygemset/bin/bundle as it works on my local machine. When I type rvm use 1.9.2#mygemset it works. Nginx server is also looking for gems in the #global gemset
my .rvmrc file:
environment_id="ruby-1.9.2-p290#mygemset"
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
then
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
if ! rvm --create use "$environment_id"
then
echo "Failed to create RVM environment '${environment_id}'."
return 1
fi
fi
I'm not sure if I understood you but you could place an .rvmc file in your rails folder and put something like this in it.
rvm use ruby-1.9.2#mygemset
Whenever you cd into that directory rvm will use settings from the .rvmc file.
RVM is going through rapid develpment so the syntax and functionality of commands sometimes vary. Anyway, I solved it with upgrading to stable RVM:
rvm get stable
which is now rvm 1.14.1 (stable). Afterwards commands like rvm 1.9.3-p194#mygemset --create
works perfectly.
Another thing I was missing is the new syntax for executing commands:
rvm #mygemset do bundle install
This will install all gems for given gemset and ruby specified in .rvmrc file

rvm not defaulting to newer version of ruby

I am using ruby on rails on Ubuntu 11.10. Ruby 1.8.7 works fine but I cannot get rvm to work with 1.9.3 or any other version.
When I run rvm reload, rvm list or rvm info, I get the following message:
bash: /usr/bin/rvm: No such file or directory
I have Ruby 1.9.3 installed via rvm and when I change the default it doesn't give me an error. But then when I check the ruby -v it defaults back to 1.8.7.
Also, when I check which ruby it shows the following:
/usr/local/bin/ruby
My guess is that it has something to do with the bash file. What do you suggest?
I am guessing that it isn't even installing it correctly.
This is what happens:
I type the following into the prompt:
bash < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
And it outputs:
ERROR: Unable to checkout branch .
Are you following the installation instructions from the rvm site?
http://beginrescueend.com/rvm/install/
If you are then look at section 2 about the shell again.
specifically this command that they tell you to run :-)
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
remember to reload or restart your shell after running that command.
run the command
source /usr/local/rvm/scripts/rvm
and then
type rvm | head -1
you should get output -
rvm is a function.
And after that run
rvm use rvm use 2.0.0 --default

Rails on Linux Mint 11

I have installed ruby via rvm on Mint 11 no problem. Installed gems, ditto. Installed rails via gem install rails, and when I type rails -v it returns the correct version. Until I close that terminal and reopen a terminal. When I do that and type rails -v I get the message
The program 'rails' is currently not installed. You can install it by typing: sudo apt-get install rails
If I then type
rvm use 1.9.2-p180 --default
and then type rails -v I again get the correct version...until I close the terminal.
I should add that I have added a path statement to my .bashrc pointing to the 1.9.2-p180 directory in my .rvm directory.
Typing ruby -v always returns the correct version.
Create .bashrc file and add .rvm command
$ sudo touch ~/.bashrc
$ sudo gedit ~/.bashrc
(Add line to .bashrc file)
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then source "$HOME/.rvm/scripts/rvm" ; fi
Logout and Login OR just update user profile from .bashrc with following command
$ . ~/.bashrc
Try adding this command to your .profile and reopening your shell:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
Use project rvmrc files. You can see how to set this up here http://beginrescueend.com/rvm/best-practices/
That way you keep all your gems seperate for each project and it's dead simple to set up
Try which rails. Maybe there is a link to a stub that gives you the note.
If that is the case calling the full path might help, e.g. /usr/local/bin/rails

problem doing bundle install on ubuntu

my situation is as follows:
I installed rvm as a root user on Ubuntu 10.04. As a root user I then installed ruby 1.9.2 and rails 3.0.1 using rvm at location '/rvm/gems/ruby1.9.2-p0/'. Then I logged in as myself on ubuntu and tried creating a rails application in /home/myself/www/myapp. When I do 'bundle install' I get the following error:
'Permission Denied' - home/myself/www/myapp/Gemfile.lock (Errno: EAccess) from /usr/local/rvm/gems/ruby1.9.2-p0/gems/bundler-1.0.7/lib/bundler/definition.rb
I am also not able to do sudo bundle install.
Moreover, when I log back in as root and try to do bundle install from there...I get error:
bash: bundle: command not found.
I am very lost now. How can I get this to work???
May you should check the permissions of your rails app folder, review the owner and group because I had the same problem and I has solved with:
chown -R USER railsapp_folder
Good luck!
When you logged in as root, did you activate rvm using
rvm use 1.9.2
EDIT:
How about (logged in as root)
rvm use 1.9.2
cd /home/myself/www/myapp
bundle install
rvmsudo runs as sudo in the current environment and from the path it was called from.
You need to make sure you've added:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
into your ~/.bashrc as per the instructions here (see "Post Install").
type rvm | head -1 should return rvm is a function if you've done it correctly.
I highly recommend starting over and installing RVM under your own account. This tutorial is perfect.

Resources