I need to test for the correct Ruby version before running the script. In project we have file .ruby_version that has number of required version. For example .ruby_version file has:
2.5.5
I could get current user version of Ruby in user using ruby --version, but this command return more information that i needed. For example:
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin19]
So my check doesn't work:
required_ruby=$(cat .ruby-version)
current_ruby=`ruby --version`
if [ "$required_ruby" != "$current_ruby" ]; then
echo "This project uses Ruby version $required_ruby, try to checkout or install it"
fi
Any idea how to fix it?
There are two special constants RUBY_VERSION and RUBY_PATCH_LEVEL available in the Ruby interpreter, so you can call the ruby command and let it give your desired information.
$ /usr/bin/ruby -e 'puts RUBY_VERSION'
2.5.5
Related
I'm getting this error message when I try to do a pod install on Mac on a Turbo Module in React Native. I believe it's because of my version of Xcode installed.
> RCT_NEW_ARCH_ENABLED=1 bundle exec pod install
Your Ruby version is 3.1.2, but your Gemfile specified 2.6.8
When I run ruby -v it returns 2.6.8.
> ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin21]
I'm not sure what to do to fix this or where it's finding 3.1.2.
2.6.8 is the builtin ruby in macOS Monterey, which can be found at /usr/bin/ruby. If bundle is finding 3.1.2 then you have another version of ruby installed (possibly via homebrew?). You can see all versions of ruby on your current PATH using the command which -a. The order of results is the order in which they are found on the PATH, as you can confirm by typing echo $PATH. Execution is on a first-come-first-served basis. On my machine the which command produces:
% which -a ruby
/opt/homebrew/opt/ruby/bin/ruby
/usr/bin/ruby
showing that I have a homebrew copy of ruby first on my PATH, followed by the macOS copy.
Similarly, you can check which installation of bundle is being used:
% which -a bundle
/opt/homebrew/opt/ruby/bin/bundle
/opt/homebrew/lib/ruby/gems/3.1.0/bin/bundle
/usr/bin/bundle
You can override the PATH ordering by editing .zshrc (long-term change); by explicitly setting it to a new set of path values in the current terminal (one-time change for the active terminal window); or by typing the fully qualified path name of the version of the command you wish to run (ex: /usr/bin/bundle ... to use the system version).
I'm not sure what Ruby version is being used on my device (macOS) by default.
ruby -v outputs ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
brew list ruby --versions outputs ruby 3.0.2
rbenv versions outputs
system
* 2.7.4 (set by /Users/nlakritz/.rbenv/version)
If ruby -v shows 2.6.3, then you are using the ruby which ships with the system by default. You can confirm this in several ways. Running the shell command which ruby will show /usr/bin/ruby. Checking your PATH will show /usr/bin earlier than the location of the homebrew or rbenv installations.
If you wish to run one of the others, you can put it earlier on the PATH or invoke it explicitly by giving the fully qualified name such as /usr/local/opt/ruby/bin/ruby myscript.rb. Another alternative which avoids twiddling the PATH variable is to use a shebang line at the beginning of different scripts pointing explicitly to the version to use with that script.
It depends what you mean by 'default'. If you're running a file with ruby filename then the version you are using is 2.6 (because you're running the same Ruby as ruby --version).
If you're running a file directly (eg. ./filename.rb) then it depends on the shebang line in the file. For example, the file could start with the line #!/usr/bin/ruby, so to check which version of ruby that file is running on, you could call /usr/bin/ruby --version.
Hopefully that helps, but I'm not 100% sure I understood what you're asking - so let me know :)
Try this:
$ which ruby
/usr/bin/ruby
$ irb
irb(main):001:0> RUBY_VERSION
=> "2.7.0"
So, rbenv is ignored, I think you're missing this in your path:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
ruby -v # should say 2.7 now
And if it says 2.7, you can add the export and eval commands at the bottom of your ~/.bashrc to load it every time you open a shell, thus kicking off rbenv override.
I would bet 2.6 for you, ie ruby -v.
I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH
I have spent literally days trying to install ruby 1.9.2 and get it working with gems :-/ I eventually gave up on my Mac OSX 10.6 machine and below is the current state on my Ubuntu machine. Any advice would be greatly appreciated!
# ruby test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mongo (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:1:in `<main>'
# cat test.rb
require 'mongo'
db = Mongo::Connection.new.db("mydb")
# gem which mongo
/usr/local/rvm/gems/ruby-1.9.2-p0/gems/mongo-1.1.2/lib/mongo.rb
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
According to this page: http://docs.rubygems.org/read/chapter/19
I symlinked which ruby I was using to match that which gem is using:
# which ruby
/usr/local/rvm/bin/ruby
# ls -l `which ruby`
lrwxrwxrwx 1 root root 44 2010-11-17 13:25 /usr/local/rvm/bin/ruby -> /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.2-p0/bin/ruby
# which gem
/usr/local/rvm/bin/gem
# gem -v
1.3.7
# ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
Try putting the following line at the beginning
require "rubygems"
Why is "rvm" displaying in your /usr/local/rvm/ path? Did you do a system-wide install, as a system administrator using administering Ruby system wide for multiple users?
Did you add [[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm' to your ~/.bashrc, ~/.bash_profile or ~/.profile (whichever you have configured)?
For normal, every day use, I recommend RVM's default setup:
RVM installation, RVM gems management.
Note to self: Buy stock in RVM. It's too cool.
Does it work under Ruby 1.8.7, which is pre-installed by default on OS X?
If so, one difference between 1.9.1 and 1.9.2 is that "." isn't part of $:'s path any more.
I recommend that you do rvm implode and delete the current setup. Then use the railsready script to setup RVM and Ruby properly for you on Ubuntu. It's important to understand that until you know what you are doing you should run the script as a user. Hope that helps.
On linux and OS X, I have always had to put require "rubygems" in the beginning. However it has always worked fine without this line on windows.
I've cloned the ruby 1.8.7 source tree. I can build ruby. But I can't figure out how to run it without installing it in system directories. How can I do it?
Background: I want to use "git bisect" to figure out which build of Ruby introduced a new behavior in my code. I need to build and run ruby against a test program, multiple times, but I don't want to clobber the ruby that the Debian package installed.
Here's what I get if I try to run the ruby I built from source:
$ ./ruby -e 'puts RUBY_VERSION'
ÀÇ ÀÇ : ÀÇ ÀÇ : cannot open shared object file: No such file or directory - ÀÇ ÀÇ (LoadError)
I've also tried installing it, but not to the system directories, and got a stack trace:
$ ./configure --prefix=/home/wayne/tmp/ruby/installed --exec-prefix=/home/wayne/tmp/ruby/installed
$ make
$ make install
$ /home/wayne/tmp/ruby/installed/bin/ruby -e 'puts RUBY_VERSION'
/home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl/ssl.rb:26: uninitialized constant OpenSSL::SSL::VERIFY_PEER (NameError)
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23:in `require'
from /home/wayne/tmp/ruby/installed/lib/ruby/1.8/openssl.rb:23
I've got the feeling that I'm close, but a miss is as good as a mile.
rvm is a great tool. and should be able to take care of the heavy lifting for switching between different ruby version (or even sets of gems).
Installation is very easy:
$ gem install rvm && rvm-install
$ echo "if [[ ! -z $HOME/.rvm ]] ; then source $HOME/.rvm ; fi" >> ~/.bash_profile
Then to install a specific version & patch level:
rvm install ruby-1.8.7-p160
Then to switch between versions:
$ rvm 1.8.7-p160
$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 160) [i686-darwin10.0.0]
$ rvm 1.8.7-p174
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.0.0]
When I need to do something like that, I use the chroot command. Create a temporary directory, install ruby into a sbin subdirectory, and chroot into the temp folder. Depending on what you are testing, you may also have to copy some system libraries into the temporary directory tree (before you chroot in).
It's probably easier to use rvm to test your app against different rubies.