How to create Migration File in rails? [duplicate] - ruby-on-rails

I have done all kinds of research and tried many different things. I know this question has been answered many times, but none of the suggested solutions are working for me.
After upgrading to Lion I am getting segmentation faults in Ruby. I'm fairly confident it's Nokogiri. So I installed libxml2 via Homebrew. I ran brew link libxml2. Then I reinstalled Nokogiri using that version of the library.
For proof:
$ nokogiri -v
# Nokogiri (1.5.0)
---
warnings: []
nokogiri: 1.5.0
ruby:
version: 1.9.2
platform: x86_64-darwin11.0.0
description: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
engine: ruby
libxml:
binding: extension
compiled: 2.7.8
loaded: 2.7.8
I've already included Nokogiri at the top of my gemfile and I've also required it in my environment file. I have no idea why I am still getting that warning.
Any suggestions or ideas to make sure it's loading the right version libxml2?

If you installed Nokogiri with gem install nokogiri, you can resolve this warning by running gem pristine nokogiri to recompile the gem's C extension.
If you installed Nokogiri with bundle install, you can resolve this warning by running bundle exec gem pristine nokogiri to recompile the C extension of the gem wherever Bundler installed it.

To fix this if you're using homebrew and bundler, add gem 'nokogiri' to the top of your Gemfile, then run these commands:
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
bundle install
If you don't use bundler, run these commands instead:
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
In your app, you should require nokogiri first, to force the app to load the dynamic libxml2 library instead of the older system version of libxml2 loaded by gems that failed to specify which library to load.

I just spent the better part of the morning working through this warning. This fix is for people using Mac OS Lion. The fix above using
bundle config build.nokogiri --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local
is for Snow Leopard with libxml2 installed via MacPorts.
With Lion, libxml2 is loaded as part of the bootstrap process. Regardless of which libxml2 Nokogiri is pointing to, the Lion system default library for libxml2 will be used at runtime. Lion uses libxml2.2.7.3 found in /usr (not /usr/local).
As mentioned many other places, one can just ignore the warning. If, like me, the warning drives you crazy, you can do this:
bundle config build.nokogiri --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local
Interestingly, if you type nokogiri -v at the command line you will get the opposite warning:
WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8
This suggests there is more to how libxml2 is being loaded, with Ruby and Rails using the system loaded libxml2 and the command line using libxml2 from the environment path. Anyway, this silences the error for me.
I’ll say it again – this is only for Lion. The previous fix will work for Snow Leopard.
This is the end of the answer. Stop reading here.
OK, you didn’t stop reading... well...
NOT RECOMMENDED!!!!!!
You have been warned. You can verify that Mac OSX is loading the libxml2 library in its bootstrap by disabling libxml2 found in /usr/lib. Do something like copying all versions of libxml2*.dylib to libxml2*.dylib.old (on my machine this was libxml2.2.7.3, libxml2.2 and libxml2).
After you have done this, running Nokogiri will not produce any errors. That is because it can’t find the loaded libxml2 and will now follow the environment path, eventually finding libxml2.2.7.8 in /opt/local.
BUT you won’t be able to copy the old libxml files back. This is because the OS needs the libxml2 that was loaded in the bootstrap.
Powering off and powering on again will brick your machine. The login screen will hang and hang and hang. Power off and power on again in single-user mode (hold Command-S while rebooting). You can watch the bootstrap occur. Low and behold, it throws an error that it can’t load libxml2 and then stops working.
Power off and power on again. This time boot into recovery mode (either hold Command-R or hold Option and then select the recovery disk). In recovery mode open the terminal (utilities/terminal). Mount /usr/lib on your HD (try /Volumes/Macintosh\ HD/usr/lib) and copy the libxml2 files back. Reboot and all will be fine.

None of this worked for me.
I had libxml2 installed at a later version (2.7.8) with brew. This caused nokogiri to compile against it and the later problems. Solution, remove it, then build, then install if desired.
Here's what worked:
brew uninstall libxml2 (if previously installed)
gem uninstall nokogiri
gem install nokogiri
brew install libxml2 (optional)

The solution (for me) after updating to Mountain Lion was much simplier:
gem uninstall nokogiri
# (and ignore the warnings about dependencies)
gem install nokogiri

