How To Install Ruby On Rails on Ubuntu 12.10

After much searching online, and a bit of trial and error, I have come up with the following commands, which when run should result in a working Ruby on Rails environment on Ubuntu 12.10.  I haven’t tested these steps on any other version of Ubuntu, but I imagine it would be at least similar for other recent versions of Ubuntu.

First you need to make sure your system is up to date:

sudo apt-get update

Next, you’ll need to install curl and NodeJS.  Curl is required to download the rvm bash script and NodeJS is needed for your local javascript execution environment.  If you don’t install NodeJS (or another javascript execution environment), you will see an error when you try to run a web app.

sudo apt-get install curl nodejs

The next commands will install RVM (Ruby Version Manager), Ruby 1.9.3, and all other dependencies needed for Ruby.

curl -L get.rvm.io | bash -s stable --auto
source ~/.rvm/scripts/rvm
. ~/.bash_profile
rvm requirements
# The next line should be what rvm requirements said to install for Ruby
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkgconfig
rvm install 1.9.3

This last command will initially show a screen which tells you to run the earlier apt-get install command.  You should press q to skip this screen.

When ruby has finished installing (which may take a few minutes), you should see something like:

Install of ruby-1.9.3-p327 - #complete

The part I have made bold is the version & patch level you should use in the next command.  This command will make sure Ruby is the correct version by default.

rvm --default use 1.9.3-p327

Now that you have installed Ruby, you can install Rails.  This command installs Rails 3.2.13

gem install rails -v 3.2.13

You should now have a working version of Ruby on Rails on Ubuntu 12.10 setup.

To test this, you can run:

rails new blog
cd blog
rails s

The first command creates the basic Ruby on Rails application framework, the second command just moves your terminal into the new directory, and the third command starts a local development server.

If these 3 commands work, you will be able to open http://localhost:3000 in your favorite browser and view the default ruby web app.

Building Axis2 Java Classes From WSDLs Using Ant Foreach

I’ve had a few requests to get an example of the build script I referenced in my post, Ant Foreach Properties, so here it is.

In our ant build.xml file we have a task called buildAllWsdls which gets all of the WSDL files from the web service URL, then feeds each of them into Axis2 to convert them to Java classes.

Here are the basic steps of what this build does:

  1. Finds each property which ends with “Service” and concatenates them into a comma delimited string (ex. ATestService, BTestService, CTestService…)
    <propertyselector property="services.list" delimiter="," match="(w)*Service" />
  2. Loop through each of those Service properties using the foreach task and get the WSDL file from the WSDL end point URL.
    <foreach list="${services.list}" inheritall="true" target="-getSingleWsdl" delimiter="," param="prettyName" />
  3. Translate the namespace to package properties file into a comma delimited list (as required by Axis 2).
    <loadfile srcfile="${conf.dir}/NStoPkg.properties" property="ns2p.all">
         <filterchain>
             <striplinecomments>
                 <comment value="//" />
             </striplinecomments>
             <prefixlines prefix="," />
             <striplinebreaks />
             <tokenfilter>
                 <trim />
                 <ignoreblank />
             </tokenfilter>
         </filterchain>
     </loadfile>
  4. Run each WSDL in the download directory through the Axis2 Java class creation Ant task.
     <foreach target="-createStubs" param="file" inheritall="true">
         <path>
             <fileset dir="${wsdldir}" />
         </path>
     </foreach>
    
    <target name="-createStubs">
        <basename property="wsdl" file="${file}" />
        <echo message="Generating client code for service: ${wsdl}" />
        <java fork="true" classname="org.apache.axis2.wsdl.WSDL2Java" failonerror="true">
            <arg line="-ns2p ${ns2p.all} -s -u -uri ${wsdldir}/${wsdl} -o ${stubsdir} --noBuildXML" />
            <classpath refid="cp.axis" />
        </java>
    </target>

If you don’t mind your web services being in a Java package that corresponds with the web service namespace, you can skip step 3 and leave out the -ns2p command line argument in the java ant task in step 4.

Here is the full example Ant build.xml file for doing this:

<project name="Axis2WSDLBuild" default="buildAllWsdls" basedir=".">
 <tstamp>
 <format property="TODAY" pattern="MM/dd/yyyy HH:mm:ss"/>
 </tstamp>

 <property name="ws-domain" value="http://services.test.com:8080" />
 <property name="webservices.dir" value="${app.dir}/axisStubsProject" />
 <property name="wsdldir" value="${webservices.dir}/wsdls" />
 <property name="stubsdir" value="${webservices.dir}" />

 <!-- patternsets -->
 <patternset id="jar.files">
 <include name="**/*.jar"/>
 </patternset>

 <path id="cp.axis">
 <fileset dir="${lib.dir}/axis">
 <patternset refid="jar.files"/>
 </fileset>
 </path>
 <path id="cp.antcontrib">
 <fileset dir="${lib.dir}/antcontrib">
 <patternset refid="jar.files"/>
 </fileset>
 </path>

 <!-- Allow usage of ant-contrib defined tasks -->
 <taskdef resource="net/sf/antcontrib/antlib.xml">
 <classpath refid="cp.antcontrib"/>
 </taskdef>

 <!-- =================================================================== -->
 <!-- Project initialization                                              -->
 <!-- =================================================================== -->
 <target name="init">
 <property name="version" value="build ${TODAY}"/>
 <property file="${conf.dir}/soa-addresses.properties"/>
 <filter token="version" value="${version}"/>
 <filter token="today" value="${TODAY}"/>
 </target>

 <!--=======================================================-->
 <!-- TARGET [buildAllWsdls]                                -->
 <!-- Target which gets and creates the web service stubs necessary for client invocation. -->
 <!--=======================================================-->
 <target name="buildAllWsdls" depends="init">
 <antcall target="wsdlGet" />
 <antcall target="buildWsdls" />
 </target>

 <!--=======================================================-->
 <!-- TARGET [buildWsdls]                                -->
 <!-- Target which creates the web service stubs necessary for client invocation. -->
 <!--=======================================================-->
 <target name="buildWsdls">
 <!-- create the download directory -->
 <mkdir dir="${stubsdir}" />
 <!--
 Translate the namespace to package properties file (as typically used in
 an Axis 1 implementation) into a comma delimited list (as required by
 Axis 2).
 -->
 <loadfile srcfile="${conf.dir}/NStoPkg.properties" property="ns2p.all">
 <filterchain>
 <striplinecomments>
 <comment value="//" />
 </striplinecomments>
 <prefixlines prefix="," />
 <striplinebreaks />
 <tokenfilter>
 <trim />
 <ignoreblank />
 </tokenfilter>
 </filterchain>
 </loadfile>
 <!-- run client generation for each WSDL in the download directory -->
 <foreach target="-createStubs" param="file" inheritall="true">
 <path>
 <fileset dir="${wsdldir}" />
 </path>
 </foreach>
 </target>

 <!--=======================================================-->
 <!-- TARGET [wsdlGet]                                -->
 <!--  Target retrieving all the WSDL files necessary for generating client code. -->
 <!--=======================================================-->
 <target name="wsdlGet">
 <!-- create the download directory -->
 <mkdir dir="${wsdldir}" />

 <!-- find the properties related to WSDL locations -->
 <propertyselector property="services.list" delimiter="," match="(w)*Service" />

 <!-- for each property found, retrieve the WSDL -->
 <foreach list="${services.list}" inheritall="true" target="-getSingleWsdl" delimiter="," param="prettyName" />

 </target>

 <!--=======================================================-->
 <!-- TARGET [-getSingleWsdl]                                -->
 <!-- Hidden Target to retrieve a single WSDL. -->
 <!--=======================================================-->
 <target name="-getSingleWsdl">
 <!-- get the property value -->
 <propertycopy property="fullPath" from="${prettyName}" />
 <!-- download the WSDL file -->
 <get src="${ws-domain}/${fullPath}?wsdl" dest="${wsdldir}/${prettyName}.wsdl" verbose="true" usetimestamp="true" />
 </target>

 <!--=======================================================-->
 <!-- TARGET [-createStubs]                                -->
 <!-- Hidden target to run WSDL2Java on an input file. -->
 <!--=======================================================-->
 <target name="-createStubs">
 <!-- strip the full path from the file -->
 <basename property="wsdl" file="${file}" />
 <echo message="Generating client code for service: ${wsdl}" />
 <java fork="true" classname="org.apache.axis2.wsdl.WSDL2Java" failonerror="true">
 <arg line="-ns2p ${ns2p.all} -s -u -uri ${wsdldir}/${wsdl} -o ${stubsdir} --noBuildXML" />
 <classpath refid="cp.axis" />
 </java>
 </target>

