‘sudo gem install …’ behind a proxy

Written by Jacob Stanley on October 27, 2010

This post applies to Ubuntu Server 10.10 (Maverick Meerkat)

I had some trouble installing gems (as root) behind our corporate proxy server today.

$ sudo gem install rake
ERROR:  http://rubygems.org/ does not appear to be a repository
ERROR:  Could not find a valid gem 'rake' (>= 0) in any repository

This is despite the fact that my http_proxy environment variable had been set in my ~/.bashrc file.

# ~/.bashrc: executed by bash(1) for non-login shells.

# HTTP Proxy
export http_proxy=http://username:password@192.168.0.1:80/

# ...

It turns out that you need to do some tweaking if you want sudo to carry your proxy environment across from the current user. To do this, run sudo visudo and add the env_keep line below:

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults env_reset
Defaults env_keep += "http_proxy https_proxy ftp_proxy"

Once you’re done editing, press Ctrl+X to exit, then Y to save your changes and make sure you save them to /etc/sudoers and not /etc/sudoers.tmp like it suggests. Press Y when it asks if you want to overwrite.

Now everything should be sweet. You can check that the environment is propagating using env.

$ sudo env | grep http_proxy
http_proxy=http://username:password@192.168.0.1:80/

And finally…

$ sudo gem install rake
Successfully installed rake-0.8.7
1 gem installed
Installing ri documentation for rake-0.8.7...
Installing RDoc documentation for rake-0.8.7...
Comment on this post…

Installing Gtk2Hs 0.11 on Windows

Written by Jacob Stanley on October 20, 2010

This is a quick to note to myself about what I had to do to install Gtk2Hs 0.11 on Windows, but others might find it useful as well.

Installing GTK+

Download the GTK+ all-in-one bundle from here. I used 2.16 because I had some problems with libpng [1] and zlib [2] when I tried 2.22.

Extract it somewhere without spaces in the name, and add the \bin directory to your PATH. I extracted the bundle to c:\gtk-2.16 so I had to add c:\gtk-2.16\bin to my PATH.

You can check you’ve done this right by running pkg-config from the command line. You should get back something similar to this:

> pkg-config --cflags gtk+-2.0
-mms-bitfields -Ic:/gtk-2.16/include/gtk-2.0 -Ic:/gtk-2.16/lib...

Installing Gtk2Hs

Add the Haskell Platform’s \mingw\bin directory to your PATH. This was C:\Program Files\Haskell Platform\2010.2.0.0\mingw\bin in my case. I found this was necessary because the Gtk2Hs install requires cpp.exe, the C preprocessor.

Once you have all the prerequisites set up, installation is simply a cabal install away.

> cabal update
> cabal install gtk2hs-buildtools
> cabal install gtk

Testing it out

You can try out your newly installed Gtk2Hs with the Hello World program from the documentation.

import Graphics.UI.Gtk

main :: IO ()
main = do
initGUI
window <- windowNew
button <- buttonNew
set window [ containerBorderWidth := 10,
containerChild := button ]
set button [ buttonLabel := "Hello World" ]
onClicked button (putStrLn "Hello World")
onDestroy window mainQuit
widgetShowAll window
mainGUI

I was able to run the code above using runghc without any issues.

> runghc Hello.hs

This gives us a window which is using the vanilla GTK+ theme. This looks a bit out of place on Windows and it’s easy to fix. Just add a gtkrc to your \etc\gtk-2.0 directory (for me c:\gtk-2.16\etc\gtk-2.0\gtkrc) with the following contents:

gtk-theme-name = "MS-Windows"

After adding a gtkrc, running our demo yields a much nicer looking window.

Various problems I encountered

1. If you’re trying to install using the GTK+ 2.22 bundle and you encounter this error:

setup.exe: gtk-0.11.2: library-dirs: c:/devel/dist/win32/libpng-1.4.3-1/lib
doesn't exist or isn't a directory

It can easily be fixed by creating an empty directory in the location that setup.exe is searching for: c:\devel\dist\win32\libpng-1.4.3-1\lib

2. Unfortunately, solving the libpng problem didn’t help me. I found that I was able to build and install the gtk package using cabal. The problem was that any programs which I built failed with an error about deflateSetHeader not being exported from zlib1.dll when I tried to run them.

Comment on this post…

Installing Heist (and Hexpat) on Windows

Written by Jacob Stanley on October 7, 2010

Edit: I’ve been in touch with Stephen Blackheath, the maintainer of Hexpat, and we’ve worked out a simple solution to this problem that looks promising. Hopefully by the time you read this Hexpat will install seamlessly via cabal, without the need for these instructions. I’ll leave the instructions below because it could be a week or two before all of this bubbles up to Heist.

So you’ve heard about Snap, that awesome new Haskell web framework, and you want to give it a try. You jump in to your Windows command prompt and cabal install snap-core and snap-server without any hassles, but when you try to install Heist you get this:

> cabal install heist
Resolving dependencies...
Configuring hexpat-0.18.3...
cabal: Missing dependency on a foreign library:
* Missing C library: expat
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it is.
cabal: Error: some packages failed to install:
heist-0.2.3 depends on hexpat-0.18.3 which failed to install.
hexpat-0.18.3 failed during the configure step. The exception was:
ExitFailure 1

Hmm, Heist depends on Hexpat, which has a dependency on a C library, Expat. Here you have two options, you can follow the instructions from the Hexpat documentation if you don’t mind hauling around a libexpat.dll with your application, or you can follow the instructions below for static linking.

First download this zip file. It contains the Expat dependencies you’ll need to install Hexpat, and a batch file which will do all the right stuff for you.

bin\libexpat.dll
include\expat.h
include\expat_external.h
lib\libexpat.a
install-heist.bat

Extract it somewhere that you don’t mind keeping around, GHC will need these files every time it builds an executable that uses Hexpat/Heist. Now you can run install-heist.bat and everything should go smoothly this time:

> install-hexpat.bat
cabal install heist --extra-include-dirs=C:\hexpat\include --extra-lib-dirs=C:\hexpat\lib
Resolving dependencies...
Configuring hexpat-0.18.3...
Preprocessing library hexpat-0.18.3...
Building hexpat-0.18.3...

etc...

If you only need to run your Hexpat/Heist program from GHCi (or via runghc) then you’ll need to put bin\libexpat.dll in your PATH or in the current directory. If you don’t need GHCi then you can just delete the bin folder, it’s not used when building via GHC. When building using GHC Expat is static linked to your app, so there’s no need for extra dlls.

If you want to move the directory that your Expat dependencies are in, the easiest thing to do is to unregister heist and hexpat and follow the instructions again.

> ghc-pkg unregister heist
> ghc-pkg unregister hexpat
Comment on this post…

More…