As per the comment from patrickmcgraw above, simply putting nokogiri as the first entry in my Gemfile worked for me. I'm putting it as a separate answer because the original comment has been buried.
source 'http://rubygems.org'
gem 'nokogiri'
gem 'rails', '3.0.20'
etc...

Bundler has options to set the default build locations. So for instance, with libxml2 installed via macports:
$ bundle config build.nokogiri --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local
After doing this and bundle install, the warning went away.
There's also some helpful examples for setting build options on the nokogiri wiki.

Looks like you have updated your system libraries after installing the gem, so you have to update Nokogiri. To use the current lib version:
gem install nokogiri -- --use-system-libraries

I had similar problem and just solved this way:
In my case, I have RVM installed, and I had #global and #project gem sets.
Both of them had nokogiri installed and one of them had built with with different libxml.
Rebuilding both of them (I have reason to do this) solved the problem.
Hope this helps..

gem uninstall nokogiri
bundle #install nokogiri again
If that fails with "libxml2 is missing." and you see gems/nokogiri-1.5.0/ext/nokogiri/mkmf.log trying to use "/usr/bin/gcc-4.2 ...", then you're missing /usr/bin/gcc-4.2
Solution:
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2
Before:
$ ll /usr/bin/gcc*
lrwxr-xr-x 1 root wheel 12 Jan 15 00:16 /usr/bin/gcc -> llvm-gcc-4.2
After:
$ ll /usr/bin/gcc*
lrwxr-xr-x 1 root wheel 12 Jan 15 00:16 /usr/bin/gcc -> llvm-gcc-4.2
lrwxr-xr-x 1 root wheel 12 Jan 15 21:07 /usr/bin/gcc-4.2 -> /usr/bin/gcc
If you're really missing libxml2 libxslt, then
brew update
brew install libxml2 libxslt
brew link libxml2 libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2/ --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib/ --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
bundle
FYI: I'm running Mountain Lion with brew, and bundler.

gem install libxml-ruby helps me

I actually had 2 versions of libxml installed, one from source, one from an RPM.
The following is my complete solution
I uninstalled source (from the source directory)
sudo make uninstall
Remove bundles
rm -rf ~/.bundle ~/.bundler
Updated LD (might have to do this as root, not sudo)
sudo ldconfig
Then reinstalled the bundle
bundle install

Just ran into this myself (OS X Lion 10.7.5). My exact message was: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.3
I tried a few suggestions mentioned here, none worked, but this did:
gem install nokogiri -- --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local
The explanation is: "This happens because the Lion system default libxml2 (loaded at bootstrap) is used, regardless of which libxml2 Nokogiri was built against."
Credits to: https://coderwall.com/p/o5ewia

OS : Maverick 10.9.3
Ruby 1.9.3
WARNING: Nokogiri was built against LibXML version 2.9.1, but has dynamically loaded 2.9.0
My solution:
gem uninstall nokogiri
brew update
cd /usr/local
brew versions libxml2
git checkout 5dd45d7 /usr/local/Library/Formula/libxml2.rb # libxml version 2.9.0
brew install libxml2
bundle install or gem install nokogiri -v "1.5.11"
Hope this help

If you have this message and your nokogiri is out-of-date with the version available from the gem source, simply run bundle update nokogiri to get the new code and recompile. Your error should go away.

