VSCode uses wrong ruby version - ruby

I'm having an issue with VSCode on Ubuntu. I have the ruby extension and the ruby-rubocop extensions installed but when I run the rubocop extension on save, I get 2 errors
rbenv: version '2.5.1' is not installed (set by RBENV_VERSION environment variable)
command /home/{username}/.rbenv/shims/rubocop returns empty output! please check configuration.
My project is using ruby version 2.7.6 installed via rbenv. 2.5.1 is an old version of ruby I had installed which I since removed. I don't understand why VSCode is trying to use version 2.5.1. When I run rubocop via a bash shell, it runs as expected. rbenv local and rbenv global are all set to 2.7.6 and my project as a .ruby-version file with 2.7.6 set.
When I run echo $RBENV_VERSION in a bash shell, I get nothing returned, but if I run this in a terminal shell within VSCode, it returns 2.5.1.
How do I get VSCode to use the correct rbenv version?

Related

Installing Ruby version using rbenv

I have been trying to install React Native into my Mac. In the installation docs, it requires Ruby version of 2.7.6 have been to be installed.
I have installed a new Ruby version using rbenv manager and it was successful. But now I cannot switch my machine to new Ruby version. Any help will be highly appreciated.
Thanks!
You have to set the version for the directory where your react project is. You can do this with:
rbenv local 2.7.6
This creates a file called .ruby-version in the directory, which tells rbenv what version of Ruby to use.
This is all visible in the README of rbenv.
You can have a look at this solution
Use the below command to change your ruby version:
rbenv global 3.1.2 # set the default Ruby version for this machine
# or:
rbenv local 3.1.2 # set the Ruby version for this directory
You can find this from rbenv Official Github Repo

new version of rails installed, rails command not recognized

I want to do a new rails app. I installed ruby 2.4.4 because I cloned a project.
I used to set the new version for the project
rbenv shell 2.4.4
because rbenv local was not working
Now that I am using ruby 2.4.4
system
2.3.4
2.3.5
* 2.4.4 (set by RBENV_VERSION environment variable)
I get following error message:
rbenv: rails: command not found
The `rails' command exists in these Ruby versions:
2.3.4
2.3.5
1) how can I fix this?
2) why is rbenv local not working?
What happens when running rbenv local 2.4.4
~/Local_Documents/CodingArea/personal_projects/zaina-project/zaina_deal_room/zaina-dealroom   setup  ruby -v
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-darwin16]
~/Local_Documents/CodingArea/personal_projects/zaina-project/zaina_deal_room/zaina-dealroom   setup  rbenv local 2.4.4
~/Local_Documents/CodingArea/personal_projects/zaina-project/zaina_deal_room/zaina-dealroom   setup  ruby -v
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-darwin16]
basically it happens nothing. Ruby version 2.3.5 is still selected. I expect to see version 2.4.4 as selected, after running rbenv local 2.4.4. I guess it's something with my ENV, that the version that I have written in a file, takes preferences, but I don't know how to fix this.
in my .zshrc I have:
export RBENV_VERSION=2.3.5 # use rbenv rehash
export ALBERT=8
export PATH="$HOME/.rbenv/bin:$PATH:./node_modules/.bin"
Is it wrong to have the ruby version there? is my PATH correct?
So, rbenv local 2.4.4 will never be applied because of export RBENV_VERSION right?
At a high level, rbenv intercepts Ruby commands using shim executables injected into your PATH, determines which Ruby version has been specified by your application, and passes your commands along to the correct Ruby installation.
When you run a command like ruby or rake, your operating system searches through a list of directories to find an executable file with that name. This list of directories lives in an environment variable called PATH, with each directory in the list separated by a colon: /usr/local/bin:/usr/bin:/bin
Once rbenv has determined which version of Ruby your application has specified, it passes the command along to the corresponding Ruby installation.
Each Ruby version is installed into its own directory under ~/.rbenv/versions. For example, you might have these versions installed: ~/.rbenv/versions/1.8.7-p371/
So, for each ruby version you must install two gems:
gem install bundler
gem install rails
About how to fix rbenv you can read in this question.

I cannot update Ruby on Homebrew from 2.0.0 to 2.3.1

I am trying to upgrade Ruby because I need to setup a Jekyll template, and I need to latest version to do it. Since I have a Mac running Sierra, I already have Ruby preinstalled as well as the Homebrew installation. When I install it using brew install ruby, it works, but when I check the version, it is still at 2.0.0 instead of 2.3.1 where it should be. Homebrew says I have 2.3.1, but the CLI says I have 2.0.0. I tried to use brew link --override ruby to make it work, but it said everything was working and it got me nowhere.
Use rbenv and plugin ruby-build. It will keep several versions of ruby on the one machine.
After install go to directory with your code, run rbenv install 2.3.1 and create file .ruby-version containing 2.3.1. All scripts running from this directory will use ruby 2.3.1.
Or you will able to set ruby version for all running scripts - rbenv global 2.3.1

switch to ruby version installed through RVM

my macbook is OSX El Capitan. It has Apple pre-installed universal Ruby version 2.0.0.
Now, I want to use latest Ruby 2.3.0. I managed to use rvm to install Ruby 2.3.0. I see the following message after rvm finished installing ruby 2.3.0:
Ruby was built without documentation, to build it run: rvm docs generate-ri
/Users/John/.rvm/bin/rvm: line 66: shell_session_update: command not found
Creating alias default for ruby-2.3.0...
* To start using RVM you need to run `source /Users/John/.rvm/scripts/rvm`
in all your open shell windows, in rare cases you need to reopen all shell windows.
I followed this message, & run command
source /Users/John/.rvm/scripts/rvm
but when I run ruby -v, the version is still Apple pre-installed Ruby 2.0.0. How can I switch to the ruby I installed through rvm?
(under path .rvm/rubies I can find ruby-2.3.0)
To switch to using 2.3.0, do
rvm use 2.3.0
To make it default, do
rvm use --default 2.3.0

Need to set ruby version each time a terminal session is started

I use rvm on my Ubuntu 14.04 machine and each time I start the terminal, the ruby version selected (from ruby -v) is ruby 1.9.3p551 . rvm --default use 2.2.3 lets me use the selected ruby version, but the entire process needs to be redone each time a new session is started.
How can this be resolved?
Check in your gemfile if there is a references to 1.9.3
If not, check this answer and follow the instructions How to set default Ruby version with RVM?
You can use the .ruby-version file.
This file checked by both ruby version managers, like rvm and rbenv.
For example you can place a file .ruby-version with 2.2.3 in the home directory ~/.ruby-version, it is should set the ruby version at 2.2.3 for any new terminal session. Also you can place a file .ruby-version in the project directory, /path/to/the/project/.ruby-version it is should set ruby version for project.

Resources