47

I am trying to install Haskell on my Macbook two days and can not get the success.

What I have done:

$ brew install ghc

Fine. Now I am trying to install

$ cabal install text

and get next error messages:

Data/Text.hs:1074:4:
     error: invalid preprocessing directive
      #-}
       ^

I have googled and found this.

So:

  • brew install gcc48

    $ /usr/local/bin/gcc-4.8 --version  
    gcc-4.8 (GCC) 4.8.2
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ cat /usr/local/lib/ghc-7.6.3/settings
      [("GCC extra via C opts", " -fwrapv"),
       ("C compiler command", "/usr/local/bin/gcc-4.8"),
       ("C compiler flags", " -m64 -fno-stack-protector  -m64"),
       ...
    
  • brew install cabal-install

  • cabal install text

And I get the same messages.

How can I fix them ?

askewchan
  • 45,161
  • 17
  • 118
  • 134
ceth
  • 44,198
  • 62
  • 180
  • 289

7 Answers7

61

brew install ghc cabal-install

If you prefer homebrew.

Note: (Edited to add) Homebrew may have an older (or newer) version of ghc than the Haskell Platform download for OSX, and there might be other differences. You can check the release notes here for the version included in Haskell Platform, as well as other information of interest. brew info ghc cabal-install will give you up to date information about Homebrew's versions.

Note: The last time I did this, I just installed haskell-stack instead, as described in Filippo's answer.

derekv
  • 3,131
  • 3
  • 21
  • 30
  • `brew info` was helpful! – Bartek Banachewicz Sep 14 '15 at 22:29
  • 9
    Prepare to have your computer burning for 30-60 minutes onces you do this, this compiles like forever. Look at https://stackoverflow.com/a/29247096/99834 for doing it in two minutes by installing binaries. – sorin Aug 11 '17 at 10:56
49

To install the "Haskell Stack" binaries via homebrew:

brew install haskell-stack

EDIT: Below my original answer

If you want to install the Haskell Platform binaries via homebrew:

brew install cask
brew cask install haskell-platform

cabal install cabal-install
Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64
  • 3
    This is what [Haskell.org](https://www.haskell.org/platform/mac.html#osx-homebrewcask) says to do [instead of installing GHC with Homebrew](https://stackoverflow.com/questions/15239289/how-to-install-haskell-cabal-tool-for-haskell-7-6-1-on-mac-osx). – Merchako Oct 15 '17 at 17:17
  • As of 05/2020: `Error: Cask 'haskell-platform' is unavailable: No Cask with this name exists.` – Indigenuity May 15 '20 at 17:25
  • 2
    Well @Indigenuity times has passed, the command is now: `brew install haskell-stack` Thanks for the headsup, I have updated my answer – Filippo Vitale May 15 '20 at 21:13
  • this now returns "Error: `brew cask` is no longer a `brew` command. Use `brew --cask` instead." when running brew cask install haskell-platform – Chebi Aug 15 '22 at 23:11
15

Try using the Mac version of the Haskell Platform, I don't have a Mac to try it out but my hope is that cabal will work out-of-the-box in the Haskell Platform.

Bas Bossink
  • 9,388
  • 4
  • 41
  • 53
  • 2
    "The Haskell Platform is deprecated since 2022 and is no longer the recommended way of installing Haskell. For the latest recommended way to install Haskell please see the Downloads page." – saiyancoder Mar 26 '22 at 14:10
  • This should no longer be the accepted answer. – Bugs Bunny Mar 29 '22 at 01:52
14

The following information suggests there may be a mac installer available:

brew info haskell-platform

We no longer package haskell-platform. Consider installing ghc
and cabal-install instead:

brew install ghc cabal-install

A binary installer is available:
https://www.haskell.org/platform/mac.html
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
xmkevinchen
  • 1,506
  • 1
  • 14
  • 17
8

In 2022, the Haskell Platform is deprecated and the officially recommended way for most platforms, including Mac, is to install GHCup first. GHCup is basically a package manager for the Haskell toolchain (GHC, Stack, Cabal, HLS). It can install multiple versions of each of these into the home directory, and "set" (symlink) the ghc/... commands to a specific version.

To install GHCup, use the install script (source). This installs GHCup and GHC, asks some questions about setting up PATH etc., and prompts you to install the Apple developer command line tools if it notices them missing.

curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

If you're on a Mac M1 and want to run ghc 8.10.7 or older, you will also need to install a version of llvm between 9 and 12 (inclusive), and put it on the path so ghc can find it. Currently however, 9.2.4 is the default, which has a native code generator (NCG) that works on M1 out of the box. It may still contain bugs on M1 though (see here for details) so consider upgrading to 9.4.2 with ghcup install ghc 9.4.2 && ghcup set ghc 9.4.2. Currently, 9.4.2 lacks the breadth of package support enjoyed by 8.10.7, but it is has fewer known bugs.


If you find yourself in an install mess, you can start from a clean slate and reinstall both the Developer Tools and GHCup with:

sudo rm -rf /Library/Developer/CommandLineTools
ghcup nuke
rm -rf ~/.ghcup
rm -rf ~/.stack
rm -rf ~/.cabal
# For the cautious, a reboot here is advised before proceeding
xcode-select --install
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

It's also possible to install GHCup via Homebrew, though that requires to manually update $PATH and install/set the wanted tools.

brew install ghcup
# Now add $HOME/.ghcup/bin to $PATH
ghcup install ghc && ghcup set ghc
Heath Raftery
  • 3,643
  • 17
  • 34
konst
  • 336
  • 2
  • 4
  • This is the answer today, and will save hours. `ghcup` will also play nicely with editors/plugins/etc and the Haskell Language Server to ensure you have the right bits and the right versions. See `ghcup list` for insight into the important tool management it does for you. One gotcha though - ghcup's installs and ghcup itself install into your home directory, so maybe not quite what you'd expect. – Heath Raftery Sep 17 '22 at 14:28
  • To be fair on M1 this only got me as far as `ghci` and `hls`. To use `ghc` I still needed to run `brew install llvm@12` and then add `/opt/homebrew/opt/llvm@12/bin` to the `PATH`. And even then that only results in a successful *compile* - even the simplest of "Hello world" programs still hang forever when executed after compilation, and then `ghcup install` and even `gcc` compiled executables also hang. So consider this only a partial solution for now! – Heath Raftery Sep 20 '22 at 04:14
  • Ah ha! Finally got a full solution. tl;dr: ghc 8.10.7 (the recommended, and default installed version) is a dead end on a M1 Pro. But 9.4.2 works fine without having to resort to llvm shenanigans. I'd like to suggest an edit to your answer @konst to include this info. – Heath Raftery Sep 20 '22 at 09:19
  • Oh dear. The hanging was not entirely llvm related. Inexplicably, if I compile and run a Haskell program on a network drive, it hangs. And from **that point on** any `ghc`/`gcc` compiled executable hangs, local or otherwise, until reboot! Preposterous, but reporting here before I have a bloody clue what's going on, so my edit to this answer is given reference. – Heath Raftery Sep 20 '22 at 10:46
  • 1
    Finally made amends. Confident now that ghc 8.10.7 requires llvm on M1 and 9.2+ works without it. Since 9.2.4 is now the default, GHCup works out of the box. Retained the useful information in the answer and deleted the rest. – Heath Raftery Nov 04 '22 at 14:00
4

There is a new ghc-dot-app project that is an alternative to Homebrew formula and Haskell Platform official installer. Here is the project's web page: Haskell for Mac OS X. Here is a discussion thread about it on Haskell Cafe: "Self-contained GHC for Mac". Here is a quote from the author's posting:

How does it work?

In https://github.com/ghcformacosx/ghc-dot-app I have a Main.hs script which downloads/builds/etc. all of the prerequisites. Then it patches the shell scripts to use a path relative to themselves (after following symlinks) rather than hard-coding an installation location. It also patches the package.conf.d files to use ${pkgroot} relative paths.

Separately, there's an Xcode project that includes all of the metadata to make it look like a real Mac app bundle (icons, Info.plist) and a small Cocoa application that has a button to open the docs and provides instructions for adding it to the PATH in .bashrc. Due to the heuristics in Mac OS X's man, this will automatically make man pages work as well.

Alexey
  • 3,843
  • 6
  • 30
  • 44
1

Try installing Haskell using brew install haskell-platform.

If you are already on Mavericks you will have to install GCC (as Mavericks ships with Clang). Follow the instructions here: https://gist.github.com/cartazio/7131371.

perl
  • 73
  • 6
  • Yes, this is absolutely the magic incantation for `homebrew` users. – sanityinc Mar 19 '14 at 08:09
  • 5
    Unfortunately, `haskell-platform` is no longer supported via `homebrew`, at least as of Sept. 8, 2014. See https://github.com/Homebrew/homebrew/issues/31609 – nonrectangular Oct 03 '14 at 00:15
  • 5
    @nonrectangular Yes, `haskell-platform` has been removed from `homebrew`, but it is available from [homebrew cask](http://caskroom.io). Just installed using `brew cask install haskell-platform` – Pan Long Oct 30 '14 at 05:06
  • 5
    Might it have been removed for a reason? If so, perhaps it's not actually advisable to install the cask'd version. – mgadda Jun 05 '15 at 04:12
  • 5
    `brew install ghc cabal-install haskell-stack` – james-see Apr 17 '20 at 03:57