Setting up a Haskell environment
- by Alejandro Serrano
- November 15, 2019
- haskell• functional• functional programming
- 8 minutes to read.
At 47 Degrees, we love all functional languages, including the venerable Haskell. Haskell, as opposed to languages such as Scala or Kotlin, only provides functional constructs to programmers. It is not strange, thus, that many of the functional libraries in other languages (think of Cats, Scalaz, Arrow, and Bow) are directly inspired by similar libraries coming from the Haskell ecosystem.
Although Haskell is old compared to the other languages mentioned above (the first Haskell Report dates from 1998), the ecosystem has rapidly evolved in the last few years. This is great (more libraries, yay!) but, as a result, tutorials on setting up a working environment do not reflect the current state of the language. This blog post is yet another attempt to provide a simple, step-by-step tutorial to get Haskell running on your machine.
What do you need?
In order to start using Haskell, you only need two things: your terminal, and a build tool. However, to work comfortably, it is also useful to have some simple editor integration.
Step 0 for Mac OS X users
Setting up a development environment in Mac OS X almost irremediably involves installing some subset of XCode, the so called Command Line Tools.
If you have already obtained XCode (for example, from the App Store), you can get the Command Line Tools by opening the Terminal and running the following command:
Note that the program weighs several GBs, so you might consider doing something more fun while you wait. After the installation finishes, you should have programs like the
clang compiler or the
git source control tool readily available.
If you do not want the full XCode package in your machine, you can also go to the Apple Developer Downloads page (although you need to create an Apple Id for this matter), and download the Command Line Tools directly. Be careful to install a version of the tools which is compatible with your Mac OS X version.
Step 1: install Stack
To get the build tool in either Mac OS X or Ubuntu you use the renowned download-and-pipe method. In other words, open a terminal and execute the following command:
curl -sSL https://get.haskellstack.org/ | sh
In Windows, you need to use the installers.
Check that everything works
To check that everything works, you can run the following set of commands:
- Create a new simple project with
stack new my-first-haskell-project simple.
- Go into the newly created folder with
- Build the project with
- Now execute it with
stack run. You should get a
hello worldmessage in the screen :)
The first time you execute
stack build, all the necessary tools are downloaded and installed in a user location (not globally for the whole system).
Note that Stack does not use the usual update mechanisms from any of the operating systems. Instead, from time to time, you are advised to run the following command:
Step 2: set up your editor
In theory, a simple text editor and the build tools is all your need. But what would we programmers do without some syntax highlighting and integration in our editors?
Visual Studio Code
Visual Studio Code is more an editor on steroids, than a full IDE, in the strand of Sublime Text or Atom. If you do not have it already, you can follow the install instructions in the Visual Studio Code website.
The editor integration comes from
ghcide. At the moment of writing this, there’s no automatic installation method for the support scripts, so you need to do it manually.
- Go to a folder where you want to install the scripts. I usually keep a
Sourcesfolder in my home directory.
- Get the source code by running
git clone https://github.com/digital-asset/ghcide.git.
- Move to the newly-created folder,
- Install the program using
Now you should be able to run
ghcide in any Stack project (for example, the
my-first-haskell-project you have created in the previous step). If you get an error about
ghcide not being found, you need to “add the folder
.local/bin in the home directory to your PATH”: this is how you do it in Mac OS X, Ubuntu, and Windows 10.
The last step is configuring Visual Studio Code itself. If you open the folder with the project you created before, you can see that there is no syntax highlighting. Go to the Extensions tab (you can find it by default as the last item on the left-hand side of the window), and look for
ghcide. Then, install the extension and reload the editor.
When dependencies change
As in any other programming environment, Haskell projects may depend on other packages. There are several repositories for them, the most important being Stackage and Hackage. You declare those dependencies in the file ending in
.cabal in your project.
ghcide is currently not able to discover newly-added dependencies by default. Every time you add one, you must follow these steps:
stack buildin your project; this downloads and installs the new dependencies.
- Reload the Visual Studio Code editor.
Add a linter
While not strictly required, HLint is a very useful tool which provides suggestions on how to make Haskell code clearer, more concise, and more idiomatic. It is thus highly recommended if you are learning Haskell.
As in the case of
ghcide, HLint has to be manually installed before using it in the editor. To do so, in a new terminal run:
stack install hlint
After this command successfully completes, you should be able to execute
hlint. If it is not the case, you need to add the
.local/bin folder to your PATH, as explained above (but you must have done it for
Now go back to the Visual Studio Code window to the Extensions tab. This time look for
haskell-linter as the name of the extension. You should start receiving linter warnings after you reload the editor.
The set up process of IntelliJ Haskell is much simpler than Visual Studio Code. This is mostly because the plug-in itself takes care of downloading and installing every necessary component. Although we refer to the IDE as IntelliJ, the plug-in actually works with any of the IDEs produced by JetBrains.
The next step is to open Settings and go to the Plugins tab. Then look for “haskell” and install IntelliJ-Haskell.
After restarting the IDE, you can open any Stack-based project (like
my-first-haskell-project created before). However, the first time you need to open it a bit differently than usual:
- Go to File, then New, then Project from Existing Sources.
- Choose the root folder of your project.
- In the next window, choose Haskell Stack as the model.
- Finally, point to the
stackexecutable when asked for the Project SDK. The plug-in already chooses the default installation folder based on your operating system, you just need to choose the
Note that the very first time you open a Haskell project, a lot of dependencies must be downloaded and installed. So be patient if you do not get all IDE features instantly.
Ready to code
At this point, you should have a working environment for your Haskell projects. Time to start hacking!