cabal VS. stack

cabal and stack are two tools one can use to install Glasgow Haskell Compiler (GHC), i.e. the binaries ghc and ghci, and Haskell software packages. stack installs Haskell programs in ~/.stack and cabal installs in ~/.cabal. Using both cabal and stack, or either one of them is possible, but it seems the current general wisdom is that stack is more user friendly especially for beginners. So I chose to uninstall cabal and delete ~/.cabal, and use stack only.

Install Haskell compiler and softwares using stack

stack is built on top of cabal (which in turn relies on ghc-pkg), the package management system for Haskell softwares, but is more user friendly in some ways.

  • Uninstall the ghc installed using the system's package management system

    • macOS:

          brew uninstall --force cabal-install
          brew uninstall --force ghc  
      
      
    • Linux:

          sudo apt-get remove ghc
          sudo apt-get remove cabal-install 
      
      
  • Install stack

    • macOS:

          brew install haskell-stack
      
      
    • Linux: Follow the instruction about installation in the official documentation. Don't just do sudo apt-get install haskell-stack.

  • Install ghc

      stack setup
    
    
  • If you want to use binaries installed via stack regularly, put them on path. Run stack path and append the paths of form ~/.stack/.../bin in the output to $PATH in ~/.bashrc.

  • Install packages using stack:

      stack install ghc-mod
      stack install cabal-install
      stack install hlint
      stack install pandoc
      stack install purescript
      stack install wreq
      stack install lens
      stack install ihaskell        
      stack install projectile
      stack install use-package
      stack install which-key
    
      stack install apply-refact
      stack install codex
      stack install hasktags        
    
    
    • ihaskell: IHaskell, Haskell kernel for Jupyter notebooks.

Install Haskell compiler and softwares using cabal

  • Install ghc and cabal

      brew install cabal-install ghc
    
    
  • Install packages via cabal

      cabal update
      cabal install happy # 2016-3-5: prerequisite for ghc-mod
      cabal install ghc-mod hlint haskell-docs    
    
    

Verifying the stack and Haskell installation

cd $HOME
# Assume 'stack setup' has been run.
stack init
stack build --dependencies-only --test

Configure Emacs for Haskell development

Install the following Emacs packages, ideally using the GNU Emacs Lisp Package Archive (see also EmacsWiki: ELPA):

  • Emacs packages specifically relevant for Haskell:
    • haskell-mode
    • haskell-snippets
    • flycheck-hlint
    • intero
  • Other Emacs packages generally helpful.

Configure a Haskell project using stack

Create a project

cd path/to/workspace/
stack new haskell-stack-test-project
cd haskell-stack-test-project
stack setup
stack build --dependencies-only --test

This creates a new project using template ~/.stack/templates/new-template.hsfiles. The project directory structure:

$ tree
.
├── LICENSE
├── Setup.hs
├── app
│   └── Main.hs
├── haskell-stack-test-project.cabal
├── src
│   └── Lib.hs
├── stack.yaml
└── test
    └── Spec.hs

Remarks:

  • app/ contains the source file(s) for more application-specific logic, including a app/Main.hs.
  • src/ contains the source file(s) for more shareable and general-purpose code.

Build

$ cd path/to/haskell-stack-test-project
$ stack build
haskell-stack-test-project-0.1.0.0: configure
Configuring haskell-stack-test-project-0.1.0.0...
haskell-stack-test-project-0.1.0.0: build
Preprocessing library haskell-stack-test-project-0.1.0.0...
[1 of 1] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.22.5.0/build/Lib.o )
In-place registering haskell-stack-test-project-0.1.0.0...
Preprocessing executable 'haskell-stack-test-project-exe' for
haskell-stack-test-project-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.22.5.0/build/haskell-stack-test-project-exe/haskell-stack-test-project-exe-tmp/Main.o )
Linking .stack-work/dist/x86_64-osx/Cabal-1.22.5.0/build/haskell-stack-test-project-exe/haskell-stack-test-project-exe ...
haskell-stack-test-project-0.1.0.0: copy/register
Installing library in
/Users/meng/Dropbox/WorkSpace-Dropbox/Computing/repo-meng-lib/Haskell/haskell-stack-test-project/.stack-work/install/x86_64-osx/lts-5.5/7.10.3/lib/x86_64-osx-ghc-7.10.3/haskell-stack-test-project-0.1.0.0-BCjrGi4SAkj4MdkT4tD42y
Installing executable(s) in
/Users/meng/Dropbox/WorkSpace-Dropbox/Computing/repo-meng-lib/Haskell/haskell-stack-test-project/.stack-work/install/x86_64-osx/lts-5.5/7.10.3/bin
Registering haskell-stack-test-project-0.1.0.0...

The path to the built executable is contained in the line starting with Linking: Linking .stack-work/dist/x86_64-osx/Cabal-1.22.5.0/build/haskell-stack-test-project-exe/haskell-stack-test-project-exe ....

$ .stack-work/dist/x86_64-osx/Cabal-1.22.5.0/build/haskell-stack-test-project-exe/haskell-stack-test-project-exe
someFunc

Configure a Haskell project using IntelliJ IDEA

  • Install JetBrains' Haskell plug-in.

  • Configure Languages & Frameworks | Haskell:

[[!img Error: Can't use string ("everything") as an ARRAY ref while "strict refs" in use at /opt/pkg/lib/perl5/vendor_perl/5.38.0/IkiWiki/Plugin/img.pm line 39.]]

If using binaries installed via cabal:

[[!img Error: Can't use string ("everything") as an ARRAY ref while "strict refs" in use at /opt/pkg/lib/perl5/vendor_perl/5.38.0/IkiWiki/Plugin/img.pm line 39.]]

  • Create a Haskell project and its first component.

Use menu File | New | Project and select 'Haskell', click 'next', and choose name for the project and the name for the first module and its sub-directory:

[[!img Error: Can't use string ("everything") as an ARRAY ref while "strict refs" in use at /opt/pkg/lib/perl5/vendor_perl/5.38.0/IkiWiki/Plugin/img.pm line 39.]]

  • Set GHC (ghc) as the platform SDK: use menu File | Project Structure, and select '+' and navigate to the installation path of the desired version of ghc, e.g. /Users/meng/.stack/programs/x86_64-osx/ghc-7.10.3 which is installed via stack or /usr/local/Cellar/ghc/7.10.3, which is installed via brew.

[[!img Error: Can't use string ("everything") as an ARRAY ref while "strict refs" in use at /opt/pkg/lib/perl5/vendor_perl/5.38.0/IkiWiki/Plugin/img.pm line 39.]]

  • Optionally, create additional modules in the project: use menu File | New | Module, select 'Haskell', and choose name for the module and directory.

[[!img Error: Can't use string ("everything") as an ARRAY ref while "strict refs" in use at /opt/pkg/lib/perl5/vendor_perl/5.38.0/IkiWiki/Plugin/img.pm line 39.]]

References

blog comments powered by Disqus