Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Haskell, Stack and Intellij IDEA IDE setup tutorial how to get started

Haskell, Stack and Intellij IDEA IDE setup tutorial how to get started

Upon completion you will have a sane, productive Haskell environment adhering to best practices.

Basics

  • Haskell is a programming language.
  • Stack is tool for Haskell projects. (similar tools for other languages include Maven, Gradle, npm, RubyGems etc)
  • Intellij IDEA IDE is a popular IDE.

Install required libraries

sudo apt-get install libtinfo-dev libghc-zlib-dev libghc-zlib-bindings-dev

Download and extract Stack

Don't install Haskell, Stack, Cabal or any other Haskell tool or library using your OS package manager or using Cabal.

Instead, download Stack from http://docs.haskellstack.org/en/stable/install_and_upgrade/#linux and extract it into ~/.local/bin (ie the complete path to stack should be ~/.local/bin/stack) and add ~/.local/bin to $PATH. (adding a folder to $PATH is beyond the scope of this tutorial, search and you will find)

Use Stack to create, build and run a project

Go to the folder where you want to create your Haskell project and run

stack new my-project
cd my-project
stack setup
stack build
stack exec my-project-exe

Stack installs Haskell (ghc) to ~/.stack/programs/x86_64-linux/ghc-7.10.3/bin/ghc, project-specific dependencies to appropriate locations inside the project, and builds and runs the project.

IDE

  • Make sure you're not inside a Haskell project folder and run
stack install hindent stylish-haskell

to install tools required by the IDE plugin for Haskell. (they will be installed to ~/.local/bin)

  • Download and Intellij IDEA IDE from http://www.jetbrains.com/idea and extract it to some appropriate folder.
  • (NOTE there are a number of different mutually exclusive Haskell plugins. If other Haskell plugins are already installed, uninstall them before installing the Intellij-Haskell plugin) Start IntelliJ up and install the plugin Intellij-Haskell. (The plugin will install its own copies of Intero and Hlint)
  • Restart IntelliJ.
  • Settings | Other | Haskell | add the respective paths to the tools (eg for hindent ~/.local/bin/hindent)
  • File | New | Project from Existing Sources | in the New Project wizard select Import project from external module and check Haskell Stack
  • In next page of wizard configure Project SDK by configuring Haskell Tool Stack with selecting path to stack binary, e.g. /usr/local/bin/stack
  • Finish wizard and project will be opened.

Wizard will try to automatically configure which folders are sources, test and which to exclude. Plugin will automatically build Intero and HLint to prevent incompatibility issues (If you use non LTS or Nightly resolver e.g. ghc-7.10.2, you may have to build them manually since there are some extra-deps should be added to stack.yaml). Those tools are built against Stackage release defined in project's stack.yaml. If you want to use later version of tool, you will have to build tool manually in project's folder by using stack build.

  • Check Project structure/Project settings/Modules which folders to exclude (like .stack-work and dist) and which folders are Source and Test. (normally src and test)

