How to control what Ruby interpreter Puppet uses? - ruby

I have an app that requires Ruby 2.1 and use Puppet to provision a handful of servers.
The problem is, when I install on the 2.1 version of Ruby, Puppet starts using that, and it is not supported.
I would like to somehow point Puppet to a 2.0 version of Ruby installed in /opt.
Any suggestions?

Perhaps you could add a shebang line in the .rb file that points to the other version of ruby?
Alternatively you could try to specifically add the bin/ruby location in /opt to the $PATH variable.

Typically, you shouldn't let the system determine the path, but instead provide the full path to the Ruby interpreter you want to execute your script:
/usr/bin/ruby /some/path/to/foo.rb
Or:
/usr/local/bin/ruby1.9.2 /path/to/bar.rb

Related

error Your Ruby version is 2.6.8, but your Gemfile specified 2.7.5

This happens when I run npx react-native init AwesomeProject.
When I check the system ruby version with ruby -v, it is already 2.7.5. ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-darwin21]. Anyone has any idea for this problem?
Sounds like you’re using rvm to manage Ruby versions.
You need to install and run the correct version, not delete the current.
Something like
rvm install 2.7.5
rvm use 2.7.5
The Gemfile Isn't (Directly) Your Issue
The Gemfile is irrelevant to solving this issue. It's just triggering it because your in-project Ruby version isn't matching what RubyGems (via the Gemfile or Gemfile.lock) expects as a constraint. It could be a minimum version, an exact version, an approximate version, and so forth. There are a lot of ways to specify version constraints in a project, and the Gemfile is just where the constraint-related exception is being raised by Bundler.
You could possibly make the problem go away simply by removing the requirement for a later version of Ruby from the Gemfile or gemspec, removing the Gemfile.lock, and re-running Bundler. However, if your code relies on features in a later version this will just create other problems for you. You really ought to uncover what's changing your Ruby environment within the project directory.
Dotfiles
There are a lot of reasons this could happen, but if your system Ruby is 2.7.5, then you need to check your project directory for various dotfiles like:
.ruby-version
.rvmrc
~/.rvmrc
.envrc
.env
or various other files that affect your shell environment or whatever Ruby version manager you're using. Most Ruby version managers respect .ruby-version, but some version managers use other files, including defaults or shims that may be set elsewhere. IDE's also often have their own project-specific configuration files, too, and they can sometimes be set to override the project's standard settings.
Also, make sure to check your Gemfile.lock and *.gemspec in addition to the Gemfile itself, in case something is being specified there or constrained by some other dependency.
Inspecting Environment Variables
You should also look at the Ruby- and RubyGems-related environment variables from your project directory to see how various values are set within the project. For example:
printenv | grep -E '^(RUBY|GEM)' | sort
Shebang Lines
In addition, you should check your shebang lines in any executable Ruby or shell scripts you're relying on to see whether a specific non-system Ruby is being invoked. For example:
grep -Enr '^#.*ruby' *.rb | grep -F '.rb:1:'
will find all the shebang lines that properly appear on the first line of a Ruby file. This will either point to a specific Ruby like #!/usr/bin/ruby or might be using a PATH lookup with #!/usr/bin/env ruby.
Shell scripts might be harder to inspect, as there may be calls to other executables or even an exec command, so you'll need to be more liberal with your grepping if you are looking for an interpreter call further down than the shebang line.
Checking PATH Order
In the case of #!/usr/bin/env ruby, you should inspect your PATH environment variable to see why the Ruby you want isn't being called first. Using which -a ruby (if supported by your OS) will show you all rubies in your PATH in the order they would be invoked by the shell. It's possible that you're simply calling an unexpected Ruby version that comes first in the PATH.

Change gem env RUBY EXECUTABLE path for one command

I would like to run gem commands, such as gem install, with a different ruby version than what is listed in gem env. The Ruby version I want to use is a pre-compiled version which I have the path for, so installing and using another version from RVM or similar would not solve my problem.
I do not want to change the RUBY EXECUTABLE permanently, just for one command at a time. I have tried to set GEM_HOME, GEM_PATH, PATH, RUBY and more. I have tried firing up gem with specific/version/of/ruby/path/ruby path/to/gem env, but I still get the default Ruby in my RUBY EXECUTABLE variable.
I even tried settingRUBY_EXECUTABLE=/path/to/correct/ruby, which also did not work.
What really surprised me was that when I edited the shebang in the path/to/gem file itself so it pointed to the correct Ruby, it still did not work! What is up with that?!
How can I change this variable so I can use gem goodness with my custom compiled Ruby?
This one is really beating me. I have now updated my rbconfig.rb to point to the desired Ruby path. I have looked at the rubygems source and replaced every single instance of the default ruby , in all the files I could find, with the path to the one I want. Even this did not set the environment correctly. Is this somehow hard-coded into the compiled ruby? If that is the case, why the star*4 is this done?
Try using rbenv (https://github.com/sstephenson/rbenv) or RVM to manage Ruby versions (https://rvm.io/). When you switch Ruby versions with rbenv, gem env will use use the new Ruby version. The following command can be used to change the Ruby version for a single shell:
$ rbenv shell 2.1.2
After hours and hours of research, stepping through the Ruby source with Pry, reading source code and more I figured out that this is not possible to do because it is hard-coded into ruby at compile time (wtf?). Anyway, the way to solve this is to simply recompile Ruby. Yeah.
There is also apparently a compile flag which you can set which removes this hard-coded environment: --enable-load-relative
After struggling with this for way to long I finally got this project working, where I have made an easy to use portable version of Ruby. Simply put, a folder with Ruby on it which you can move about, put on a USB stick or whatever, and it still works :)

