Haskell on Bash/WSL


I’ve recently received a couple of questions asking when Haskell support would be added to WSL, and was surprised since I thought Bash/WSL users were aware of the fact that Haskell has been working for the last few weeks, ever since #14986 in fact, but it appears that we’d not explicitly communicated the fact, so … here we are! 🙂

Haskell now runs in Bash on Windows!*

* You’ll need to be running Windows 10 Insider build #14986 or later.

Background

Haskell is a popular and powerful functional programming language which wasn’t able to run on Bash/WSL in Anniversary Update or earlier Insider builds, because WSL had not yet implemented the timer_create() syscall. Now that WSL builds >= #14986 do support timer_create() we can run Haskell and many other tools (e.g. Elm, Cabal, Pandoc, Agda, etc.) 🙂

Note: You can, as with most modern dev tools, also run Haskell very happily on Windows, although you may find some packages and add-ons require Linux, which is where the ability to run on Bash/WSL comes in!

Hello World in Haskell

Let’s create a Haskell Hello World sample:

First, install the Glasgow Haskell Compiler:

$ sudo apt install ghc

While we could run the Haskell interactive REPL, the GHC compiler can, of course, also compile Linux executables:

Open your favorite Linux editor (mine is Vim) creating a new hello.hs file

$ vim hello.hs

Now enter the following Haskell code:

main = putStrLn "Hello World!"

This declares a main function whose implementation calls putStrLn that writes “Hello World” to stdout.

After exiting your editor (:wq[Enter] in Vim ;)), compile this file by typing the following:

$ ghc hello.hs -o hello
[1 of 1] Compiling Main             ( hello.hs, hello.o )
Linking hello ...

This will generate a Linux ELF64 binary file called hello, along with hello.hi and hello.o, which are just intermediate compilation files. You can check the hello binary using the file tool:

$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=0296a5fedb69302e0310bbcb7a8bea8f860a52c5, not stripped

You can now run the binary (note: the ./ prefix is required when executing a local binary):

$ ./hello  Hello World!

Congratulations, you’re now on your way to being a Haskell guru 🙂

 

Comments (5)

  1. Shao Cheng says:

    Running GHC on WSL is still dreadfully slow though. A `stack setup` command takes approximately 2 hrs on my Surface Book; with a normal Ubuntu installation it shall never exceed 10min.

    1. Disk IO performance is not yet where we want it to be. We’ve got some minor perf improvements coming in Creators Update, with more substantial improvements planned in future releases. AS you can imagine, we need to take a great deal of care making changes to the IO infrastructure, so it’s not something we can fix quickly. However, do know that we are keen to make big improvements in this area.

  2. radian says:

    Fails to install for me (tried 2 machines)

    1. Which Windows 10 Insider build are you running? What errors are you seeing?

      1. radian says:

        Ah, it was not yet updated to a recent enough build.
        Thanks.

Skip to main content