Monday 14 July 2014

Installing Java, Ant and Maven on Raspberry Pi

In order to run Ruby Shoes 4 on my Raspberry Pi, I need to install JRuby. It hasn't been as straight forward as I'd like.The Raspian JRuby packages are very old and there is an issue that is stopping rbenv installing JRuby. Some of my problems could well be memory related as my Raspberry Pi is a model A.

I decided that I'd try to build JRuby from source. Building JRuby from source requires both Ant and Maven. When I went to install Maven with apt-get, the package manager also wanted to install so many other things including what seemed like three different Java runtimes.

I decided to install Ant and Maven myself. I installed Oracle's Java 1.7 Development Kit with apt-get:

    sudo apt-get install oracle-java7-jdk

I then used the instructions at the The Tao of Mac as a basis from which to directly install Ant and Maven.

The first thing to do was to set the JAVA_HOME environment variable which is required by Maven. I added the following line to my .bash_profile file in my home directory:

    JAVA_HOME="/usr/bin/java"

I downloaded the Ant 1.9.4 binary from Apache into my home directory, then deleted the manual folder using the File Manager. I then created a Java directory  in the /opt directory:

    sudo mkdir /opt/java

Next I moved Ant into that directory:

    sudo mv apache-ant-1.9.4 /opt/java

I then created a soft link to that directory to make it easier to install new versions of Ant in the future:

    cd /opt/java
    sudo ln -s apache-ant-1.9.4 ant

Next I added the ant/bin directory to the $PATH variable in my .bash_proifle:

    PATH="/home/pi/.rbenv/bin:$PATH:/opt/java/ant/bin"

The last thing with Ant was to check that it had been installed properly:

   pi@raspberrypi:/opt/java$ ant -version
   Apache Ant(TM) version 1.9.4 compiled on April 29 2014

The procedure for Maven was very similar, though this time I used wget to retrieve the Maven binary from the command line:

   cd ~
    wget http://www.apache.org/dist/maven/binaries/apache-maven-3.2.1-bin.tar.gz
    sudo tar -zxvf ~/apache-maven-3.2.1-bin.tar.gz
    sudo ln -s apache-maven-3.2.1 maven

Last thing to do was update my $PATH variable to include the Maven directory:

Of course, not installing Ant and Maven via the package manager means I will need to keep them up-to-date manually. I don't see that being a problem as I don't expect them to change that frequently or for me to make a lot of use of them.

Note: I have used my .bash_profile to store my $PATH and other environment variables. This means that I have to run a bash shell for them to come into effect. The standard Terminal on the Raspian desktop does not default to bash. The easiest way that I have found to open a terminal session on the desktop running bash is to use the "start" menu at the bottom lefthand corner of the screen and select Others-> bash. (I normally access the Raspberry Pi by using ssh from a MacBook. The terminal on the Mac loads bash by default.) 

Another thing to remember is that when changing your .bash_profile the changes don't take effect immediately. If you are using the desktop terminal, you need to close the terminal window and re-open it for the changes to take effect. If you are logged in to the shell, either directly or via ssh, you will need to logout and login again.

No comments: