/usr/bin/env: ruby: No such file or directory - ruby

I use Ubuntu 14.04 and I have installed ruby with rbenv months ago. Everything was working fine but I believed I messed with $PATH doing other stuff and now it says /usr/bin/env: ruby: No such file or directory every time I try to use a command related to ruby.
rbenv versions
2.2.3
echo $PATH
/home/rafael/.rbenv/shims:/home/rafael/.rbenv/bin:/home/rafael/.rbenv/shims:/home/rafael/.rbenv/bin:/home/rafael/.rbenv/shims:/home/rafael/.rbenv/bin:/usr/local/heroku/bin:/home/rafael/.rbenv/plugins/ruby-build/bin:/home/rafael/.rbenv/shims:/home/rafael/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/rafael/.rvm/bin:/home/rafael/.rvm/bin
It is possible to find ruby 2.2.3 inside rbenv versions but it is like it is not installed. I am thinking of deleting everything and reinstall ruby. But I want to believe that there is a simpler solution that I am not seeing. Help, please.

It's one of 2 things
The path that crontab uses doesn't have your Ruby in it. This is usually because the "safe" path that cron uses doesn't include /usr/local/bin
Cron isn't finding the rbenv paths on the server.
You can set the path explicitly in your crontab and see if that fixes it in either case.

Related

Installed gems are not running

I am trying to install jekyll.
I run gem install bundler jekyll
After installing gems I run bundle init and get
bash: bundle: command not found
How can I fix it?
You can get a gem's directory using gem which. For example:
$ gem which jekyll
/home/username/.rvm/gems/ruby-2.6.4/gems/jekyll-4.0.0/lib/jekyll.rb
Then append the directory to your PATH:
$ export PATH="$PATH:/home/username/.rvm/gems/ruby-2.6.4:"
The reason why you are getting "command not found" after installing gems is because they were installed in a location that your bash shell does not yet know about. The way the computer looks up commands is by looking at the PATH, which is a list of folders where the computer should look for commands, such as bundle. The previous answer is on the right track, but unfortunately, the gem which command will only tell you about locations that are already in your PATH, which might not include the folder where bundler and jekyll were installed.
The location of the gems depends on how you installed Ruby, so without knowing that, I can't tell you what to put in your PATH. What I can tell you is that what you are experiencing is unfortunately very common, but there is a fix. To avoid needless frustration and to help people like you, I wrote a script that will automatically set up a proper Ruby environment for you, including updating your PATH and everything else that is necessary to be able to install gems and use them right away without getting any errors. Check out the links at the bottom of this answer to learn more about my script.
In the meantime, I can make some guesses and see if I can help. If you installed Ruby with Homebrew, then this should fix it:
Run this command:
echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7.0/bin:$PATH"' >> ~/.bash_profile
And then quit and restart your terminal. Now you should be able to run bundle init.
If you are reading this and you are not the original poster, you might need to replace .bash_profile in the command above with .zshrc depending on which shell you are using. You can tell by looking at the error. If you are using zsh, it will say zsh: bundle: command not found.
You can read more about my script and other ways to fix the "command not found" error in these articles:
https://www.moncefbelyamani.com/troubleshooting-command-not-found-in-the-terminal/
https://www.moncefbelyamani.com/how-to-install-jekyll-on-a-mac-the-easy-way/
https://www.moncefbelyamani.com/the-definitive-guide-to-installing-ruby-gems-on-a-mac/

Upgrading Git Bash to run newly downloaded ruby 2.0.0

I'm on a Windows machine, so unfortunately I can't use RVM, which would make this super easy.
I previously downloaded Rails and Ruby on a new Windows machine. Rails 4.0.3 and Ruby 1.9.3. For some reason, the package I installed didn't install the new version of ruby. So I just went to http://rubyinstaller.org/downloads/ and downloaded Ruby 2.0.0. If I browse to my Apps to access the Start Command Prompt with Ruby, the version is 2.0.0 (ruby -v). But I use Git Bash, http://git-scm.com/downloads, as my Command Line. Right now, the current ruby version is still 1.9.3 in my Git Bash window. How do I update it to use the newly downloaded Ruby 2.0.0???
Thanks for the help.
Issuing
$ which ruby
will tell you which of the two ruby executables GIT Bash wants to use.
For situations where it is necessary to have two versions of Ruby, it's possible to select one or other for general use using the PATH environment variable.
The order of paths in the Bash $PATH environment variable is important - if the path for your ruby 1.9.3 executable appears before the path for your ruby 2.0.0 executable, then the interpreter will use the 1.9.3. So, for example;
Ruby 1.9.3 is in /c/Software/Ruby/1.9.3/ruby.exe
Ruby 2.0.0 is in /c/Program Files/Ruby/2.0.0/ruby.exe
And your PATH variable is as follows;
$ echo $PATH
/c/GIT/bin:.:/c/Software/Ruby/1.9.3/:/c/Program Files/Ruby/2.0.0/
Then you would need to re-order your PATH variable so that the 2.0.0 path comes before the 1.9.3 path. Find your .bashrc file (by default in your home directory) and examine any PATH definitions, e.g.;
PATH=$PATH:/c/Program Files/Ruby/2.0.0/
And modify so that your 2.0.0 path has precedence
PATH=/c/Program Files/Ruby/2.0.0/:$PATH
You can issue this command on the command line also, making sure to do
$ export $PATH
once you've made your changes. Otherwise you'll need to source .bashrc or start a new shell. GIT Bash should then pick up the correct executable.
An alternative is to create aliases or symbolic links for each executable which specifies their version, such that typing ;
$ ruby193
Executes the 1.9.3 ruby and
$ ruby200
executes the 2.0.0 version.
Aliasing is as follows;
$ alias ruby193=/c/Software/Ruby/1.9.3/ruby.exe
Linking is as follows;
$ ln -s /c/Software/Ruby/1.9.3/ruby.exe /c/GIT/bin/ruby193
Aliases you use frequently should be put in .bashrc .