Plugin will automatically download library sources (since Stack version 1.2.1 also for test dependencies). They will be added as source libraries to module. This option gives you nice navigation features through libraries. Sources are downloaded to folder .ideaHaskellLib inside root of project. After changes to dependencies you can download them again by using Tools | Download Haskell Library Sources. This is what enables viewing docs while coding. It creates a folder ideaHaskellLib in which it runs stack unpack for every dependency output by stack list-dependencies. Remember to repeat this step on any change in dependencies.

  • Add the ideaHaskellLib folder to the .gitignore file for the project (it's not part of your projects source code).

In the background for each Haskell project two Stack repls are running. You can restart them by Tools/Restart Haskell Stack REPLs. When you make large changes to stack.yaml or Cabal file, you have to restart IntelliJ project.

Most or all of the usual features of Intellij like auto-completion, go to declaration ctrl+b etc should now work.

MISC

Development flow

Build and run the project

Terminal has to be used for this. Go to path to root of my-project and run

stack build && stack exec my-project-exe

Interactive interpreter

One of the advantages of Haskell is the interactive interpreter. Go to path to root of my-project and run

stack ghci

to fire up the interpreter. The Main module and other project modules should be automatically loaded. With the modules loaded, inside the interpreter, run

main

to interactively call the main function.

New functions can be defined and tried out interactively inside the interpreter. When editing source files, run

:r

inside the interpreter to reload them. (NOTE reloading edited source files into the interpreter does only that- rebuilding the project is still necessary to reflect source file edits in the built project)

Updating Haskell and Stack

Stack can install and manage multiple Haskell versions, and different versions of different libraries in different projects. Stack itself can be upgraded to the latest version simply by running

stack upgrade

This is why neither Haskell, Stack, Cabal nor any other Haskell tool or library should be installed and managed through the OS package manager or Cabal. (anything installed and managed through the OS package manager will be a single, global version, and the versions are often lagging severely)

Other IntelliJ plugins for Haskell

As previously seen, there are other IntelliJ plugins for Haskell that may or may not be better than, have more features etc than the Intellij-Haskell plugin, but I've been unable to get any of them running. Which brings us to...

What went in to this Gist

I tried several times to pick up Haskell, but always gave up, not due to anything to do with the language itself, but due to not being able to set up a sane, productive environment. After lots of time, energy and frustration it appears as if I've finally figured out how to do it. This Gist is for my own future reference, but I hope others will find it useful as well, and that others won't have to go through what I went through just in order to get started.

@rikvdkleij

This comment has been minimized.

Copy link

rikvdkleij commented Oct 25, 2016

FYI, I have released new version of IntelliJ Haskell plugin which mainly depends on Stack and Intero.

@shawnritchie

This comment has been minimized.

Copy link

shawnritchie commented Jan 12, 2017

missing hindent on the following installation instruction stack install hlint stylish-haskell ghc-mod

@andrewufrank

This comment has been minimized.

Copy link

andrewufrank commented Jan 29, 2017

Thank you very much for this clean description. I was giving up on Leksah (harder to install with each new version), trying Atom (which I did not see how to register a cabal build - and other minor annoyances), and eclipse where the Haskell plug-ins are out of date, finding your blog. It seems to work, albeit some of the GUI has changed in the current 2016.3.3 January 16 build.

More detailed instructions to load the Intellij-Haskell plug-in (and to avoid the older Haskell plugin) I found on github (https://github.com/rikvdkleij/intellij-haskell).

The path for stack in defining the SDK was offered (and in my case in /usr...) and needed only confirmation. however, i could not find Tools | Add Haskell package dependencies but found Analyze | Analyze Module dependencies which then ran for half an hour - a warning here for those that have many installed packages from hackage. so far, everything seems fine!

let me conclude with restating your conclusions: an IDE is of prime importance to learn a language; the lack of an easy to install and easy to use IDE, which works for beginners and professional software engineers alike, is certainly one of the points that makes it difficult for Haskell to gain acceptance in the commercial world. compare with the Python IDE, which is not perfect, but kept the attention of my 14 year old son and he learned the language - given the current state of Haskell IDEs, i did not dare to propose that he learns Haskell.

@ninjazoete

This comment has been minimized.

Copy link

ninjazoete commented Feb 23, 2017

I am not sure did I miss something? I get spammed by IntelliJ "Something went wrong while calling hlint. Error: ghc: unrecognised flag: -- did you mean one of: -F -v -j unrecognised flag: --json Usage: For basic information, try the `--help' option."

Edit: I only found under Editor that it uses hlint and I can only enable/disable it. Where do I customize it through IDE?
Edit2: Under Preferences - Other Settings - Haskell I only have stylish Haskell and hindent paths to configure. No ghc-mod

@jellyr

This comment has been minimized.

Copy link

jellyr commented Feb 27, 2017

to: ninjazoete

Are you on windows? If so , execute:
stack exec -- hlint --json xxxx.hs (change xxx which the hs file is exist)
If hlint is exist on your path, it should success! if not , you should install hlint.

If your Intellij still cannot find hlint, It's because you didn't run your Intellij IDE with (run as administrator) option.
So, right click Intellij, choose "run as admin", the ide should find the hlint!

@heat-wave

This comment has been minimized.

Copy link

heat-wave commented Mar 2, 2017

@jellyr

stack exec -- hlint --json xxxx.hs (change xxx which the hs file is exist)

I'm on Linux, running this line in a terminal yields the following:
"[]
hlint: unable to decommit memory: Invalid argument
hlint: unable to decommit memory: Invalid argument"
So hlint does exist (in ~/.local/bin), but IDEA is still spamming "Something went wrong while calling hlint." Any ideas?

@androidfred

This comment has been minimized.

Copy link
Owner Author

androidfred commented Mar 25, 2017

@rikvdkleij Can't thank you enough for the plugin and your continued work on it.

@shawnritchie Thanks. The current version of the plugin only requires running stack install hindent stylish-haskell, it takes care of the rest of its depenencies itself.

@andrewufrank Agreed, hopefully this plugin can fill those needs.

@ninjazoete The current version of the plugin only requires running stack install hindent stylish-haskell, it takes care of the rest of its depenencies itself.

@heat-wave Try hitting up @rikvdkleij on the plugin project page https://github.com/rikvdkleij/intellij-haskell

@akhanal

This comment has been minimized.

Copy link

akhanal commented Aug 29, 2017

I also had to do following.
Haskell plugin build in IntelliJ was giving error while building Hoogle database.

sudo apt-get install libghc-zlib-dev libghc-zlib-bindings-dev

@androidfred

This comment has been minimized.

Copy link
Owner Author

androidfred commented Dec 3, 2017

@akhanal Thanks for your comment, I've updated the Gist!

@micmor

This comment has been minimized.

Copy link

micmor commented Dec 11, 2017

If error while building hoogle database in IntelliJ still appears, try:

stack install hoogle
hoogle generate

and then inside IntelliJ : (Re)Build hoogle database

@VrUnRealEngine4

This comment has been minimized.

Copy link

VrUnRealEngine4 commented Jan 21, 2018

I cannot figure why I cannot run my Haskell with IntellJ... the editor work grea!!! but the debug and run option does noting and keep taking me to the configuration and I have no idea what I should fill in. Am I making the wrong assumption that is to be used more than a editor for Haskelle?

@Quantification

This comment has been minimized.

Copy link

Quantification commented Mar 28, 2018

Thank you for sharing the results of your Haskell IDE search. As a beginner, agree that this combination works out of the box. (Win 7 x64)

@ibalashov

This comment has been minimized.

Copy link

ibalashov commented Apr 19, 2018

Somehow debugging is not available, it is expected?

@androidfred

This comment has been minimized.

Copy link
Owner Author

androidfred commented Jun 7, 2018

@VrUnRealEngine4 @ibalashov Due to the nature of Haskell, "debugging" doesn't really work the same as with other languages, and it's not supported as part of this particular setup.

@viktor-ferenczi

This comment has been minimized.

Copy link

viktor-ferenczi commented Aug 10, 2018

@VrUnRealEngine4 The executable is defined in package.yaml, just create a Haskell Stack / Haskell Stack Runner configuration with default options, name it "Run" and you are ready to go:
image
Please note, that I changed the message in Lib.hs of the generated default project to "Hello World!", this is why that's printed.

@rikvdkleij

This comment has been minimized.

Copy link

rikvdkleij commented Sep 15, 2018

FYI, the latest betas are not published to stable channel of IntelliJ repo but to the alpha channel. See https://github.com/rikvdkleij/intellij-haskell#installing-the-plugin

@pwm

This comment has been minimized.

Copy link

pwm commented Nov 26, 2018

Just wanted to say thank you for this! So far it seems to be working great for someone (me) who's used to IntelliJ.

@pwm

This comment has been minimized.

Copy link

pwm commented Nov 28, 2018

@rikvdkleij is there a way to add support for https://github.com/lspitzner/brittany ? Also just wanted to thank you for this awesome plugin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.