Skip to content

Instantly share code, notes, and snippets.

@yantonov
Last active June 11, 2020 09:20
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save yantonov/10083524 to your computer and use it in GitHub Desktop.
Save yantonov/10083524 to your computer and use it in GitHub Desktop.
How to install latest GHC from source + latest stack + cabal + cabal-install on ubuntu

How to install GHC from source + latest stack + cabal + cabal-install on ubuntu

for your convinience this instuction is available as:
gist
git repo

preferred way install stack than install ghc

stack (package manager and build tool, preferrered way to manage dependencies)

# settings
DOWNLOADS_DIR=$HOME/Downloads

STACK_ARCHITECTURE="x86_64"  
STACK_PLATFORM="linux"  
STACK_DIST_URL="https://www.stackage.org/stack/$STACK_PLATFORM-$STACK_ARCHITECTURE"
STACK_INSTALL_DIR="$HOME/Development/bin"

cd $DOWNLOADS_DIR

curl -L -O $STACK_DIST_URL  

# in case if error like this: 
#curl: (77) error setting certificate verify locations: CAfile: 
# /etc/pki/tls/certs/ca-bundle.crt CApath: 
# ...
# create ~/.curlrc file
# and put this lines to it
# capath=/etc/ssl/certs/
# cacert=/etc/ssl/certs/ca-certificates.crt

STACK_DIST_FILENAME=`ls -1 | grep 'stack-.*\.tar\.gz'`
STACK_VERSION=`echo $STACK_DIST_FILENAME | sed -E 's/stack-([.0-9]+)-.*/\1/'`
STACK_TARGET_DIR="stack-$STACK_VERSION"
echo "stack $STACK_VERSION will be installed"  

tar xvf $STACK_DIST_FILENAME  
STACK_DIST_UNZIPPED_DIR=`ls -d -1 stack-*/`

# move to home development dir
rm -rf $STACK_INSTALL_DIR/$STACK_TARGET_DIR  
mkdir -p $STACK_INSTALL_DIR
mv $STACK_DIST_UNZIPPED_DIR $STACK_INSTALL_DIR/$STACK_TARGET_DIR

# sym link
rm -rvi stack  
ln -s `pwd`/$STACK_TARGET_DIR stack  

# add to PATH environment  
STACK_HOME=$HOME/Development/bin/stack  
PATH=$STACK_HOME:$PATH

# clean up
cd $DOWNLOADS_DIR
rm -rf stack-$STACK_VERSION*

install ghc using stack

# install ghc
stack setup
# run repl
stack ghci

OLD way (manual installation of ghc)

### ubuntu prerequisites

# Multiprecision arithmetic library developers tools, zlib  
sudo apt-get install libgmp-dev zlib1g-dev -y  
sudo -K

ghc

DOWNLOADS_DIR="$HOME/Downloads"

GHC_VERSION=`curl https://downloads.haskell.org/~ghc/ | grep -E '([.0-9]+)/' | sed -E 's/.*>([.0-9]+).*/\1/' | uniq | sort -r | head -n 1`
ARCHITECTURE="x86_64"  
# for 32 bit ARCHITECTURE="i386"  
PLATFORM="deb8-linux"  
GHC_DIST_FILENAME="ghc-$GHC_VERSION-$ARCHITECTURE-$PLATFORM.tar.xz"

### ghc installation

echo "GHC $GHC_VERSION will be installed"  

# get distr  
cd $DOWNLOADS_DIR
GHC_DIST_URL="https://www.haskell.org/ghc/dist/$GHC_VERSION/$GHC_DIST_FILENAME"
curl -L -O $GHC_DIST_URL  
tar xvfJ $GHC_DIST_FILENAME  
cd ghc-$GHC_VERSION  

# install to  
mkdir $HOME/Development/bin/ghc-$GHC_VERSION  
# or choose another path

./configure --prefix=$HOME/Development/bin/ghc-$GHC_VERSION  

make install

# symbol links  
cd $HOME/Development/bin
rm -f ghc
ln -s `pwd`/ghc-$GHC_VERSION ghc  

# add $HOME/Development/bin/ghc to $PATH  
# add this line to ~/.profile  
export GHC_HOME=$HOME/Development/bin/ghc  
export PATH=$GHC_HOME/bin:${PATH}

# to use updated path without log off
source ~/.profile

# remove temporary files  
cd $DOWNLOADS_DIR  
rm -rfv ghc-$GHC_VERSION*

cabal (package manager, old way to manage dependencies)

# remove old  
rm -rfv $HOME/.cabal
rm -rfv $HOME/.ghc

cabal library

CABAL_VERSION=`curl https://www.haskell.org/cabal/release/cabal-latest/ | grep -E 'Cabal-([.0-9]+\.[0-9]).*' | sed -E 's/.*>Cabal-([.0-9]+\.[0-9]).*/\1/' | head -n 1`      
CABAL_DIST_FILENAME="Cabal-$CABAL_VERSION.tar.gz"

echo "Cabal $CABAL_VERSION will be installed..."  