OS: Catalina
Warning: warning nokogiri was built against libxml version 2.9.10 but has dynamically loaded 2.9.4
I followed Michiel de Mare steps, but brew install libxml2 --with-xml2-config failed with invalid option error. So I installed libxml2 and libxslt and took note of the output from both commands.
brew install libxml2
==> Downloading https://homebrew.bintray.com/bottles/libxml2-2.9.10_2.catalina.bottle.tar.gz
Already downloaded: /Users/alberto/Library/Caches/Homebrew/downloads/9ddf5cb90fd16a7eb531e37bb748fd392f30214d9fe1568b2b70d28cc368c8f7--libxml2-2.9.10_2.catalina.bottle.tar.gz
==> Pouring libxml2-2.9.10_2.catalina.bottle.tar.gz
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have libxml2 first in your PATH run:
echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc
For compilers to find libxml2 you may need to set:
export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
For pkg-config to find libxml2 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"
==> Summary
🍺 /usr/local/Cellar/libxml2/2.9.10_2: 280 files, 10.6MB
brew install libxslt
==> Downloading https://homebrew.bintray.com/bottles/libxslt-1.1.34.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/cbadecf3186f45754220dff4cbdfbb576882a211d615b52249a4c9d8ba4d7c3a?response-content-disposition=attachment%3Bfil
######################################################################## 100.0%
==> Pouring libxslt-1.1.34.catalina.bottle.tar.gz
==> Caveats
To allow the nokogiri gem to link against this libxslt run:
gem install nokogiri -- --with-xslt-dir=/usr/local/opt/libxslt
libxslt is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have libxslt first in your PATH run:
echo 'export PATH="/usr/local/opt/libxslt/bin:$PATH"' >> ~/.zshrc
For compilers to find libxslt you may need to set:
export LDFLAGS="-L/usr/local/opt/libxslt/lib"
export CPPFLAGS="-I/usr/local/opt/libxslt/include"
For pkg-config to find libxslt you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/libxslt/lib/pkgconfig"
==> Summary
🍺 /usr/local/Cellar/libxslt/1.1.34: 136 files, 2.8MB
And I used those directories when configuring bundle for nokogiri
bundle config build.nokogiri --with-xml2-include=/usr/local/opt/libxml2/include --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xslt-dir=/usr/local/opt/libxslt
To summarize I executed these steps
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2
brew install libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/opt/libxml2/include --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xslt-dir=/usr/local/opt/libxslt
bundle install

Related

El Capitan Update rails

