Installing libssh2 for Haskell on Windows

Although the thought of setting up and actually getting two major open source frameworks linking on Windows seems daunting, the process of building and installing libssh2 for Haskell is relatively easy. To start off, you'll need to make sure that you have some kind of Haskell installation (preferably the latest Haskell Platform) and an installation of MSYS2.

MSYS2 ships with the beautiful package manager pacman, so we can use it to automatically fetch the development packages we need. First, open the MinGW shell and upgrade your package list and system:

$ pacman -Syu

This may take a while and it's very likely you'll need to restart your shell afterwards as it often begins to throw errors after a major update of the runtime components. Then, let's install the two libraries we need to build libssh2-hs and their development packages (header files):

$ pacman -S base-devel binutils openssl-devel libssh2-devel

If you choose to make GHC's MinGW installation available in the MSYS2 path, you need not install binutils. Next, make GHC and Cabal available to us inside MinGW:

$ export PATH=$PATH:"C:\Program Files\Haskell Platform\2014.2.0.0\bin"
$ export PATH=$PATH:"C:\Program Files\Haskell Platform\2014.2.0.0\lib\extralibs\bin"

Depending on what version of the Haskell Platform you installed, you may need to adjust the path. Next, fetch the latest package list for Cabal:

$ cabal update

Since libssh2 uses c2hs for generating bindings, we need to install it first:

$ cabal install c2hs

Finally, we can install the libssh2 package. We need to let it know about the extra search directories available in MSYS2:

$ cabal install libssh2 --extra-include-dirs="C:\msys64\usr\include" --extra-lib-dirs="C:\msys64\usr\lib"

At this point your installation should succeed and the libssh2 bindings will be available for linking from outside your MinGW environment. Note that if you're using 64-bit Windows, it's imperative that you install only the latest version of libssh2, as I recently submitted a pull request to fix build issues which occur on that platform.