# clone dist  
cd $DOWNLOADS_DIR  
curl -O "https://www.haskell.org/cabal/release/cabal-$CABAL_VERSION/$CABAL_DIST_FILENAME"  

# extract   
tar xzvf $CABAL_DIST_FILENAME  
cd Cabal-$CABAL_VERSION  

# build
ghc --make Setup.hs
./Setup configure --user --enable-library-profiling
./Setup build
./Setup install

# Remove temporary files
cd $DOWNLOADS_DIR
rm -rfv Cabal-$CABAL_VERSION*

cabal-install

CABAL_INSTALL_VERSION=`curl https://www.haskell.org/cabal/release/cabal-install-latest/ | grep -E 'cabal-install-([.0-9]+\.[0-9]).*' | sed -E 's/.*>cabal-install-([.0-9]+\.[0-9]).*/\1/' | head -n 1`      
CABAL_INSTALL_DIST_FILENAME="cabal-install-$CABAL_INSTALL_VERSION.tar.gz"  

echo "cabal install $CABAL_INSTALL_VERSION will be installed..."  

# get distributive  
cd $DOWNLOADS_DIR  
curl -O "https://www.haskell.org/cabal/release/cabal-install-$CABAL_INSTALL_VERSION/$CABAL_INSTALL_DIST_FILENAME"  

# extract archive  
tar xzvf $CABAL_INSTALL_DIST_FILENAME  
cd cabal-install-$CABAL_INSTALL_VERSION  

# install  
./bootstrap.sh

# remove temporary files  
cd $DOWNLOADS_DIR  
rm -rfv cabal-install-$CABAL_INSTALL_VERSION*  

# add path to cabal to PATH environment
export CABAL_HOME=$HOME/.cabal
export PATH=$CABAL_HOME/bin:$PATH
@naphthalene
Copy link

Thanks ^_^
edit: one small note: your instructions in the middle portion for the cabal-library are for version 1.20.0.2, not sure if intentional? I've forked and updated it with 1.20.0.3.

@yantonov
Copy link
Author

Thanks, fixed.

@selivanovm
Copy link

Good stuff, thanks! The only thing - I needed to install zlib1g-dev to build cabal-install.

@unthingable
Copy link

Forgot the dot: CABAL_HOME=$HOME/.cabal

@yantonov
Copy link
Author

@unthingable fixed, thanks!

@yantonov
Copy link
Author

yantonov commented Feb 8, 2015

@selivanovm thanks, fixed

@jprider63
Copy link

@yantonov
Copy link
Author

@jprider63 added, thanks

@Tarrasch
Copy link

@palmerba
Copy link

Hey I'm having a problem with the ./bootstrap.sh it keeps telling me that it can't find haddock. Can anyone help me?

@yantonov
Copy link
Author

@palmerba Check:
export GHC_HOME=$HOME/Development/bin/ghc
export PATH=$GHC_HOME/bin:${PATH}
I mean check than ghc and haddock are accessible using $PATH
At least ghc --version prints current version 7.10

@palmerba
Copy link

So I got it to find haddock but now when I run it I get the error
src/Data/Binary.hs:1:1:
Could not find module 'Prelude'
Perhaps you haven't installed the "dyn" library for package 'base'?

@palmerba
Copy link

Nevermind figured it out

@oguya
Copy link

oguya commented Aug 13, 2015

  • I keep getting the following error yet I've already installed ghc.

    joguya@localhost Cabal-1.22.4.0 $ ./Setup configure --user --prefix=/home/joguya/apps/cabal/1.22.4.0/
    Configuring Cabal-1.22.4.0...
    Setup: The program 'ghc' version >=6.4 is required but it could not be found.
    
  • installed ghc version

    joguya@localhost Cabal-1.22.4.0 $ ghc --version
    The Glorious Glasgow Haskell Compilation System, version 7.10.2
    
  • path to ghc

    joguya@localhost Cabal-1.22.4.0 $ which ghc
    ~/apps/ghc/7.10.2/bin/ghc
    joguya@localhost Cabal-1.22.4.0 $ echo $GHC_HOME 
    /home/joguya/apps/ghc/7.10.2/bin/ghc
    

@howaboutudance
Copy link

on Debian I had to install the ghc-dynamic package via apt-get but otherwise smooth sailing. Otherwise running bootstrap.sh will give a error not finding Prelude. Thanks!

@sportanova
Copy link

Is there any advantage to not just using stack and creating executables directly from it?

@yantonov
Copy link
Author

@sportanova advantange...hmmm, no as i can judge, building ghc from source its just another way (thats why these instruction preserved)
of course you can install required version of ghc using stack setup

@sportanova
Copy link

@yantonov awesome - thanks!

@timo-a
Copy link

timo-a commented Feb 9, 2017

@yantonov
$STACK_INSTALL_DIR/$STACK_TARGET_DIR is never created.

can you add mkdir -p $STACK_INSTALL_DIR/$STACK_TARGET_DIR ?

@yantonov
Copy link
Author

@timo-a thanks, fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment