title: Deploying Yesod to Heroku, can't build statically
body: <p>I'm very new to Yesod and I'm having trouble building Yesod statically
  so I can deploy to Heroku.</p>
  
  <p>I have changed the default .cabal file to reflect static compilation</p>
  
  <pre><code>if flag(production)
     cpp-options:   -DPRODUCTION
     ghc-options:   -Wall -threaded -O2 -static -optl-static
  else
     ghc-options:   -Wall -threaded -O0
  </code></pre>
  
  <p>And it no longer builds.  I get a whole bunch of warnings and then a
  slew of undefined references like this:</p>
  
  <pre><code>Linking dist/build/personal-website/personal-website ...
  /usr/lib/ghc-7.0.3/libHSrts_thr.a(Linker.thr_o): In function
  `internal_dlopen':
  Linker.c:(.text+0x407): warning: Using 'dlopen' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/ghc-7.0.3/unix-2.4.2.0/libHSunix-2.4.2.0.a(HsUnix.o): In
  function `__hsunix_getpwent':
  HsUnix.c:(.text+0xa1): warning: Using 'getpwent' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/ghc-7.0.3/unix-2.4.2.0/libHSunix-2.4.2.0.a(HsUnix.o): In
  function `__hsunix_getpwnam_r':
  HsUnix.c:(.text+0xb1): warning: Using 'getpwnam_r' in statically
  linked applications requires at runtime the shared libraries from the
  glibc version used for linking
  /usr/lib/libpq.a(thread.o): In function `pqGetpwuid':
  (.text+0x15): warning: Using 'getpwuid_r' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/libpq.a(ip.o): In function `pg_getaddrinfo_all':
  (.text+0x31): warning: Using 'getaddrinfo' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/ghc-7.0.3/site-local/network-2.3.0.2/
  libHSnetwork-2.3.0.2.a(BSD__63.o): In function `sD3z_info':
  (.text+0xe4): warning: Using 'gethostbyname' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/ghc-7.0.3/site-local/network-2.3.0.2/
  libHSnetwork-2.3.0.2.a(BSD__164.o): In function `sFKc_info':
  (.text+0x12d): warning: Using 'getprotobyname' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/ghc-7.0.3/site-local/network-2.3.0.2/
  libHSnetwork-2.3.0.2.a(BSD__155.o): In function `sFDs_info':
  (.text+0x4c): warning: Using 'getservbyname' in statically linked
  applications requires at runtime the shared libraries from the glibc
  version used for linking
  /usr/lib/libpq.a(fe-misc.o): In function `pqSocketCheck':
  (.text+0xa2d): undefined reference to `SSL_pending'
  /usr/lib/libpq.a(fe-secure.o): In function `SSLerrmessage':
  (.text+0x31): undefined reference to `ERR_get_error'
  /usr/lib/libpq.a(fe-secure.o): In function `SSLerrmessage':
  (.text+0x41): undefined reference to `ERR_reason_error_string'
  /usr/lib/libpq.a(fe-secure.o): In function `initialize_SSL':
  (.text+0x2f8): undefined reference to `SSL_check_private_key'
  /usr/lib/libpq.a(fe-secure.o): In function `initialize_SSL':
  (.text+0x3c0): undefined reference to `SSL_CTX_load_verify_locations'
  (... snip ...)
  </code></pre>
  
  <p>If I just compile with just <code>-static</code> and without <code>-optl-static</code>
  everything builds fine but the application crashes when it tries to
  start on Heroku.</p>
  
  <pre><code>2011-12-28T01:20:51+00:00 heroku[web.1]: Starting process with command
  `./dist/build/personal-website/personal-website -p 41083`
  2011-12-28T01:20:51+00:00 app[web.1]: ./dist/build/personal-website/
  personal-website: error while loading shared libraries: libgmp.so.10:
  cannot open shared object file: No such file or directory
  2011-12-28T01:20:52+00:00 heroku[web.1]: State changed from starting
  to crashed
  </code></pre>
  
  <p>I tried adding libgmp.so.10 to the LD_LIBRARY_PATH as suggested in <a href="http://translate.google.com/translate?hl=en&amp;sl=ja&amp;tl=en&amp;u=http://d.hatena.ne.jp/thimura/20111117/1321480728" rel="noreferrer">here</a>
  and then got the following error:</p>
  
  <pre><code>2011-12-28T01:31:23+00:00 app[web.1]: ./dist/build/personal-website/
  personal-website: /lib/libc.so.6: version `GLIBC_2.14' not found
  (required by ./dist/build/personal-website/personal-website)
  2011-12-28T01:31:23+00:00 app[web.1]: ./dist/build/personal-website/
  personal-website: /lib/libc.so.6: version `GLIBC_2.14' not found
  (required by /app/dist/build/personal-website/libgmp.so.10)
  2011-12-28T01:31:25+00:00 heroku[web.1]: State changed from starting
  to crashed
  2011-12-28T01:31:25+00:00 heroku[web.1]: Process exited
  </code></pre>
  
  <p>It seems that the version of libc that I'm compiling against is
  different.  I tried also adding libc to the batch of libraries the
  same way I did for libgmp but this results in a segmentation fault
  when the application starts on the Heroku side.</p>
  
  <p>Everything works fine on my PC.  I'm running 64bit archlinux with ghc
  7.0.3.  <a href="http://www.yesodweb.com/blog/2011/07/haskell-on-heroku" rel="noreferrer">The blog post on the official Yesod blog</a> looked pretty easy
  but I'm stumped at this point.  Anyone have any ideas?  If there's a way to get this thing working <em>without</em> building statically I'm open to that too.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Per <code>Employed Russians</code> answer I did the following to fix this.</p>
  
  <p>First created a new directory <code>lib</code> under the project directory and copied the missing shared libraries into it.  You can get this information by running <code>ldd path/to/executable</code> and <code>heroku run ldd path/to/executable</code> and comparing the output.</p>
  
  <p>I then did <code>heroku config:add LD_LIBRARY_PATH=./lib</code> so when the application is started the dynamic linker will look for libraries in the new lib directory.</p>
  
  <p>Finally I created an ubuntu 11.10 virtual machine and built and deployed to Heroku from there, this has an old enough glibc that it works on the Heroku host.</p>
  
  <p>Edit:
  I've since written a tutorial on the <a href="https://github.com/yesodweb/yesod/wiki/Deploying-Yesod-Apps-to-Heroku" rel="noreferrer">Yesod wiki</a></p>