Friday, January 4, 2013

Short Guide: Install Python and Gnuplot in Os X

Background

There are two software tools that I always uses:
However to get this up and running on an Apple Mac with Os X, is not always straightforward. Here I give you a guide of how to set this up on a Mac Os X mountain lion (10.8.2), from a clean install.

The bads:

  • The python you got with your Mac Os X is just the main python package, usually, you would like to have some additional packages like numpy or scipy if you does scientific work, as I do.
  • For gnuplot, you might want to use the latest functions, or perhaps use animated gif's output then you need to compile gnuplot by your self and you can not use some of the old binaries that are floating around on the Internet. Compile under Os X is usually not so straight forward.
  • GCC, the c-compiler, is not included by default in Os X.

The goods:

  • Mac os X, has some kind of "unix" back-end, if I am making it short. This means for you an opportunity to use some of the tools from that world, like gnuplot for example. 
  • Python is a neat kind of language, and to this date, this is already included in Mac Os X. (python version 2.7.2)
  • For the python package's that you usually need, Chris Fonnesbeck, have made a very nice procedure.
  • If you know were to look, GCC, can easily be downloaded.
  • You have this guide, that you reading know, hopefully that can help you.
This guide is split into two parts: python part I and gnuplot part II. The time consumed for you to do the first part, I estimate to be less than 10 minutes. The second part, however,  takes ruffly one hour of time, and is depending on how much earlier experience you have with the terminal window of your mac. Of-course if you do not need both tools, you can do either part separately, thats up to you.

Part I: 

Install of Python's Extra Packages by Using Chris Fonnesbeck Procedure

  1. Open up a new terminal-window in Os X and simply write in one line (copy/paste is easiest):
    sudo curl -o install_superpack.sh https://raw.github.com/fonnesbeck/ScipySuperpack/master/install_superpack.sh
    and the result will looks like this:

    As noticed you will have to give login password for your computer to make it start downloading the script-file named install_superpack.sh.
  2. Now we need to run this script, so write this in the same terminal window:
    sudo sh install_superpack.sh

    The script will download and install a bunch of essential packages for python, and this might take some time (<20 min) if you are on a slow connection.
  3. That's it now we have the most essential packages installed and we can do some serious work. The packages installed were:

    *PyMC is a python module that implements Bayesian statistical models and fitting algorithms, including Markov chain Monte Carlo.

    *Statsmodels is a Python package that provides a complement to scipy for statistical computations including descriptive statistics and estimation and inference for statistical models.

    *pandas is a Python package providing data structures designed to make working with "relational" or "labeled" data both easy and intuitive for doing practical, real world data analysis in Python.

    *IPython: One of Python’s most useful features is its interactive interpreter. The goal of IPython is to create a comprehensive environment for interactive and exploratory computing.

    *matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.

    *SciPy includes modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and more.

    *NumPy is the fundamental package needed for scientific computing with Python. This package contains: a powerful N-dimensional array object, sophisticated (broadcasting) functions, tools for integrating C/C++ and Fortran code, useful linear algebra, Fourier transform, and random number capabilities.
  4. We can make a quick test and trying to load some of the modules into python to see if everything went OK. Start python in a terminal windows by simply writing python, then in the python interpreter just started, write import scipy if the >>> prompt just returned back then everything went probably fine. This is how it shall look like if OK:


    ...and if it was not OK, you will instead see:

Part II: 

