Ruby Version Different Depending on Terminal Tab - ruby

This question seems to happen a lot (here, here, and here), but never with a successful answer.
I have ruby 2.5.0 installed correctly and have verified it with ruby -v. However, when I open a new Terminal tab, it defaults me back to ruby 2.3.3p222 for some reason. I have ruby '~> 2.5.0' in my gemfile, if it helps.
Does anyone know how this is happening?

RVM can read the .ruby-version file on the root of your project folder.
echo 2.5.0 > .ruby-version
Open a new terminal and check the Ruby version.

A new tab opens a new shell, which re-reads your shell's initialization files. It doesn't start with the environment variables which happen to be set in the currently-active tab.

If you are using rbenv, you can set your global ruby by:
rbenv global 2.5.0
Pretty much the same thing for rvm:
rvm --default use 2.5.0

Related

Why doesn't RubyMine 2018.2 detect my gems?

I'm trying to debug a Rails project within RubyMine v2018.2 (2018.2.1, 2018.2.4). When I start the editor I get a warning:
RubyMine Gem Manager: RubyMine has detected that some of the gems required for 'project' are not installed.
When I try to run RubyMine's Bundler install command I'm told
bash -c "RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install -V"
Activating bundler (< 2) failed:
Could not find 'bundler' (< 2) among 28 total gem(s)
Checked in 'GEM_PATH=/home/brad/.gem/ruby/2.5.0:/var/lib/gems/2.5.0:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.5.0:/usr/share/rubygems-integration/2.5.0:/usr/share/rubygems-integration/all', execute `gem env` for more information
To install the version of bundler this project requires, run `gem install bundler -v '< 2'`
...and when I try to run or debug my application from the IDE I'm told "Could not find bundler (version) required by your project-path/Gemfile.lock."
Why doesn't RubyMine detect my installed Gems?
My environment
Ubuntu 18.04.01
zsh 5.4.2
rbenv 1.0.0
rbenv-binstubs # 8ecb7ca8
Rubymine 2018.2.4
I'm using rbenv to manage my Ruby versions. The system has Ruby 2.5.1 installed, but my project depends on 2.5.0. I've set 2.5.0 as the rbenv global version. Configuration for how rbenv chooses a Ruby version seems okay; I get the following results from the noninteractive commands:
> echo $RBENV_VERSION
(no response)
> cat ~/.rbenv/version
2.5.0
> rbenv global
2.5.0
After noticing rbenv wasn't working properly in some RubyMine commands I moved the eval "$(rbenv init -)" setup step from by ~/.zshrc up to ~/.zshenv and ~/.profile so it will also apply to bash and zsh login shells (per guidance here). I'm now getting matching results for interactive and login shells, but still getting unexpected behavior from the Bundle install command within RubyMine.
What is working
My application installs, builds and runs just fine from an interactive shell.
RubyMine's Ruby SDK settings correctly detect rvm and its installed versions, and all of the installed gems for that version.
What I've tried
The output from the bundle install command claims it's running
bash -c "RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install"
When I run this from my interactive shell or with RubyMine's "Run anything" feature (login shell) this command works fine - as does any of the following:
RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install
/usr/bin/rbenv exec bundle install
rbenv exec bundle install
bundle install
So I can't figure out what context RubyMine is using to run this command. It's clear from the error message that the GEM_PATH is incorrect, causing RubyMine to look in the wrong place for bundler, but I don't know where it's getting this configuration. If I bring up the "RubyGems Environment" view in RubyMine I get:
RUBYGEMS VERSION: 2.7.3
RUBY VERSION: 2.5.0 (2017-12-25 patchlevel 0) [x86_64-linux]
INSTALLATION DIRECTORY: /home/brad/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
USER INSTALLATION DIRECTORY: /home/brad/.gem/ruby/2.5.0
RUBY EXECUTABLE: /home/brad/.rbenv/versions/2.5.0/bin/ruby
EXECUTABLE DIRECTORY: /home/brad/.rbenv/versions/2.5.0/bin
SPEC CACHE DIRECTORY: /home/brad/.gem/specs
SYSTEM CONFIGURATION DIRECTORY: /home/brad/.rbenv/versions/2.5.0/etc
RUBYGEMS PLATFORMS:
ruby
x86_64-linux
GEM PATHS:
/home/brad/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
/home/brad/.gem/ruby/2.5.0
(...)
SHELL PATH:
/home/brad/.rbenv/versions/2.5.0/bin
/usr/lib/rbenv/libexec
/home/brad/.rbenv/plugins/ruby-build/bin
/home/brad/.rbenv/plugins/rbenv-gemset/bin
/home/brad/.rbenv/plugins/rbenv-binstubs/bin
(...rest of PATH...)
----------------------
IDE: RubyMine 2018.2.4, build #RM-182.4505.55
OS: Linux 4.15.0-38-generic[amd64]
Java: 1.8.0_152-release-1248-b8
RubyMine SDK Environment:
Sdk: rbenv: 2.5.0
Sdk Version: ver.2.5.0p0 ( revision 61468) p0
Ruby Interpreter: /home/brad/.rbenv/versions/2.5.0/bin/ruby
RVM Sdk: Rbenv sdk, gemset nullSdk Language Level: 2.5
Sdk Load Path:
~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/did_you_mean-1.2.0/lib
~/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0
~/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/x86_64-linux
~/.rbenv/versions/2.5.0/lib/ruby/site_ruby
~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0
~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0/x86_64-linux
~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby
~/.rbenv/versions/2.5.0/lib/ruby/2.5.0
~/.rbenv/versions/2.5.0/lib/ruby/2.5.0/x86_64-linux
~/.local/share/JetBrains/Toolbox/apps/RubyMine/ch-0/182.4505.55/rubystubs25
Sdk Gem paths:
~/.gem/ruby/2.5.0/gems
~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/bundler/gems
~/.bundle/ruby/2.5.0/gems
~/.bundle/ruby/2.5.0/bundler/gems
~/.gem/ruby/2.5.0/bundler/gems
~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems
(full list of gems used for project)
So I think that last bit - "Sdk Gem paths" - might be the problem point, but I don't see any way to configure it.
I'm also already aware of the YouTrack issue "Rubymine 2018.2 release does not take $GEM_HOME environment variable" and don't think it's quite what's happening here. It also claims to be fixed in 2018.3 EAP1, so I installed 2018.3 EAP7 but am still seeing this problem.
I got things working! By signing out and back in. :facepalm:
To be specific, I think the key steps were
Move the $(rbenv init -) step from my .zshrc up to my .zshenv and/or to .profile
Signing out and back in.
The key insight was noticing that ~/.rbenv/shims was missing from the RubyMine Gem Environment despite showing up in both interactive and login shells (in and out of RubyMine), and realizing that while new shells were picking up my .profile changes, RubyMine itself was probably inheriting its environment variables from my session and I'd need to log out/in to to pick up the changes.
I now see ~/.rbenv/shims in my RubyMine Gem Environment, and both the Bundler Install command and my run configurations are working again.
Thank you Casper for pointing me in the right direction!
Been where you are before a bunch of times.
Under the list of scripts you can run, click "Edit Configurations". Should be in the main toolbar.
Double check that you have "Run the script in context of bundle (bundle exec)" checked on the bundler tab. This is SUPER important and is not checked by default. Make sure you check for any rake, rails or irb configurations.
Keep in mind that the run configuration is where you can override the settings for the project. It will create all kinds of problems if this is not set appropriately.
Hope this helps! Bit me a bunch of times.

warning from ruby version file

I cloned a project and it had a .ruby-version file to specify ruby version of the project. content:
2.0.0
when I switched into the project dir for the first time it gave me warning:
ruby-2.0.0-p645 is not installed. To install do: 'rvm install
ruby-2.0.0-p645'
However it's true that my system didn't have Ruby 2.0 but Ruby 2.2 instead. Does that warning makes sense? Is there a way I use duplicate functionality of (~>) gem dependency in ruby-version file?
Based on the warning, you are using rvm. As another answer says, rbenv can also use a .ruby-version file, but switching to rbenv isn't neccesary, and won't really change anything.
I don't think rvm's .ruby-version file supports a range of versions. It needs to specify a version name that could be passed to rvm use, and I don't think there's any way to say a range of versions there.
You can:
Install ruby 2.0.0 with rvm: rvm install 2.0.0. When you switch into the project directory, rvm will automatically switch you to using ruby 2.0.0 like the .ruby-version file directs.
Simply change your .ruby-version file to specify the version of ruby you want to use instead. Just edit it in a text editor, or run rvm --ruby-version use 2.2.3 to have it write the .ruby-version file for you. Now when you switch into the project directory, rvm will automatically switch you to 2.2.3.
Delete the .ruby-version file entirely. You aren't required to use it. Personally, I don't use them and don't find them helpful. The .ruby-version file is meant to force a particular version of ruby to be used in that project -- but most projects work with multiple versions of ruby, and this isn't really neccesary, and as you've seen can be a headache when it's out of date and is trying to force you to use an older version of ruby, when a newer one would probably work fine.
.ruby_version is used for rbenv
You should install rbenv
or create .rvmrc for rvm

does rbenv set the PATH variable to point to the correct ruby?

I installed rbenv and it seems and copied 2 versions of ruby to ~/.rbenv/versions, it shows
them both correctly.
When I run
rbenv global 1.8.7-p72
It says ok and also points to it. However when I run--
ruby --version
I get ruby not found. Basically, the ruby executable is not found in the path. Can anyone help me with this problem?
My .bash_profile is
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv versions outputs --
1.8.7-p72
*1.9.3-p448 ( set by /home/user/.rbenv/version)
Also my ~/.rbenv/shims directory doesn't have a ruby executable. It has erb, gem,irb , rake, rdoc , ri and testrb. Could this be the issue?
I'm sorry I'm completely clueless
Did you execute all the step in the installation instructions https://github.com/sstephenson/rbenv? I.e. did you add the required snippets to your shell startup (.bash_profile) and so forth?
If you followed the instructions, could you provide the output of rbenv versions and which -a ruby for a start?
EDIT:
rbenv rehash seems to be what generates the shims. This must be executed every time you install a new ruby version. How did you install the rubies? With ruby-build, as recommended in the installation instructions?
When I encountered this error, all I had to do was to make sure that $PATH had shims prior to system version of ruby like so:
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

irb not starting

I'm running Ruby 1.9.2 w/ rbenv. I recently moved my Users directory to a different drive. Ruby and irb show up using which.
$ which ruby
/Volumes/Pig/Users/arrow/.rbenv/shims/ruby
$ which irb
/Volumes/Pig/Users/arrow/.rbenv/shims/irb
However, running irb hangs indefinitely.
I've also been running ruby files under the current config but executing $ ruby -v also hangs.
rbenv rehash can help solve these sort of problems. Anytime you modify your Ruby installation you need to rerun that command to let rbenv rebuilt its soft-links.
From the command-line output:
rehash Rehash rbenv shims (run this after installing binaries)
You might also need to tell rbenv where your binaries are again, i.e., rbenv global 1.9.2... and let it adjust things.
Remember, rbenv assumed your binaries were on a different volume so it might have gotten confused and needed you to straighten it out.
The other issue could be that Ruby bound to files on the other volume at compile time (because that's what rbenv does, it compiles Ruby for you) so when the files couldn't be found the apps crashed. Reinstalling rbenv would fix that, as telling it to reinstall a particular Ruby should have fixed it.
I uninstalled/installed rbenv and all is well.

MacVim cannot find gems when launched from the command line

I have MacVim installed with Janus and a few extra lines in both .vimrc.local and .gvimrc.local.
When I launch mvim from the terminal (iterm2 or terminal) and try !rails -v I get
Could not find RubyGem rails (>= 0) (Gem::LoadError)
Launching MacVim from the dock and doing !rails -v works fine and returns Rails 3.0.7.
I get the same behaviour with all gems.
I realise I'll likely have to provide more information to solve this but tbh I have no idea whether it is macvim, rvm, bash, or something else causing the issue.
TIA
I also use MacVim (I'm running it from iTerm (screen)), and !rails -v works.
Did you set rvm installed ruby interpreter to be default ruby executable on system? Try running
which ruby
from terminal, and also you could try to run !which ruby from MacVim. You should get something like:
/Users/your_user/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
If not, try setting one of your rvm installed rubies as default (As explained on http://beginrescueend.com/rubies/default/).

Resources