Haskell on Windows
Haskell is fascinating, but getting started can be a bit rough – especially if you are on Windows. Here are some necessary steps to get you over these initial stumbling blocks when running the Haskell Platform (2012.4 or 2014.2) on Windows.
It is quite obvioius that a lot of the users of Haskell are on other platforms, and as a Windows-user you quickly run into some problems not experienced on other platforms. This is doubly frustrating as a newbie because you haven’t had time to develop the necessary intuition about where to look for solutions yet. It also certainly does not help that Haskell error messages are known to be misleading.
Making Networking code work on Windows
All networking code, both clients and servers, must be wrapped in withSocketsDo. The withSocketsDo construct initializes the winsock library which is a prerequisite for all networking on Windows. This is not required on any other Haskell platform. On all other platforms, withSocketsDo does nothing, and can therefore safely be added to make code run on all platforms.
The most common error message indiciating a missing withSocketsDo is
getAddrInfo: does not exist (error 10093)
Apart from your own code, several great (and widely-used) libraries also suffer from this problem. In this case just try wrapping the main function call. In the popular live diagnostics package ekg by Tibell, this is done by replacing the call to forkServer with withSocketsDo $ forkServer. I have submitted a patch to Tibell so expect this problem to be solved in future releases. But a lot of networking libraries remain.
The withSocketsDo function requires
import Network.Socket (withSocketsDo)
Adding Cygwin to the path
When installing packages with cabal, you will sometimes encounter the error message
cabal.exe: The package has a ‘./configure’ script. This requires a Unix compatibility toolchain such as MinGW+MSYS or Cygwin.
This is easy to solve, you just need to install CygWin and then make sure the binaries are in your path. If you install cygwin to c:\cygwin then the binaries are in c:\cygwin\bin. To add c:\cygwin\bin to your path temporarily you can use a command prompt and do
set path=%path%;c:\cygwin\bin
Or you can go to Control Panel, System, Advanced, Environment Variables, Edit and add it there permanently. While you are editing paths, look for c:\program files\haskell\bin. If it is there, just delete it. The Haskell Plattform installer (at least as of 2014.02) adds this non-existing path by mistake.
CygWin is a great product, have a look at some of the tools it includes and you will quickly find that you need it on all your windows boxes.
Upgrading Cabal
Inevitably, you will run cabal update and get the message
Note: there is a new version of cabal-install available.
As the good citizen you are you will probably want to keep things up to date and try
cabal install cabal-install
You will be greeted with a successful install with no error messages. Yet on your next run on cabal update you will get the same error. Double-checking with cabal –version will tell you that even though the upgrade succeeded, you are still running the old version. How nice.
The reason is that the new cabal is installed in your user folder (for example c:\users\<username>\appdata\roaming\cabal\bin), while the original one bundled with the Haskell Platform is located, well, with the platform (rather unexpectedly in C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin). Since your user folder is either not in the path, or in the path, but later than the platform, you are still executing the old cabal.
The solution is to delete or rename the old cabal binary.
Installing Encoding
The encoding package is a godsend for anyone working with a variety of character set encodings. The package will not install on Windows unless you set a flag:
cabal install -f-systemencoding (yes, both dashes are correct)
Replacing tabs with spaces
If you are starting out with creating your initial projects in an editor, you may get some surprising parsing errors where all seems well. The reason is that Haskell by default is white-space aware, which, by the way, is a great characteristic it shares with Python. The easiest way to make the parsing errors go away is to set your editor to replace tabs with spaces. In Notepad++ (free, and more than sufficient for your first few projects), this is done by going to Settings, Languages and selecting “replace by space” under Tab Settings. Surprisingly enough, this is not default in the built-in Haskell language and syntax highlighting settings in Notepad++.
Nice tips. Some remarks:
In my experience, MSYS is preferable to Cygwin. I also usually add GHC_DIR\mingw\bin to PATH to avoid potential problems due to different versions of gcc.
The cabal upgrade issue has been fixed in the Git version of the Haskell Platform installer, I believe. %APPDATA%/cabal/bin is now put before %PLATFORM_DIR/bin% in PATH.
Useful tips! Thanks for posting this.
Thanks much. I have also found that on Windows you may have to use older versions of packages than the most current (such as the repa package).
http://msysgit.github.io/
This gives you git and msys in one go, so you can run configure scripts. Makes things slightly less painful.
Useful stuff. GHC on Windows needs more love. Lots of people use GHC on Windows but somehow very few people are willing ot give much help and support. You are an exception — thank you.
A suggestion: would you feel like adding your insights to the GHC-on-Windows guidance page?
http://www.haskell.org/haskellwiki/Windows
It’s a wiki so you can edit it yourself. You could point to your blog post or (better for readers) just fold your stuff into the material already there.
It’s linked from GHC’s documentation page
http://www.haskell.org/haskellwiki/GHC
(though perhaps not prominently enough.
Thanks!
Simon
Highly descriptive post, I liked that bit. Will there
be a part 2?
Hello everyone!
I don’t know why Windows code should be treated differently. I think that call to withSocketsDo could be internally made on-demand basis or something like that.
This problem with paths for cabal install could be resolved into the Haskell Platform installer directly and no one will hit it in the future.