Compile and Install of Gnuplot

  1. First we need to download the gcc compiler, this is part of the xcode package from apple, but  I do not use xcode, and so I just want the command-line-tools and these can be downloaded just as easily. Firstly go to Apples developer web page,

      that is https://developer.apple.com/downloads/.

    You will have to login with your apple_id. Search for the dmg-file: "Command Line Tools (OS X Mountain Lion) for X code" and download it, (~110Mb). Install it by open the downloaded file, then click on the package and follow the on-screen instructions.

  2. You can test if the gcc was installed properly by open up a terminal window and writing g++, This is how it  shall look like:

    ...and if it was not installed properly:

  3. Now we are ready to download and compile the necessary extra stuff that you need in order to compile gnuplot. Now the tuff part starts. The problem is that if you want to compile gnuplot, you will find out that you need some additional libraries installed in order to do this, and these additional libraries will also require some libraries.... that needs some extra libraries...etc, and so on, so forth. To make it simple for you, I have collected all the extra stuff you need in one download:

    you can download it here (these libraries were downloaded Jan-2013).

    However I will mention during each step, were you can go to grab the latest versions if you like. The additional stuff required is given in this list below and must be processed in list order, so lets start with, freetype2. If you used the collected file link above, unzip it, and in this folder you find each tar.gz files of the list below.
    I.
    freetype2
    II.
    pkg-config
    III.
    fontconfig
    IV.
    libpng
    V.
    JPEG
    VI.
    libgd
    VII.
    AquaTerm
    VIII.
    gnuplot
  4. freetype2, you can find at http://www.freetype.org/freetype2/index.html, under development section, download the latest tar.gz version (at writing this was 2.4.11), double-click the file to unpacks it into its own folder called Freetype2-2.4.11, (if you use Safari this is done automatically). Open up the terminal window and cd into this new folder (when you first start terminal, you end up in your home folder under this directory you find the Desktop folder and Download folder) To list and change directories use the ls and cd commands.


     Now we shall configure this source code by writing in the terminal window, this runs the configure script:
    ./configureYou will see some text output response and dependent on package-size this can be lots of text or as in this case very little text. The prompt is then given back to you, and now you are ready to compile the package and you do that by writing:
    make
    Compilation can some time take a very long time but for this gnuplot guide it is quite fast for all steps. When finished you follow up by a finally installing of the package, here you will be asked for login password.
    sudo make install
    Hopefully you finished the first package successfully and you got no "error and exit" failure.
  5. pkg-config, is our next task, you can find it here http://www.freedesktop.org/wiki/Software/pkg-config, look under the releases link and grab the latest tar.gz file (pkg-config-0.27.1.tar.gz), and now similar as the earlier step, unpack, use terminal and enter the pkg-config folder. Note: if you use folders in then path were you downloaded the file into that has strange characters, white spaces etc, you might get an error here, so better use simple folder names, or unpack the downloaded folder in your home folder. 

    Write in terminal: ./configure --with-internal-glib
    Followed by: make
    and finally: sudo make install , I guess that your password is needed once more.
  6. fontconfig, you find at http://www.freedesktop.org/wiki/Software/fontconfig, look under link called release and grab latest tar-gz file (fontconfig-2.10.2.tar.gz). Enter folder with terminal, same procedure as before, but now I give you a hint, instead of writing the three commands, one by one, you can give them all three att once by writing:
    ./configure && make && sudo make install

    (The && means that next command in line shall be done if the previous was successfully.)
  7. libpng, is the next one in line, this package will later give us the capabilities to handle PNG-image files in gnuplot. Grab it here http://www.libpng.org/pub/png/libpng.html, and look under the source-code section and download the latest tar.gz file (libpng-1.5.13.tar.gz),. Unpack and enter folder with a terminal window and guess what?, -do the three commands:
    ./configure && make && sudo make install





  8. jpeg, If we use PNG-images why not use JPEG's also? Go to http://www.ijg.org, and grab the latest file directly linked to at the front-page, (jpegsrc.v8d.tar.gz). Unpack, terminal, enter folder, and do the three command salut and confirm with password.
    ./configure && make && sudo make install
  9. libgd, All steps we just went through were needed in order to prepare for this single package. You are supposed to get it here http://libgd.org, but site has been down for more than a year?, and I never had any success with given alternatives. Luckily I found the library (version 2.0.35) floating around the web, so I stored it on my dropbox here.
    Do the download, unpack, terminal, and enter folder. But here I suggest running the three commands separately. So after the configuration command is given you will se a summary list, showing:

     You shall have four YES, one NO, and a final YES, if you have been successfully. If you have more than one NO, then you see in this list in which step you failed on earlier. Of course if you want XPM images capabilities you are on your own installing this, thus I do not need it myself.
    Continue with make and sudo make install to finnish this step.
  10. AquaTerm, If you have not earlier installed AquaTerm, this nice terminal for gnuplot in Mac Os X, then now is time to do so, go to: http://sourceforge.net/projects/aquaterm, and download latest version (1.1.0) as a dmg package and click and install this in a similar manner as you did with gcc in step one.
  11. gnuplot, yes we have arrived at the last step, now it is time to compile and install the one thing that we actually only wanted from the beginning, -gnuplot. Go to their webpage, http://www.gnuplot.info, and download their latest source as a tar.gz file, (gnuplot-4.6.1.tar.gz), you will find it under the Download from SourceForge link at the main page. Unpack the file and enter the folder within the terminal-window, then run the configure command once more. After the command finished, you can scroll back up in the window, and look for the topic line:
    ** Configuration summary for gnuplot 4.6.1:,  and under this section verify that JPEG, GIF, animated GIF and  PNG terminals is followed by: YES, also make sure that aqua terminal is included as well, you find it a little further down in the list. Se screen-dump below, section to look for is marked green.


     Complete this step by continuing with the commands:
    make and sudo make install.
  12. Now when you have gnuplot installed, the last thing to do is to make sure gnuplot finds the right fonts needed when you use other terminals than the default aqua. Do this test by opening up the terminal window again, either a new one or else you must change directory back to your home directory again. This you can do by writing cd followed by enter-key press. When you are located back in home, write in the terminal:
    vi .bash_profile , now you have started to edit a config-file for bash. Bash is the default shell you always automatically begin to use when you start the terminal window, if you look closely, bash is written in the window name at top of the terminal window. After you have run the command above you end up in the vi text editor and the screen is filled with rows of "~", just press i-key on your keyboard. You are now in "-- INSERT --" mode as seen at the bottom of the terminal window and that means that you now can write text into the file. Copy and paste this  one line into the file:
    export GDFONTPATH="$HOME/Library/Fonts:/Library/Fonts:/System/Library/Fonts"
    Then you press ESC-key, and now you see -- INSERT -- disappeared from bottom. Now press :-key and then write wq . As noticed these three characters are written in the bottom of the window. Press enter and the file is saved and you return back to the home directory. Note; If the config-file already exist you will instead see other config settings than just empty rows and in that case, use arrow-down key to go to bottom of file and insert the text at bottom instead.

    If 
    everything was successful you can test out gnuplot simply by writing gnuplot in a new terminal window and gnuplot will start,. Then in the gnuplot environment write set term gif to se if gnuplot finds the fonts, if OK, you will se this:

    If error you will instead se this, and you need to recheck if you wrote correctly in the config file or if you perhaps forgot to start a new terminal window before testing, this is needed, because the config-file will other wise not be reread.

  13. As a final test start a new terminal window and change directory back the the gnuplot directory were you compiled gnuplot, at step 11, in this folder there is a subfolder named demo, cd into this  folder and then start gnuplot. In the gnuplot environment write load "all.dem" followed by enter, either you can sit-back and enjoy all the capabilities that is now shown off by gnuplot, thus you have finished install gnuplot, or everything ha failed because you are at step number 13, the unlucky number.

Final Comment

The python home page is the first place to start with, if you are completely fresh at python try the beginners guide at the same page. The document section at scipy gives you more information on the numpy and scipy packages when you want to do something scientific.
The included demos in gnuplot can be viewed with a simple text-editor, and there you can get tips of how to do when making your own plots, therefore this folder is worth keeping. The other files/folders  you downloaded during the installation now you can delete, thus you do not need them anymore files have been installed else-where for you.
If you need more help when using gnuplot try writing help in the gnuplot enviroment. Wikipedia is also a good start http://en.wikipedia.org/wiki/Gnuplot.

The Q/A site stackoverflow.com is also a great place to visit if you have more detailed questions/problems.

Update Maverick Os X, 2014 Januari

I tried installing this on Maverick: Python (part I) no problem, Gnuplot (part II) I didn't manage with the new 1.1.1 version of AquaTerm and have to go back to the 1.1.0 version to succeed. Also Libpng needs to be compiled and installed before freetype nowadays.
Versions used:
libpng 1.6.8
Freetype 2.5.2
pkg-config 0.28
Fontconfig 2.11.0
Jpeg v9
libgd 2.1.0
AquaTerm1.1.0
gnuplot 4.6.4
Bunch of files: download it here (these libraries were downloaded Jan-2014)