Sunday, December 12, 2010

Installing the Haskell network library on Windows

Summary: This post describes how to install the Haskell network library on Windows.

The network library used to be bundled with GHC. Unfortunately it was unbundled from the standard installer (in my opinion, a mistake), and people were required to either install it using Cabal (rather tricky) or switch to using the Haskell Platform (which is not suitable for power developers who want to test with newer/older GHC versions, or those who want to upgrade/downgrade network). This post describes how to install the library on Windows using Cabal.

First, install Cygwin. I selected lots of options (configure tools, auto conf, compilers) - but I'm not sure which are necessary.

Then start a Cygwin window and run:

WHICHGHC=`which ghc` && PATH=`dirname $WHICHGHC`/../mingw/bin:$PATH && cabal install network --configure-option --host=i386-unknown-mingw32 --global --enable-library-profiling

This configures network to use mingw32, and sets up the PATH so that mingw binaries from GHC are first, and get configured in. You can then use the network library as normal, without ever using Cygwin again. I have successfully executed this command on GHC 6.12.3 and 7.0.1.

Dependencies on foreign libraries are a problem for any cross platform language. Several years ago, installing Haskell libraries was a painful process - but Hackage and Cabal have made it impressively easy. The only remaining packages that are complex to install are those that bind to foreign libraries, most of which use configure scripts, which are not well suited to Windows. I hope over time even libraries with foreign dependencies will become easy to install.

Update: Ivan Perez suggests the alternative form:

WHICHGHC=`which ghc` && PATH=`dirname $WHICHGHC`/../mingw/bin:$PATH && cabal install network --configure-option --build=i386-unknown-mingw32 --configure-option --host=i686-pc-cygwin --global --enable-library-profiling


Stephen Tetley said...

Hi Neil - I'd suggest its easier to install it with MinGW / MSYS rather than Cygwin. Although I use Cygwin for all shell stuff including GHCi, I use MinGW / MSYS to install Haskell libs that are foreign bindings.

For network I think I just ran configure / build / install on the Setup.hs from MSys - I don't use cabal-install.

Neil Mitchell said...

Hi Stephen. Installing MinGW/MSYS is usually a multi-step process, and it's easy to go wrong if you get the paths/versions wrong between the various stages. Cygwin has a much nicer installer - if you just hit Next several times you are almost guaranteed to end up with a working Cygwin. For that reason, I tend to use Cygwin instead (there was also a reason why in my particular setup MinGW was unsuitable, but that's a side issue).

For MinGW installations, if you are using MinGW binaries (which sounds likely) you have to be careful that you have a MinGW and GHC that agree - otherwise you'll get random errors at link time (I've made that mistake before).

Johan Tibell said...

Neil, I don't quite understand how being bundled with GHC or HP makes a difference. Could you please elaborate?

Neil Mitchell said...

Johan: Network is the one native library that isn't bundled with GHC that ends up being a dependency of most things. If Network was bundled with GHC I could download any GHC snapshot, and use cabal install in the Windows console to get most things up and running. Without bundling, I have to also have Cygwin/MinGW and be familiar with additional tools beyond GHC/Windows - which puts a fairly large additional burden.

As a concrete example, my wife is running GHC 6.12.3. There are some features in 7.0.1 which would be useful to her, but due to the way her machine is locked down she can't install either Cygwin or MinGW, so is stuck on 6.12.3.

PS. Thanks for your work on network, it is appreciated!

Anonymous said...

does this then create a dependency on cygwin1.dll?

Neil Mitchell said...

Anon: No, this creates a pure Windows package, without any Cygwin dependency. Cygwin is being used to run the configure script, but GHC's bundled MinGW is being used to compile the library.

Johan Tibell said...

I think I understand. There are nightly drops of the Windows installer so you can get newer versions of network that way? If so, perhaps we should have nightly drops of the HP installer as well.

Neil Mitchell said...

I really am after nightly versions of GHC - nightly versions of the platform is probably a good idea, but unless it also bundled GHC head it wouldn't be so useful (and the platform changes fairly rarely).

Robert Massaioli said...

Such a useful comment, it worked perfectly first time. That was awesome. Right when I needed it too because windows support for network is not so crash hot.

Andrew said...

I seem to be having the same issue trying to install snap which depends on a newer version of network than ships with Haskell Platform. Couldn't the configure script for Network be rewritten in Haskell to remove the dependency on Unix tools which generally aren't available on a Windows platform ?

Neil Mitchell said...

Andrew: It could be, but it would be a reasonable amount of work - although I would certainly appreciate it.

Ivan Perez said...

Hi Neil,

Thanks for this, post, it was really helpful. The exact line you posted didn't work on my system, but the following did:

WHICHGHC=`which ghc` && PATH=`dirname $WHICHGHC`/../mingw/bin:$PATH && cabal install network --configure-option --build=i386-unknown-mingw32 --configure-option --host=i686-pc-cygwin

(I didn't need profiling and global installation, so I removed that part).

Neil Mitchell said...

Ivan: Thanks for the comment, I updated the post to include your suggestion at the bottom.

Kyle Van Berendonck said...

For MSys2 x86:

cabal install network --configure-option --build=i386-unknown-mingw32 --configure-option --host=i686-pc-mingw32