Haskell and Visual Studio Code Setup

In this post we will walk you through a basic Haskell setup utilizing Visual Studio Code (Code Editor / IDE). We will need to install Visual Studio Code, GHC (the Glasgow Haskell Compiler), Stack(Haskell build tool), Cabal (Haskell package manager / build tool), Haskero (Visual Studio plugin that enables Haskell support), and Intero (powers the IDE like features in Haskero).

Install Visual Studio Code

Let’s start off by installing Visual Studio Code. Head to https://code.visualstudio.com/ and download the correct version for your operating system and install it.

Install Haskell Platform

Next let’s go ahead and go to https://www.haskell.org/platform/ and download the latest version of the Haskell Platform for your operating system and install it (in some cases you can simply install it with your OS’s package manager). The Haskell Platform includes GHC, Cabal, and Stack (all of which we need).

Load up a terminal window (or command-prompt in windows) and try the following commands (to output the version of each) to make sure they all installed successfully.

Test for GHC’s version: ghc --version

Test for Cabal’s version: cabal --version

Test for Stack’s version: stack --version

If any of them did not install (ie. the command is unrecognized) then you’ll need to go to their respective page (GHC, Cabal, Stack) and follow the instructions.

Install Haskero in Visual Studio Code

Load up Visual Studio Code. Click on the Extensions Icon of the far left (it looks like a square and is at the bottom of the icons list). Search for the Haskero extension. Install Haskero by clicking on the install button. Exit out of Visual Studio Code after installing Haskero.

Creating your first Haskell Project using Stack

Open up a terminal window and navigate to the directory you wish to create your haskell project in. Type the following command:

stack new MyFirstHaskellProject new-template

This creates a stack project named MyFirstHaskellProject using the new-template template. This creates a directory named MyFirstHaskellProject in the working directory which contains the project. MyFirstHaskellProject can be changed to whatever name desired. Different templates can be used other then the new-template template when started a new project. The templates available can be listed out by typing stack templates in the terminal.

Adding Intero to your Project

NOTE: you may need to upgrade your stack version & try again if the following steps fail.

Now that we’ve made a project go ahead and change directories to the MyFirstHaskellProject directory created by stack in the terminal. First run stack setup to setup stack. Next run stack build to build the project. This will build the project. Now run stack build intero to setup Intero for the project. This will allow Haskero to work correctly. Finally run code . to load up the project in Visual Studio Code. Haskero should now be working correctly. If Haskero is having issues finding stack then you’ll need to make sure that stack is on the system path (or mess with the Haskero configuration so that it has the correct location to run stack from). You can use the command stack build --exec MyFirstHaskellProject to compile and then run the built executable.

Looking around the newly created project

In the project you should see a few files & directories have been created for you. MyFirstHaskellProject.cabal & stack.yaml are filled with settings used when building the project. app/Main.hs is the Haskell file containing the entry point of the application. The src & test directories are to contain source code and test code respectively.

Future projects

Here’s a list of what you need to do in the future when starting a new project.

  1. Run stack new MyFirstHaskellProject new-template to initialize a new project.
  2. Change directories in terminal to the new project.
  3. Run stack build to do an initial build of the project.
  4. Run stack build intero to add Intero to the project.
  5. Run code . to start Visual Studio Code with the project loaded up.