Just updated my system to El Capitan and when launching the rails server (that worked perfectly on Yosemite) I get the following warning.
Ignoring bcrypt-3.1.10 because its extensions are not built. Try: gem pristine bcrypt --version 3.1.10
Ignoring bcrypt-ruby-3.1.2 because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.1.2
Ignoring bcrypt-ruby-3.0.1 because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.0.1
Ignoring binding_of_caller-0.7.2 because its extensions are not built. Try: gem pristine binding_of_caller --version 0.7.2
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
when running the referenced commands I get:
-bash: bundle: command not found
rails update
Ignoring json-1.8.1 because its extensions are not built. Try: gem pristine json --version 1.8.1
Ignoring json-1.8.2 because its extensions are not built. Try: gem pristine json --version 1.8.2
Ignoring json-1.8.3 because its extensions are not built. Try: gem pristine json --version 1.8.3
Ignoring nokogiri-1.6.6.2 because its extensions are not built. Try: gem pristine nokogiri --version 1.6.6.2
Ignoring atomic-1.1.14 because its extensions are not built. Try: gem pristine atomic --version 1.1.14
Ignoring atomic-1.1.14 because its extensions are not built. Try: gem pristine atomic --version 1.1.14
Ignoring bcrypt-ruby-3.0.1 because its extensions are not built. Try: gem pristine bcrypt-ruby --version 3.0.1
Ignoring binding_of_caller-0.7.2 because its extensions are not built. Try: gem pristine binding_of_caller --version 0.7.2
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
Could not find bcrypt-3.1.10 in any of the sources
Run `bundle install` to install missing gems.
also tried running:
sudo gem pristine --all
Restoring gems to pristine condition...
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/CFPropertyList-2.2.8/README
So now, it can't even find the bundle install path. Seems like the update has really messed it up.
When I run gem pristine --all I get the following:
Restored bcrypt-ruby-3.1.5
Skipped bigdecimal-1.2.0, it is a default gem
Building native extensions. This could take a while...
Restored binding_of_caller-0.7.2
Restored bootstrap-sass-3.3.5.1
Restored builder-3.2.2
Restored builder-3.1.4
Restored bundler-1.10.6
Restored bundler-1.9.6
Building native extensions. This could take a while...
ERROR: While executing gem ... (Gem::Ext::BuildError)
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -r ./siteconf20151001-3494-1jca623.rb extconf.rb
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
make: *** No rule to make target `/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h', needed by `breakpoint.o'. Stop.
make failed, exit code 2
Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/byebug-6.0.2 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-15/2.0.0/byebug-6.0.2/gem_make.out
Solution is in the following link.
Hardware
Upgrade to El Capitan, with Homebrew & Ruby
... and Xcode and Java, etc.
If you don't already have homebrew installed, do that first, so you don't have to deal with SIP issues. Install all Software Updates available in the Apple Menu, up to and including El Capitan.
After the installs and forced reboots my 27" Thunderbolt display wouldn't display anything. Unplug, replug didn't help. One more reboot fixed it.
Software
In order...
Xcode
The El Capitan install wiped out my Xcode install, so I had to search for it in the App Store and reinstall. I now believe that I deleted Xcode awhile ago and forgot, hence the reinstall. I did this on a machine that I rarely use.
After Xcode is finished installing, open iTerm, which I use as a Terminal.app replacement, or Terminal.app if you are so inclined.
∴ xcode-select --install
xcode-select: note: install requested for command line developer tools
A dialog opened, I chose "Install", which finished in about a minute.
Java
Next, run java, which prompts with a dialog that has a button "More Info" which takes you to a website where you can accept the license and download the Java .dmg file. You need the JDK, not the JRE, if you intend to run java command-line based tools.
Unable to find any JVMs matching version "1.7".
No Java runtime present, try --request to install.
∴ java
No Java runtime present, requesting install.
Once downloaded, open the .dmg (for me it was jdk-8u60-macosx-x64.dmg) and install, then restart iTerm.
∴ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
Apps like RubyMine should work now, well, at least launch.
Homebrew
The doctor informed me of several warnings which I took care of as instructed.
∴ brew doctor
∴ sudo chown -R $(whoami):admin /usr/local
∴ cd /usr/local/Library && git stash && git clean -d -f
∴ cd ~
∴ brew prune
∴ brew unlink qt5
Then I was able to update Homebrew.
∴ brew update
qt5
I know the capybara-webkit gem will require qt5, and I know qt is one of the primary remaining pain points in homebrew / El Capitan issues tracker, so I decided to get it out of the way first.
∴ brew uninstall qt5
∴ brew install qt5
∴ brew linkapps qt5
∴ brew link --force qt5
That last line is required to make qmake available. Check with which qmake. Otherwise you will end up with this on the Capybara install later on:
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/pboling/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20150930-48087-1vw6bye.rb extconf.rb
sh: qmake: command not found
*** extconf.rb failed ***
GPG
Some software is now being signed with GPG, like RVM, for example, so install it.
∴ brew install gpg
RVM
My Ruby was working, but I was having trouble with some native gem installations, so I decided to start fresh with a Ruby compiled against all the new homebrew and Xcode libraries.
∴ rvm implode
Implode actually failed to remove the main ~/.rvm directory due to permissions issues. I had to sudo remove it.
∴ sudo rm -rf /Users/pboling/.rvm
Follow the instructions here for installing RVM, I installed the development version, hoping it would have more El Capitan fixes in it:
∴ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
∴ \curl -sSL https://get.rvm.io | bash
The main app I currently work on is using Ruby 2.1.2, so I install that first, and then go to the project directory to have the gemset created and to bundle.
∴ rvm install 2.1.2
∴ cd ~/project
∴ gem install bundler -v 1.9.7
∴ bundle install
Bundler version 1.9.7 because that's what Heroku is on, and I want to stay in sync, because 1.10* changed things that make the Gemfile.lock incompatible with 1.9.7 on the deployed server (for me, with multiple gem sources, private gem server, etc).
Postgres
I also took the opportunity to upgrade to the latest Postgres.app. Alternatively you can install postgres with brew install postgres. I choose Postgres.app begause it is supported directly by Heroku, and I like to think there is a parity win there somewhere.
You should try RVM or RBENV or whatever the flavor of the week is. This will allow you to easily manage your Ruby Interpreters and Gemsets on a per project basis. I still use RVM and I'm quite happy with it.
https://rvm.io/rvm/install

Nokogiri problems with LibXML on Mountain Lion

