OS-X and F# [Clone It, Build It, Install It, Hack It]

Ok.

Mission: Get F# running on OS-X and executing via repl.

There are a number of steps here, that if you don’t have everything covered things just won’t work. So let’s knock out the prerequisites first. This will include getting autoconf installed. There are some other tools that I’ve added below that are good to have installed too, so get autoconf, automake, and libtool installed.

[sourcecode language=”bash”]
export build=~/devtools # or wherever you’d like to build
mkdir -p $build

cd $build
curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-2.68.tar.gz
tar xzf autoconf-2.68.tar.gz
cd autoconf-2.68
./configure –prefix=/usr/local
make
sudo make install
export PATH=/usr/local/bin

cd $build
curl -OL http://ftpmirror.gnu.org/automake/automake-1.11.tar.gz
tar xzf automake-1.11.tar.gz
cd automake-1.11
./configure –prefix=/usr/local
make
sudo make install

cd $build
curl -OL http://ftpmirror.gnu.org/libtool/libtool-2.4.tar.gz
tar xzf libtool-2.4.tar.gz
cd libtool-2.4
./configure –prefix=/usr/local
make
sudo make install
[/sourcecode]

Now we’re ready to get some F# (64-bit flavors too) running on OS-X. This is very likely to take more than a few minutes because we’ve got two actual builds to do here.

First step is to get mono built and installed.

[sourcecode language=”bash”]
git clone https://github.com/mono/mono
cd mono
./autogen.sh –prefix=/mono64 –enable-nls=no
make
sudo make install
[/sourcecode]

Now clone fsharp, build and install it.

[sourcecode language=”bash”]
git clone https://github.com/fsharp/fsharp
cd fsharp
./autogen.sh –prefix=/mono64
make
sudo make install
[/sourcecode]

Finally get the binary executable paths added to the paths file.

[sourcecode language=”bash”]
vi /etc/paths
[/sourcecode]

Add the following paths to the paths file.

[sourcecode language=”bash”]
/mono64/bin/mono
/mono64/bin
[/sourcecode]

Once that’s done, run the tests to confirm everything has built right and is operable.

[sourcecode language=”bash”]
cd tests/fsharp/core
./run-all.sh
[/sourcecode]

Now… that should all be ok.

repl

What I wanted now is something to sling some code and execute it. The easiest way, is to use the ‘fsharpi’ repl. At the OS-X terminal launch the repl.
Enter this line…

[sourcecode language=”bash”]
let x = 5;;
[/sourcecode]

Then let y to a value…

[sourcecode language=”bash”]
let y = 20;;
[/sourcecode]

Then enter…

[sourcecode language=”bash”]
y + x;;
[/sourcecode]

…and you should see the math calculated. Then you can quit out of the repl with the following command.

[sourcecode language=”bash”]
#quit;;
[/sourcecode]

The overall repl should come out looking like this.

[sourcecode language=”bash”]
> let x = 5;;

val x : int = 5

> let y = 20;;

val y : int = 20

> y + x;;
val it : int = 25
> #quit;;

– Exit…
[/sourcecode]

Compilation – A Step Further

Just to show one more step. Let’s do a compile of a file with F# Code in it. Create a file called process.fs and enter the following code.

[sourcecode language=”fsharp”]
open System

[<EntryPoint>]
let main (argv :string[]) =
printfn "Hello World"
Console.ReadLine() |> ignore
0
[/sourcecode]

Now run the compiler ‘fsharpc’.

[sourcecode language=”bash”]
fsharpc process.fs
[/sourcecode]

Now before you freak out screaming “OMG WTF am I supposed to do with a process.exe file…!!!” just calm down and execute the following command.

[sourcecode language=”bash”]
mono process.exe
[/sourcecode]

There you’ll see the execution of “Hello World”. Welcome to F# on OS-X. More to come, Keep thrashing code!

Note: I’ve got a github repo for this and coming F# coding available at sharpgrounds.

4 thoughts on “OS-X and F# [Clone It, Build It, Install It, Hack It]

Comments are closed.