I somehow messed my $PATH up. (changed it) and my ruby and rails gems weren't working. What I did in efforts to fix this was to reinstall RVM. Now my ruby works fine in the Terminal.
On running gem install railsI get greeted with this:
Permission denied - /Users/emKaro/.rvm/gems/ruby-2.0.0-p247/gems/atomic-1.1.13/test/test_atomic.rb
When I try to install rails through sudo gem install rails, I get this error:
ERROR: While executing gem ... (Errno::EACCES)
I have XCode installed with Command-Line tools installed as well. Even went ahead to install GCC but get the very same error when I try to install rails.
This is the full error message
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /Users/emKaro/.rvm/gems/ruby-2.0.0-p247/gems/atomic-1.1.13/test/test_atomic.rb
emmanuels-imac:~ siaW$ sudo gem install rails
Password:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:519:in `try_link0'
from /Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:817:in `try_run'
from extconf.rb:24:in `<main>'
Gem files will remain installed in /Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.13 for inspection.
Results logged to /Users/emKaro/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.13/ext/gem_make.out
I am the admin of my computer and running Mac OS 10.8.3. I'd appreciate some help with the rails install.
EDIT:
Thanks #Christoph
The only code that helped was to actually remove the RVM folder with
sudo rm -rf ~/.rvm/
I tried only to uninstal and it didn't work. So removing the folder and then reinstalling RVM does the job.
Like Dave pointed out up there, there should be no need to sudo any Gem installation. You could try to reset the permissions on your RVM directory:
sudo chown -R emKaro: ~/.rvm/
and try your Gem instllation again. If that doesn't work out, you should consider removing your RVM installation and start over:
sudo rm -rf ~/.rvm/ # remove your RVM directory
There should really be no need for using sudo in any of this.
I'm running 10.8.5 OSX and was trying to install Rails with Ruby 1.9.3-p448 - so I don't know if it's exactly the same problem, but by looking in the mkmf.rb code where it failed, I found that the problem was that the basic CC command wasn't running. For some reason, it needed to search for the folders in /usr/local, and for some reason, /usr/local was set to 740 permissions on my install (which I had gotten from someone else so not sure what he had done prior to my getting the machine.)
So after I did chmod +x /usr/local, everything worked ok.
I had this problem in Ubuntu, but it was because I didn't installed the rvm with "Run command as a login shell", after the installation of rvm they told about this, and the link to look for the information to enable in ubuntu.
Here's the link:
https://rvm.io/integration/gnome-terminal
For RVM to work properly, you have to check the 'Run command as login shell' checkbox on the Title and Command tab of gnome-terminal's Edit ▸ Profile Preferences menu dialog, in case the menu is missing right click the terminal app and navigate Profiles ▸ Profile Preferences.
Related
I'm trying to install a gem ruby-oci8 on the rails.
I get the following error,
$ gem install ruby-oci8
Building native extensions. This could take a while...
ERROR: Error installing ruby-oci8:
ERROR: Failed to build gem native extension.
current directory: /home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8
/home/solovievga/.rvm/rubies/ruby-2.4.2/bin/ruby -r ./siteconf20180213-21757-1hli0rl.rb extconf.rb
checking for load library path...
LD_LIBRARY_PATH is not set.
checking ld.so.conf... no
checking for cc... ok
checking for gcc... yes
checking for LP64... yes
checking for sys/types.h... yes
checking for ruby header... ok
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/solovievga/.rvm/rubies/ruby-2.4.2/bin/$(RUBY_BASE_NAME)
--with-instant-client
--without-instant-client
/home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:909:in `get_home': RuntimeError (RuntimeError)
from /home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:728:in `initialize'
from /home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:269:in `new'
from /home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:269:in `get'
from extconf.rb:22:in `<main>'
---------------------------------------------------
Error Message:
Set the environment variable ORACLE_HOME if Oracle Full Client.
Append the path of Oracle client libraries to LD_LIBRARY_PATH if Oracle Instant Client.
Backtrace:
/home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:909:in `get_home'
/home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:728:in `initialize'
/home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:269:in `new'
/home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1/ext/oci8/oraconf.rb:269:in `get'
extconf.rb:22:in `<main>'
---------------------------------------------------
See:
* http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-full-client.md for Oracle full client
* http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-instant-client.md for Oracle instant client
* http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/install-on-osx.md for OS X
* http://www.rubydoc.info/github/kubo/ruby-oci8/file/docs/report-installation-issue.md to report an issue.
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/solovievga/.rvm/gems/ruby-2.4.2/extensions/x86_64-linux/2.4.0/ruby-oci8-2.2.5.1/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/solovievga/.rvm/gems/ruby-2.4.2/gems/ruby-oci8-2.2.5.1 for inspection.
Results logged to /home/solovievga/.rvm/gems/ruby-2.4.2/extensions/x86_64-linux/2.4.0/ruby-oci8-2.2.5.1/gem_make.out
I wonder how I can install the ruby-oci8 package?
You have to set LD_LIBRARY_PATH to install ruby-oci8 gem. Here I am assuming you are using linux os. Try this on your terminal.
$ LD_LIBRARY_PATH=$ORACLE_HOME/lib
$ export LD_LIBRARY_PATH
$ gem install ruby-oci8
Edit:
If you uses mswin32 ruby, use the following command instead.
gem install --platform x86-mingw32 ruby-oci8
or
gem install --platform x64-mingw32 ruby-oci8
Let me know If you have any query or question.
Happy Coding
These steps take you through the instant client installation process as well, just to provide some context. Download the instant and SDK zip files here:
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
Unzip both files in the following directory as root.
/opt/oracle
Enter the instantclient_12_2 directory following the unzip and create a symlink for the libclntsh.so file:
ln -s libclntsh.so.12.1 libclntsh.so
Now, depending on your OS, you'll need to update your LD_LIBRARY_PATH location. In Ubuntu, do the following:
sudo nano /etc/ld.so.conf.d/moreLibs.conf
And enter the following in the file and save
/opt/oracle/instantclient_12_2
Now run the following command:
sudo ldconfig
And rerun your gem install
gem install ruby-oci8
This solution worked for me
Before executing bundle install, Run below code on the console or add it to bash and source.
Export OCI_DIR=/opt/oracle/instantclient_12_2
Here /opt/oracle/instantclient_12_2 is path to instantclient on my mac machine.
You need to download instantclient from
https://www.oracle.com/database/technologies/instant-client/downloads.html
Unzip the download into /opt/oracle directory and export OCI_DIR environment variable.
I have a similar problem installing the ruby-oci8 gem. In my case, there is already an app that uses it. When I deployed another app using the same gem, during bundle install, this error occurred. It asked for the LD_LIBRARY_PATH environment variable. Apparently, the installer of the old app did not set the variable to persist.
I thought that there should be an oracle library somewhere in the server so I searched for it. It was not in /opt but in /usr/lib. I set the variable to /usr/lib/oracle/11.1/client64/lib then the gem was installed smoothly.
My machine using OSX El Capitan. I already follow the instructions one by one. It's working fine until I need to install rails, something unwanted happened. BTW I already installed Xcode command line tools but still not fix even I restart the terminal and machine. I got this error message.
gem install rails -v 4.2.4
Error message
Last login: Sat Nov 7 15:07:53 on ttys000
Mohammads-MacBook-Air% gem install rails -v 4.2.4
Ignoring curb-0.8.8 because its extensions are not built. Try: gem pristine curb --version 0.8.8
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/ZERO/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20151107-786-tg78i0.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Ignoring curb-0.8.8 because its extensions are not built. Try: gem pristine curb --version 0.8.8
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.
To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions. If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/ZERO/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
--help
--clean
--use-system-libraries
--enable-static
--disable-static
--with-zlib-dir
--without-zlib-dir
--with-zlib-include
--without-zlib-include=${zlib-dir}/include
--with-zlib-lib
--without-zlib-lib=${zlib-dir}/lib
--enable-cross-build
--disable-cross-build
extconf failed, exit code 1
Gem files will remain installed in /Users/ZERO/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/ZERO/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0-static/nokogiri-1.6.6.2/gem_make.out
Mohammads-MacBook-Air%
Reference: https://gorails.com/setup/osx/10.11-el-capitan
There error message tells you:
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.
To install Command Line Tools, try running xcode-select --install on
terminal and follow the instructions. If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
Unfortunately this error message is sometimes misleading. What the message really tries to tell you is that it just wasn't able to use the Command Line Tools. That does not necessarily mean they are not installed - it might mean that you just did not accept the license agreement yet.
I noticed that this is a common problem after installing Xcode and it recurs after updating to newer versions of Xcode. I recommened to do the follow steps after each installation or upgrade of Xcode:
Open Xcode and accept the license agreement
Try to reinstall the Command Line Tools
The second step seems to depend on your OS versions. If xcode-select --install does not work try to start the install Command Line Developer Tools app in /System/Library/CoreServices.
After reinstalling the Command Line Tools try to rerun bundle install.
It's working now. Using rvm to handel it.
rvm install ruby-2.1.1
rvm 2.1.1
gem install rails -v 4.1.0
Opening Xcode and accepting the license agreement fixed this error for me. Afterwards, could run sudo gem install rails successfully.
I am trying to install the pg gem for use with Postgres.app on my local machine. I am running Mavericks.
Postgres.app is installed and running fine, but I cannot get the gem to work. I've done the following:
Used the command 'env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config' from the Postgres.app documentation
Updated Homebrew and installed the Apple GCC 4.2
Installed the Xcode developer tools
Updated my $PATH to reference both the Postgres.app bin and lib directories
All with no success. Here is the specific error message I receive:
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config'
This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/MacOS/bin/pg_config
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
--with-pg
--without-pg
--with-pg-config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/
I'd appreciate any help you can offer. Thanks!
You probably have the wrong path for --with-pg-config, check if it's actually there.
You can find the correct path to pg_config with:
find /Applications -name pg_config
In the latest Postgres.app Version the path is:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
In my case (running Postgres.app v9.3.4.2) it only seemed to work when prepending the environment architecture flags:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Being on a brand new mac, here's what I had to do:
Install Xcode tools from the app store
Open Xcode tools and accept the license
Now run (hopefully a future-proofed command):
version=$(ls /Applications/Postgres.app/Contents/Versions/ | tail -1)
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/$version/bin/pg_config .
If you have trouble, you can troubleshoot a bit by checking out the actual error in the mkmf.log which you can find by running (if using rvm):
cd ~/.rvm ; find . -name mkmf.log | grep pg
I was able to install pg with this command
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
I found my path by running
sudo find / -name "pg_config"
and I Successfully installed pg-0.17.1
adding the postgress bin dir to the path also does the trick. just add the bin to the pat like this. with recent installs the latest symbolic link makes sure that this path should be 'stable' for future version upgrades.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
To resolve this issue I installed postgres using homebrew using the following in the terminal window:
brew install postgres
Homebrew can be found here
http://brew.sh
This worked for me:
gem install pg -- --with-pg-config=`which pg_config`
For future reference, since a lot of us are posting new paths for the new version numbers:
Currently I'm seeing a symlink called latest in /Applications/Postgres.app/Contents/Versions/.
You should be able to just do:
$ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
And forget about the version number. This might not apply to every (older) version, but I myself was looking for a snippet I could save and reuse.
I always failed to install rails on my new Air with OSX 10.8.4, even I succeed installed ruby2, Command Line Tool of XCode, it still prompts some error when I try to install rails.
I found lots solutions from the Internet, but they all don't work on my issue, I'm completely confused, doesn't any can help me? I'll really appreciate!!!
localhost:myapp zerocool$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
localhost:myapp zerocool$ sudo gem install rails
Password:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:519:in `try_link0'
from /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:817:in `try_run'
from extconf.rb:24:in `<main>'
Gem files will remain installed in /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12 for inspection.
Results logged to /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12/ext/gem_make.out
I ran into this issue today and it seems that my download of ruby-2.0.0 did not work properly. I would recommend trying rvm reinstall 2.0.0 followed by gem install rails.
Finally, I got the solution! Run rvm implode,and use rm to delete /etc/rvmrc and ~/.rmvrc, restart your mac, and you can use gem install rails to install rails successfully.
First I installed gcc via homebrew:
installing gcc on mavericks using brew
brew tap homebrew/dupes
brew install apple-gcc42
Then I symlinked the newly installed apple-gcc42
into my /usr/bin folder
cd /usr/bin
sudo mv gcc gcc_mavs sudo ln -s /usr/local/Cellar/apple-gcc42/4.2.1-5666.3/bin/gcc-4.2 gcc
Credits: https://coderwall.com/p/lqpp8w
I had this same error when running against ruby-2.0.0-p195, but updating to ruby-2.0.0-p247 (the current RVM default for 2.0.0) fixed it.
I'm new to Ruby on Rails, and I'm getting the below error when I'm installing it.
Please note, I have installed the latest Xcode and installed command line tools, and I'm using OSX 10.8.4. I've installed Ruby 2.0.0 as well.
-bash-3.2$ sudo gem install rails
Password:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:519:in `try_link0'
from /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:817:in `try_run'
from extconf.rb:24:in `<main>'
Gem files will remain installed in /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12 for inspection.
Results logged to /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12/ext/gem_make.out
I had similar problem and it gets solved by installing command line tools:
xcode-select --install
The symlinking did not solve the problem for me. I uninstalled my ruby that I previously had installed using rvm by typing rvm uninstall ruby-2.0.0 and then I typed rvm requirements. This command found that I needed a few other things, such as gcc46. It's strange that the gcc compiler was needed, even though I had installed the xcode command line tools, and gcc was most definitely installed on my system.
So, if none of the usual methods are working, give rvm requirements a shot!
I guess it's a same problem with this question.
Try sudo ln -s /usr/bin/{llvm-,}gcc-4.2 and gem install rails again.
By the way, you're in your own home directory, you don't have to use sudo.
I just ran into this with ruby 2.5.3 and was able to install cairo by calling:
PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig gem install cairo -v '1.15.2'
Took a while to find something that worked, as I already had the command line tools installed, and am using asdf not rvm.
Thanks to this thread for helping with the fix: https://github.com/ruby-gnome2/ruby-gnome2/issues/259#issuecomment-59751807