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).
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.
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.
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.
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.
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.
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.
Here’s a list of what you need to do in the future when starting a new project.
stack new MyFirstHaskellProject new-template
to initialize a new project.stack build
to do an initial build of the project.stack build intero
to add Intero to the project.code .
to start Visual Studio Code with the project loaded up.