</project>

the soa-addresses.properties file, which contains the different web service URLs:

ATestService=services/ATestService
BTestService=services/BTestService
CTestService=services/CTestService
DTestService=services/DTestService
ETestService=services/ETestService

and the NStoPkg.properties file which converts the default web service namespace based off the WSDL URL to a specified java package:

http://typens.test.com/common=com.test.axis.common.types
http://servicens.test.com/common/ATestService=com.test.axis.common.services.atestservice
http://servicens.test.com/common/BTestService=com.test.axis.common.services.btestservice
http://servicens.test.com/common/CTestService=com.test.axis.common.services.ctestservice
http://servicens.test.com/common/DTestService=com.test.axis.common.services.dtestservice
http://servicens.test.com/common/ETestService=com.test.axis.common.services.etestservice

Ant Foreach Properties

Update 10/23/2009: An example of this build has been posted.

I’ve been working with Apache Ant a bit at work the past couple weeks. We’ve been setting up a Hudson continuous integration server to automate some of our builds and deployments. We’re also hoping to setup more automated tests using tools like Selenium, HTTPUnit, JUnit, etc., but those are going to be gradually added with the coming releases.

I’ve worked with Ant a lot before, but hadn’t really used the Ant-Contrib Tasks until modifying this build script. The foreach task is useful for running a list of things through a certain ant target. We have a properties file that has the webservice name and the associated WSDL URL. We feed that list to an Ant target through the foreach task (for each URL, go get the WSDL file/definition), then we generate Axis2 stubs from each WSDL file. To automate the generation/retrieving of these WSDL files I was moving the base URL (ex: http://services.test.com/) to the environment property file, since this is different in each environment (Test, QA, Production), but the relative URL (ex: /services/…) stays the same. So to summarize, the webservices properties file now has ExampleService=/services/ExampleService and the environment properties file has SERVICES-DOMAIN=http://services.test.com (SERVICES-DOMAIN is assigned to the ws-domain after the environment properties file is loaded in the ant build file).

Here’s where I ran into a little issue that I wanted to share so no one else has to spend a half hour debugging it. Well, also so I can find the answer here the next time I run into it. The property for the base URL wasn’t evaluating when I was running the WSDL list through the foreach call to get/download the WSDL file. It kept erroring saying ${ws-domain}/services/ExampleService?wsdl couldn’t be found. I spent a few minutes putting echo’s in the ant script to see where it was failing until I saw ${ws-domain} was set up until the ant target that gets the WSDL file. It was then I saw this foreach task attribute:

Attribute Description
inheritall If true, pass all properties to the called target. Defaults to false.

Now I’m not sure why you wouldn’t want properties passed, but I’m sure there is some good reason why this is false by default. Maybe. So of course, adding this attribute set as true to the foreach ant task made everything work without a hitch.

Project Euler Primes

I first checked out Project Euler a couple years ago when one of my friends mentioned it to me. If you haven’t heard of it before it is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve.

I took a couple classes in college for basic C++ programming, but we never got too deep. It was mostly principles of programming, etc. and, since it was a class aimed at Electrical Engineers first programming class and not Computer Science majors, we spent a bit of time on structures of programs and other general concepts that are core to most programming languages. I was (and still am) interested in strengthening my C++ knowledge, so I decided to try the Project Euler problems in C++.

So far I haven’t completed many (13 out of 228), but haven’t spent too much time on it, even though I started a couple years ago… The questions range in difficulty from Problem #1 Find the sum of all the multiples of 3 or 5 below 1000. to Problem #198 How many ambiguous numbers x = p/q, 0 < x < 1/100, are there whose denominator q does not exceed 108? The higher number problem # doesn’t necessarily mean it is more difficult, but difficulty is measured by how many people have solved the problem. You can see a trend of how people may start doing some of the problems, but quickly the numbers dwindle. For example, over 48,000 people have completed problem #1, but less than 20,000 people have completed more than 10 and less than 2,000 people have completed more than 25 problems. Once you’ve solved a problem, you can look at other ways people have solved it in other languages or even by hand. However, you can’t see any hints or other posts about a problem on the site until you’ve typed in the correct answer for that problem.

Today I spent a little bit of time optimizing some of the old algorithms I had used for previous problems. (Several algorithms can be re-used between problems and the less time it takes for them to run, the less time you have to wait to see if your answer is correct…) I started with optimizing my function to check if a number is a prime number.

There are basic things you can do to make figuring out if a number is prime or not. For example, it’s very easy and quick to find the remainder of a number when it is divided by 2 or 3. Since this is a lot of the cases for non-prime numbers, it’s best to check these cases first before checking if a number is divisible by any other number. A couple other quick (and perhaps obvious) things to do is to not check every single number from 1 … n where n is the number you are trying to check is prime or not… We know that anything even is divisble by 2 , so instead of checking if the number is divisible by 2, 4, 6, 8… start by checking if it’s divisible by 5 (since we’ve already checked 2 and 3) and check every odd number from there (5,7,9,11…). This cuts your computations in half. Also, we know that we only have to check up to the square root of the number (16/2 = 8, so there’s no point in checking if there is a remainder with 16/8). This cuts the computations in at least half again. After adding this optimizations, my code went from a minute or two to run down to a few seconds.

If you enjoy programming, math, or especially both, I would recommend checking out some of the problems on the site. It is a great way to keep your math skills from getting too rusty and a great way to learn how to do some math programming in a new language. If nothing else, you’ll get your mind thinking of different ways to solve problems. ^()

(Re)Start a Blog

Well it’s 2009 and I’ve finally started blogging again… So, with it being the first day of the year, it only seems fit that I post about some of my goals for the year. (And gives me an excuse to cross one off already). Leave a few of your goals for 2009 in the comments.

Start blogging (again). I had a blog a few years ago that I posted on pretty frequently for a while. Then I got busy and I just dropped it and the domain name. I’ve wanted to start blogging again for a while, but just hadn’t taken the time to start the process of getting the domain names and hosting. I think blogging is important for anyone working in the computer industry because it helps you stay on top of things. So hopefully I’ll be posting some note-worthy things here in the coming year to help people besides myself.

Read more of the Bible. I’ve started reading a chapter from the Bible every night recently and it’s been going well so far. I just finished reading Ezekial, and if you’ve ever read it, you know it’s hard to comprehend so I’ve just started Psalms, which should be a little easier of a read. I want to read more of the Bible besides just one chapter a night, though. There are 1189 chapters in the Bible, so even if I read two chapters per day, it would still take over 1.5 years straight to read the entire thing. (I plan on reading the whole Bible, but that is a lifetime goal.) I read Proverbs recently and would recommend anyone, Christian or not, to read through it. A majority of it, as you can guess, are sayings that most people hear or say throughout their lifetime.

“Hatred stirs up dissension, but love covers over all wrongs.” Proverbs 10:12

Work out regularly. I’m sure this one is on most everyone’s New Year’s Resolutions. I started doing small work outs semi-frequently towards the end of last year. If you follow me on Twitter, you probably saw me tweet about hundred push ups. If you need a place to start, I would greatly recommend this program. You only do push ups three days a week (Monday, Wednesday, and Friday worked best for me) for six weeks. It felt great to do the exercises and the difference it was making. Of course since the program only goes for 6 weeks, you can guess what happened at the end… I’ve started doing pushups again the past few nights, but I would also like to do some other exercises to help the rest of my body, too.

Take more, and hopefully better, pictures. I’ve had a digital SLR camera for about 8 years now and it wasn’t till last year that I actually started working with the manual settings on the camera. Ever since starting that, I’ve had more inspiration to take more pictures. Since I don’t really do any post-corrections on my pictures, just adjusting the manual settings has made them look much better. I’d like to take many more pictures, and possibly even some that could be sold or used for someone besides myself.

Contribute to an open source project. I think contributing to something like an open source project is a great way to both become a better programmer and meet other people like yourself. I’m not sure which project I would try to help out, but I’m sure one will stick out at me sooner or later.

Finish (and start) some of the projects I haven’t made time to work on. I’ve sadly been neglecting a few projects that I’ve been meaning to work on for a while (one I started with someone almost 2 years ago). Hopefully we can get kicking on that one and a few others and get some products/services out there for people to use and enjoy.

Wake up at 5am, every day. Like a few of my other goals, I had been doing this for a bit last year, but stopped for one reason or another. I’ve changed my alarm so it is much more annoying and loud, which gives me extra incentive to turn it off so I don’t wake the rest of the family too much. This should give me some time to work on all of these things without having to forfeit anything on my final goal…

Continue to spend good quality time with my family. If none of these other goals get done because I don’t have time, that’s alright, as long as I get to be with my loved ones.