Home > front end >  error Your Ruby version is 2.6.8, but your Gemfile specified 2.7.5
error Your Ruby version is 2.6.8, but your Gemfile specified 2.7.5

Time:11-09

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?

CodePudding user response:

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

CodePudding user response:

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:

  1. .ruby-version
  2. .rvmrc
  3. ~/.rvmrc
  4. .envrc
  5. .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.

  • Related