How to load the RVM part of user's .bashrc to run Ruby CGI scripts under Apache?

I've configured a new server on Ubuntu 12.04 and I started to use RVM.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
!#/usr/bin/ruby
to
!#/usr/bin/env ruby
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment (last lines of .bashrc) was not loaded under Apache.
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
sudo ln -s /home/apache_user/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby
But I want to know if there's a better solution, because I guess that rvm --default use is better than sudo ln -s.
I am thinking about:
loading the RVM Environment on startup (but I don't know how to achieve that);
loading the RVM Environment for each web request (which can degrade performance, and I don't know how to configure Apache to do that);
maybe the RVM Environment is loaded and all I must do is to guess the name of relevant variables to pass with PassEnv directive. But I doubt that. (Why Apache would run the .bashrc instead of another shell like csh or ksh?)
you can source the ruby environment, I'm not sure if it's enough to source it in $HOME for apache or if you need to modify /etc/init.d/apache2, but the line is:
source /path/to/rvm/environments/<name>
where for <name> you can either use full ruby name or an alias name
You can create aliases with:
rvm alias create veve 1.9.3-p125#my-project
which for RVM installed in /home/app/.rvm will allow you to use:
source /home/app/.rvm/environments/veve
in .bashrc or /etc/init.d/apache2 (just near top, bellow shebang).
you could always do
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby
That will link the default version of the rvm-managed Ruby to /usr/bin/ruby and you will never have to do anything. set it and forget it.

Can't get ruby 1.9.3 installed on OSX Mountain Lion

I have home brew installed and rbenv. I've followed these instructions:
http://octopress.org/docs/setup/rbenv/
Everything seems to work flawlessly but when I type
ruby -v
it still says 1.8.7...
Not sure what I'm doing wrong.
Unix will run the copy of Ruby that it finds first in your search path. Type these commands in Terminal:
echo $PATH
which ruby
The first will show you the order in which Unix is looking for the ruby command. The second will tell you which ruby it's finding. You may need to edit the PATH= command in your .bash_profile or .bashrc to change the order of the search path, e.g.:
PATH=/usr/local/bin:/usr/local/sbin:$PATH
prepends /usr/local/bin and /usr/local/sbin to your existing PATH to tell Unix to search your /usr/local/bin directory before /usr/bin
I have tried MacPorts and Homebrew many times the last couple of years. When it comes to ruby no other system can match RVM.
There are numerous guides online. Google, pick one and go.
NOTE: Octopress bloging system, comes with an rvm-ready bundle which will install and use ruby-1.9.2x, last time I checked :-)

Installing Ruby on Mac 1.9.2, still getting 1.8.7 even though path changed

I'm on a Mac running 10.6.4 Snow Leopard, and apparently ruby comes ready to go. But I'm new to Ruby, trying to learn the Rails framework, and so i decided to install the latest version 1.9.2. I followed the instructions here, but after I compile and install, when I run ruby -v I'm still getting 1.8.7. Anyone can help a noob out?
When I use which ruby I am getting usr/local/bin/ruby, so the path has changed and is correct.
UPDATE:
It seems I was having issues because I was using two login files to set my path (.bash_login and .profile). You can only use one, and the first one that exists and is readable will be used. I eventually switched to RVM and used .bash_login to load RVM into shell.
Well, the sw isn't lying to you. So something is not as you expect.
Try /usr/local/bin/ruby -v and see what version it is.
Added: Also, try locate ruby|more to see where the ruby files are on your system.
You may need to change your path to use your newly installed copy of ruby.
Added more: did you add the Path to your ~/.profile file as the instructions tell you to do in step 1?
Did you restart your terminal session after changing the ~/.profile file? (Quit and restart terminal.)

Resources