How do i get list of all installed packages in ruby 2.0.0

I want to get list of all installed packages on my machine using ruby gem or plugin. Functionality is similar to dpkg -l command on ubuntu. Is there any appropriate ruby gem or plugin available to get the same?
If you mean Ruby gems, then
gem list
You can execute a shell command inside the ruby interpreter. In your case, simply run
`dpkg -l`
The output will be a string containing the output of the command. Please note the `.
Keep in mind there are several ways to perform a shell command in Ruby.
There is a Debian package called ruby-debian (in older Debian releases it is called dpkg-ruby) which contains Ruby bindings to dkpg.
Note: it relies on a C extension, so it will not work on Ruby implementations that don't support them, such as IronRuby.

How to tell Terminal which version of Ruby to use?

I have two related questions that I was hoping someone could help out with.
I recently installed Ruby 1.9.2 on my Mac (running Snow Leopard 10.6.4) and I haven’t been able to figure out how to get Terminal to use the new Ruby as a default, rather than the factory-installed Ruby 1.8.7. The old Ruby 1.8.7 is located in my ~/usr/bin/ruby directory while the new Ruby 1.9.2 is in ~/usr/local/bin/ruby. Someone said that I need to put the new version of Ruby's directory in the PATH prior to the old version's directory so that the system looks there first - is this correct? If so, can anyone provide step by step instructions on how to do this?
I’ve created a new directory but can’t seem to figure out the correct way to add that directory to my PATH using the Terminal bash shell. I tried using the instructions that I found here (http://www.macgasm.net/2008/04/10/ad...thin-terminal/) twice but they didn't work for me. The directory containing my program ("Ruby_Programs") shows up in the PATH but when I try to run "ruby newprogram.rb" from the command line it results in ":ruby: No such file or directory -- newprogram.rb (LoadError)". The file definitely exists and is a functional Ruby program. I did change the name of the directory to "Ruby Programs" and then back to "Ruby_Programs" - could that have somehow caused this problem?
Any help would be greatly appreciated. Here is my current PATH:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/sbrriffe/src:/usr/X11/bin:/Users/sbriffe/Ruby_Programs/:
You might want to check out rvm. You can install multiple versions of ruby side by side and easily switch between them. If you follow the rvm installation notes you won't have any more path problems.
Your Ruby Programs directory shouldn't be in your path: the location of your ruby interpreter should be. Then, you cd to the location of your ruby program, and run it from there: ruby program.rb.
Since you are on a Mac, check out homebrew for something that will make installing software easier. I have my homebrew set up in /usr/local, and it works great.
Once you have installed stuff where you need it, then you'll want to adjust your $PATH. The items in $PATH are searched in the order they appear, so in your ~/.bashrc, you'll want to add:
export PATH=/usr/local/bin:$PATH
To make sure /usr/local/bin gets searched before /usr/bin.
I would use RVM to get everything installed, and then once you have RVM installed it is easy to set your default Ruby version.
Check out https://rvm.io/ -- once you have that installed you can change your default by using : $ rvm use 1.9.2 --default
hope that helps- you can do this with any version, not only 1.9.2

Getting Textmate to recognize Ruby version upgrade

I used the instructions at http://bparanj.blogspot.com/2010/06/installing-ruby-191-on-snow-leopard.html to install Ruby version 1.92 on my Mac running Snow Leopard. The only deviation is in step 3, which calls for .bash_profile to be updated. I have .profile, but not .bash_profile, in my home directory, so I added the export command to the last line of .profile. The installation completed successfully (with the same two warning messages as mentioned, which I too disregarded), as Ruby -v in a terminal prints
ruby 1.9.2dev (2010-07-02 revision 28524) [x86_64-darwin10.4.0].
When I run Textmate, however, cntrl-R invokes Ruby version 1.8.7, as it did before the 1.9.2 installation. In Textmate's Preferences-Advanced-Shell Variables, TM_RUBY is set to /usr/bin/ruby. The (binary alias) file 'ruby' has not been updated. What is the easiest way for me to instruct Textmate to use the newer version of Ruby? Please note my understanding of OS X is relatively limited.
What is the easiest way for me to
instruct Ruby to use the newer version
of Ruby?
I believe you mean "What is the easiest way for me to instruct Textmate to use the newer version of Ruby?"
Assuming that is the case, have you tried to edit the TM_RUBY shell variable to point to your newly installed version? According to the docs you referenced, it should be somewhere under /usr/local (most likely /usr/local/bin/ruby).
You can find out the location of your ruby installation by typing the following in your terminal window:
$ which ruby
/usr/local/bin/ruby
then perform the following to verify the version
$ ruby -v
Once you have the proper ruby path, in Textmate, double-click the 'value' of the TM_RUBY shell variable & type in the path to your 1.9.2 install.
Why not just create a .bash_profile file in your home directory?

Resources