I recently did a clean install of Mountain Lion, and after installing Nokogiri - got an error when starting up the Rails console:
WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8
So I looked at other questions here on SO, and uninstalled everything and tried again, but I noticed the install instructions are out of date on the Nokogiri website: http://nokogiri.org/tutorials/installing_nokogiri.html
Because 'brew install libxml2 libxslt', actually installs libxml2 2.8.0, and reading further down the instructions it refers to 2.7.8:
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2
--with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib
--with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26
--with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include
--with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib
(notice libxml2/2.7.8)
So I uninstall again, and reinstall with: (libxml2/2.8.0)
sudo gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib
And while it seems to work fine in IRB, it doesn't in Rails C - it still says:
WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8
I've tried running bundle update, but it's still the same.
Any ideas how I can fix this please?
so you first ran gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib ... and it still said "Nokogiri was built against LibXML version 2.8.0"? that looks almost as if it didn't pay attention to the switches.
anyway, the problem is exactly as stated by your computer: the libxml*.so it uses at runtime is the old version. try man ldconfig, look in the standard dirs for the old libxml library. it might be enough to put the new one next to it and point the symlink at it.
disclaimer: i'm not an osxer, buyer beware.
Unless you have a pressing need to use a newer version of libxml2, the standard version distributed with OS X Mountain Lion will do just fine. I have used
brew uninstall libxml2
brew uninstall libxslt
gem uninstall nokogiri
gem install nokogiri
to good effect. The re-installation of Ruby 1.9.3 recommended here was not necessary. Of course, libxslt needs to be uninstalled only if you had installed it previously (as I had).
If you do uninstall one of the libraries, you might wish to check via
brew missing
whether you have accidentally removed a dependency.
Check your gemfile and make sure nokogiri is implicitly defined -- don't rely an implied inclusion to give you the correct version of the nokogiri gem.
Then uninstall libxml2 and libxslt if you have them installed via brew, uninstall nokogiri via gem, and reinstall it via bundler install.

WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8

After making a fresh install of Mac OS X 10.8 Mountain Lion, and after installing Ruby 1.9.3 and Ruby on Rails 3.2.6, I started the Rails console and I got this warning message:
WARNING: Nokogiri was built against LibXML version 2.7.3, but has
dynamically loaded 2.7.8
How can I fix it?
I reinstalled Ruby, that fixed it. Was able to use the built-in libraries.
I have found some fixes for Lion, but none for Mountain Lion yet. Nonetheless I have tried this and it works:
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew link libxml2
brew install libxslt
brew link libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2/ --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib/ --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
Source (for Lion): https://gist.github.com/1349681
gem uninstall nokogiri
bundle install
Even Better:
gem pristine nokogiri
Note:
This will happen repeatedly as you upgrade your system and libraries change.
Note that linking as in #Davids post will interfere with your build tools and link into your OS (which is most likely not what you want if you're using brew), but you can just execute each command above except omit the 'brew link' lines and everything will still build just fine, eg:
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2/ --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/lib/ --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
Putting gem 'nokogiri' above gem 'pg' in my Gemfile fixed this for me.
My Gemfile didn't have nokogiri in it, but it was a dependency that was in Gemfile.lock, so I put it in my Gemfile explicitly.
Based on the answer from #David the following steps were enough for me. No compiler flags and custom paths.
gem uninstall nokogiri
brew update
brew install libxml2
brew update libxml2
brew link libxml2
gem install nokogiri

Nokogiri osx 10.5.8 libxml incompatible library version

