0

I am using Fedora 20 and installed Ruby version 2.0 and Rails version 4.1.6.

I tried rails new example but got this error:

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.11

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/bin/ruby extconf.rb 
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h


Gem files will remain installed in /home/sntr/.gem/ruby/gems/json-1.8.1 for inspection.
Results logged to /home/sntr/.gem/ruby/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
An error occurred while installing json (1.8.1), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

I tried gem install json -v '1.8.1' or sudo yum install json ... but it didn't change, I couldn't run Rails.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1622511
  • 91
  • 1
  • 6
  • 2
    Looks like the ruby headers are missing. Try installing them first via YUM: `sudo yum install ruby-devel` Following that, you may encounter a few other devel packages needed, like perhaps `libxml2-devel` and for databases `sqlite3-devel` (and `mysql-devel` if you intend to use MySQL) – Michael Berkowski Oct 30 '14 at 18:52
  • Here's a comparable question for RHEL http://stackoverflow.com/questions/18918588/did-i-install-ruby-1-9-3-correctly-on-rhel though, you won't want `ruby193-devel` packages, but rather plain `ruby-devel` on Fedora, which ships Ruby 2.0 – Michael Berkowski Oct 30 '14 at 19:08

4 Answers4

1

I tried gem install json -v '1.8.1' or sudo yum install json ... but it didn't change, I couldn't run Rails.

System package would actually solve the problem, but you would need to lock its version in your Gemfile.

Perhaps you don't know that, but the whole Ruby on Rails framework is actually packaged, so running:

# yum install rubygem-rails

will install Rails for you. But since you will be most likely installing some other gems or their newer versions, install Ruby header files as well by installing the ruby-devel package:

# yum install ruby-devel

Using system Ruby package is actually a good choice that is easy to install and gives you security updates among other things. Using Ruby version manager is going to help you only if you need other version of Ruby that is not provided by your OS.

0

Use RVM instead.

If you are still new, it gives you an option for experimenting with different versions and environments.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Aret
  • 475
  • 3
  • 9
  • All the same problems relating to missing development headers (`-devel` RPM packages) will be encountered with RVM. More even, because Ruby itself will need to be compiled. Not to say that RVM isn't a good solution when Ruby is sorted out initially and well understood on a platform... – Michael Berkowski Oct 30 '14 at 18:56
  • @MichaelBerkowski, what do you mean by saying 'Not to say that RVM isn't a good solution when Ruby is sorted out initially and well understood on a platform...'? – blelump Oct 30 '14 at 20:03
  • @blelump I mean that RVM _is_ a good solution once you understand how to get Ruby, Rails and Rubygems properly working on your platform without it. But introducing RVM before even being able to spin up a simple `rails new` is asking for trouble, in my opinion. – Michael Berkowski Oct 30 '14 at 20:19
  • 2
    @MichaelBerkowski, RVM is aware of the prerequisites needed to install Ruby, and will tell the user, along with installing them if they're not found. On a bare CentOS VM I just installed RVM, and then told it to install Ruby 2.1.4, and RVM said "Installing required packages: libyaml-devel, libffi-devel, autoconf, gcc-c++, readline-devel, zlib-devel, openssl-devel, automake, libtool, bison", after which it compiled Ruby correctly. Installing Rails then happens without problem using `gem install rails`. – the Tin Man Oct 30 '14 at 21:06
  • @theTinMan my experience running rvm on not already prepared machines must be limited to environments where I don't have root/sudo, where rvm will fail without openssl-devel, non-default on rhel. Just held up last week by that – Michael Berkowski Oct 30 '14 at 23:05
  • @MichaelBerkowski it does not seem to be a case here. – Aret Oct 31 '14 at 15:59
0

The problem here is that bundler doesn't use the system rubygems if newer versions are available from rubygems.org. So, even if you have rubygem-json installed from Fedora packages, if for example it's version 1.8.1, it will be ignored because version 1.8.2 is in rubygems.

The same issue occurs with several other gems, but doesn't result in bundle failing unless the gem uses native extensions (i.e. C code that needs to be compiled). Thus the json gem failed to install, but other gems will install successfully (from rubygems, not from yum).

This issue was discussed in depth on Github, and unfortunately it has no easy solution, and not one that involves Bundler. Bundler is meant to ensure that, given a Gemfile/Gemfile.lock, that it installs exactly the versions of gems given, regardless of what system it's being run on.

If you must use only system gems, then you need to create your app with rails new app --no-gemfile, install all of the necessary system gems yourself, and document which gems they are so that the gem installation can be repeated later for testing and deployment. In particular, your app will not have a Gemfile or Gemfile.lock. Typically you only need to do this if you're developing for deployment on a specific OS distribution (e.g. RHEL 7) and are only allowed to use system-provided packages anyway. This is common enough in enterprise software development. If you aren't in this scenario, you should probably not use system ruby or rubygems at all, and prefer RVM.

Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
0

Just run bundle update at the root of the project

alex
  • 646
  • 9
  • 19