Search code examples
windowsgolibxml2

Go lang: how to install libxml2/gokogiri on windows


If there a relatively simple way to make go + libxml2 + gokogiri work on windows?

I mean that I may be can install it (but at the moment I can not, stuck with Package libxml-2.0 was not found in the pkg-config search path), but then I need to provide my utilite to other people, who will never be able (or would wish ) to install lall libxml2 dependencies, modify PATH etc on windows...

It work flawless on Ubuntu...

I found this https://github.com/moovweb/gokogiri/issues/49 thats funny with installation of Gimp 2 (what?!), but I still cannot make it run with such error, I guess might be issue with PATH, but all PATH are set

$ go get github.com/moovweb/gokogiri
# github.com/moovweb/gokogiri/help
Documents\go\src\github.com\moovweb\gokogiri\help\help.go:6:25: fatal error: lib
xml/tree.h: No such file or directory
 #include <libxml/tree.h>
                         ^
compilation terminated.
# github.com/moovweb/gokogiri/xpath
Documents\go\src\github.com\moovweb\gokogiri\xpath\expression.go:4:26: fatal err
or: libxml/xpath.h: No such file or directory
 #include <libxml/xpath.h>
                          ^
compilation terminated.

Solution

  • You are struggling because it is hard to combine packages that were built by different people for different purposes and get your environment set up correctly. I think it is best to use MSYS2, an environment for Windows that provides a consistent set of packages for things like gcc, go, libxml2, and iconv. MSYS2 has a package manager (pacman) that helps you easily install them and keep them updated.

    I don't do much programming with Go, but I am familiar with MSYS2 and it seems like I was able to get gokogiri installed using MSYS2. You should open MSYS2's "MinGW-w64 Win64 Shell" from the Start menu (mingw64_shell.bat), and try running these commands:

    pacman -S mingw-w64-x86_64-{gcc,go,libxml2,iconv}
    export GOROOT=/mingw64/
    export GOPATH=/c/Users/David/Documents/goproj/
    mkdir -p $GOPATH
    go get github.com/moovweb/gokogiri
    

    I think GOPATH should be set to the directory of your project. If you run into an error, it might be because some pacman package is required that I didn't list here.

    The string mingw-w64-x86_64-{gcc,go,libxml2,iconv} gets expanded by Bash into the following list of packages:

    mingw-w64-x86_64-gcc
    mingw-w64-x86_64-go
    mingw-w64-x86_64-libxml2
    mingw-w64-x86_64-iconv
    

    If you are actually using 32-bit Windows, replace x86_64 with i686 in the instructions above.

    If you are curious, the scripts for building those packages are here: https://github.com/Alexpux/MINGW-packages

    As a disclaimer, I haven't actually compiled any go programs in MSYS2, so there could be big problems I am unaware of.

    Also, one of the main developers of MSYS2 (alexpux) said this in the #msys2 IRC chat on 2015-06-21:

    We not build go for a long time. This package in very WIP state Also see https://github.com/Alexpux/MINGW-packages/issues/421

    So you might need to fix some issues with the MSYS2 Go package and recompile it yourself to really make this work. But you have the PKGBUILD script that was used to build it, so maybe that will be less hard than what you are trying to do right now, which involves compiling/collecting every dependency of gokogiri.

    MSYS2 would make your other installation of go, libxml2, and iconv obsolete. You can delete those things once you get your MSYS2 environment working.