I have hit upon a stumbling block here trying to get Nokogiri working on osx 10.5.8.
Tried http://nokogiri.org/tutorials/installing_nokogiri.html
and then http://martinisoftware.com/2009/07/31/nokogiri-on-leopard.html
A colleague has been trying to help too but cannot get rid of this error message:
Library not loaded: /usr/local/lib/libxml2.2.dylib
...
Reason: Incompatible library version: nokogiri.bundle requires version 11.0.0 or later, but libxml2.2.dylib provides version 10.0.0
We have tried to compile the gem using the newly installed libraries but still coming up against this error.
Any help or pointers really appreciated!
I have been given a copy of osx 10.6 - could this help??
Thanks
Actually, the answer is in: http://nokogiri.org/tutorials/installing_nokogiri.html
brew install libxml2 libxslt
brew link libxml2 libxslt
Then install libiconv from source:
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
make
sudo make install
Then (finally) install nokogiri:
gem install nokogiri --
--with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2
--with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib
--with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26
--with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include
--with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib
Making sure that you have the versions of libxml2 as you have them in /usr/local/Cellar/libxml (in my case it was 2.8.0).
Happy coding!
The following worked for me on OS-X 10.9.1 (Mavericks):
brew install libxml2 libxslt
gem install nokogiri -- --with-xslt-dir=/usr/local/opt/libsxlt
I just hopped on my 10.5 machine and remembered getting a working version of Nokogiri there - so looked up the command history and here's a cut'n'paste of my exact commands, using Homebrew:
brew install libxml2 libxslt
brew install libiconv
sudo gem install nokogiri -- --with-xslt-dir=/usr/local/homebrew/Cellar/libxslt/1.1.26
Although I'd still upgrade to 10.6.
Older answer:
This answer might help. After resisting OS X package managers, I gave in to Homebrew which helped me solve similar issues. This is for RVM (where I remember going through a similar process) but the key is to tell the nokogiri gem to use the new libxml2 install.
BTW (slightly off-topic but relevant if you're going to 10.6) OOI you can check your mac for a 64 bit EFI in Terminal:
ioreg -l -p IODeviceTree | grep firmware-abi
you should see (if 64 bit):
"firmware-abi" = <"EFI64">
although (as detailed in the replies to that article) this doesn't mean everything's plain sailing...

Nokogiri installation fails -libxml2 is missing

I always worked my way around Nokogiri installation issues by following the documentation in the "Installing Nokogiri" tutorial.
But this time, even after installing all the dependencies, Nokogiri hasn't been installed. I get the following error:
libxml2 is missing. please visit <http://nokogiri.org/tutorials/installing_nokogiri.html>
I tried installing it by specifying the libxml2 and libxslt directories:
sudo gem install nokogiri -- --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-dir=/usr/
but it returned the same error.
I followed all the other related Stack Overflow articles and none helped. Does anyone have a solution?
You may actually need to install both of these packages
sudo apt-get install libxslt-dev libxml2-dev
First, install the dependencies:
sudo apt-get install libxslt-dev libxml2-dev
If you still receive the error, you may be missing a compiler toolchain:
sudo apt-get install build-essential
You'll get the "libxml2 is missing" error if you're missing a build toolchain (at least I ran into this issue on Debian Lenny).
The Nokogiri build test-compiles a libxml2 header file to verify that it is present, however, it doesn't differentiate between "libxml2 is missing" and "a compiler to test libxml2 is missing".
In Mac OS X (Mavericks), installing the libraries with brew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.
Summarising:
If previously installed, uninstall the gem:
gem uninstall nokogiri
Use Homebrew to install libxml2, libxslt and libiconv:
brew install libxml2 libxslt libiconv
Install the gem specifying the paths to the libraries to be linked against:
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
I just had the same issue on Fedora 13. After a frustrating and unsuccessful search to make
gem install nokogiri
work for me, I was able to install it and get around the libxml2 error via yum.
Simply install the gem via yum instead of the gem command:
su
yum search rubygem-nokogiri #this find the proper package name
yum install rubygem-nokogiri.i686
This helped me find the right answer for Fedora and, as I am using RVM for Ruby package management,
yum install rubygem-nokogiri
will pull in all the Ruby gems and dependencies into the system, not into my RVM environment, and in my experience that leads to a very frustrating and humbling experience.
So, taking your find of the Nokogiri yum gem you can use:
yum provides rubygem-nokogiri
and get a list of the dependencies for rubygem-Nokogiri which showed me the libraries that were missing. After that I ran:
yum install libxml2-devel libxslt libxslt-devel
Now Nokogiri compiles in Fedora and Nokogiri installs. D'oh!, we need the headers to compile Nokogiri from the devel libraries.
In Mac OS X (Mavericks) if none of these solutions work, try:
ARCHFLAGS="-arch x86_64" gem install nokogiri
or
ARCHFLAGS="-arch i386" gem install nokogiri
depending on your system's architecture.
You usually need development files for building gems. Try:
sudo apt-get install libxslt-dev libxml2-dev
(I just saw that Eric suggested the same in a comment.)
I was able to get this installed with Chocolatey, Windows 8.1 x64, and DevKit x64.
cinst libxml2
cinst libxslt
cinst libiconv
gem install nokogiri --
--with-xml2-include=C:\Chocolatey\lib\libxml2.2.7.8.7\build\native\include
--with-xml2-lib=C:\Chocolatey\lib\libxml2.redist.2.7.8.7\build\native\bin\v110\x64\Release\dynamic\cdecl
--with-iconv-include=C:\Chocolatey\lib\libiconv.1.14.0.11\build\native\include
--with-iconv-lib=C:\Chocolatey\lib\libiconv.redist.1.14.0.11\build\native\bin\v110\x64\Release\dynamic\cdecl
--with-xslt-include=C:\Chocolatey\lib\libxslt.1.1.28.0\build\native\include
--with-xslt-lib=C:\Chocolatey\lib\libxslt.redist.1.1.28.0\build\native\bin\v110\x64\Release\dynamic
You'll have to verify the version number in the paths are correct.
You may possibly need to add Microsoft's NuGet repository:
-Source "https://go.microsoft.com/fwlink/?LinkID=230477"
It will be:
sudo yum install -y libxml2 libxml2-devel
on RHEL servers.
At macOS none of above/below had really worked for me until I explicitly provided XCode libxml2 path to --with-xml2-include.
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2 --use-system-libraries
I was able to install Nokogiri 1.6.5 on Fedora 20 by doing:
export NOKOGIRI_USE_SYSTEM_LIBRARIES=true
Then running:
gem install nokogiri
For anyone else experiencing this issue, I solved it by running this command within the project directory
gem install nokogiri -- --use-system-libraries
Update: I ran into this again and tried just updating the gem which worked for me...
gem update nokogiri
Have you tried installing libxml2? Not from rubygems, but via the standard install process for your operating system. On Ubuntu/Debian for example:
sudo apt-get install libxml2
On any recent version of OS X it should already be installed.
For OSX users, if you've had success installing Nokogiri before, yet are getting errors installing it on, say, using a new version of Ruby that you've added and that error that includes a message like:
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
This could be indicative of a XCode upgrade having happened via the App Store, either manually or automatically, and you not having opened it since that time.
If so, you should be able to open XCode, agree to the new license, and then install Nokogiri successfully.
On Mac OS X Yosemite my mistake was that I tried to use sudo gem install when it's a rule of thumb to not use superuser privileges when installing gems.
In my case it tried to modify the system installation of Ruby, and that's not a good idea. I installed rbenv, installed Ruby 2.2.2 thru it and set it as global, which is a term from rbenv's documentation. After that Nokogiri could install itself with a simple
gem install nokogiri
No hacks or workarounds were necessary, just a properly set environment; Nokogiri does the rest perfectly.
Was able to install vagrant-awe by following the above post but using the command as follow:
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 vagrant plugin install vagrant-aws
For Windows x64:
gem inst nokogiri --pre --platform ruby
For more information check this thread: https://github.com/sparklemotion/nokogiri/issues/864
I was able to install Nokogiri by running the following packages:
$ dnf group install "C Development Tools and Libraries"
$ dnf install ruby-devel libxml2-devel patch
=== TLDR ===
As the instructions state, build Ruby with the latest clang compiler, or at least the same version that Rubygems will use to compile Nokogiri's native extensions libxml and libxsl.
If using RVM, building from source with the --with-ggc=clang flag was what did it for me:
rvm install 2.2 --with-gcc=clang
Replace 2.2 with whatever version you want. --with-gcc=clang ensures RVM builds from source and uses clang to do so; otherwise RVM may a pre-built binary Ruby, which is what tripped me up.
Then, install Nokogiri as normal using Bundler or Rubygems.
=== IN DEPTH ===
I struggled with this for a while. gem install nokogiri gave me:
checking for xmlParseDoc() in libxml/parser.h... no
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
Digging into mkmf.log I saw:
conftest.c:15:27: error: too few arguments to function call, single argument 'cur' was not specified
int t(void) { xmlParseDoc(); return 0; }
Nokogiri supplies its own libxml and libxsl (as of 1.6.4). The signature defined in Nokogiri's local copy of parser.h (found under the gem install directory) is:
xmlParseDoc (const xmlChar *cur);
So I was at a loss as to how the method call in the hermetically sealed conftest.c file usage couldn't match up with the header file for parser.h.
When I realized I had probably installed a binary Ruby I removed and reinstalled using --with-gcc=clang (to force compilation and use clang) and the problem was solved:
rvm uninstall 2.2
rvm install 2.2 --with-gcc=clang
gem install nokogiri
I'm not exactly sure why that works as the system libxml header /usr/include/libxml2/libxml/parser.h has the same signature as Nokogiri's local copy.
It's weird, but it worked. Just make sure you compile a Ruby with clang.
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Change your version with it.

Resources