Saturday, October 16, 2010

HOWTO: Setting Up a New Ubuntu GNU/Linux Box for Java/CFML Development

Since I wind up doing this every six months with each new release of Ubuntu, I thought this time around I'd document how I set up an Ubuntu GNU/Linux box for Java and CFML development. This is partly so I don't scratch my head thinking "was there anything else?" next time, but mostly for people who want to dive into GNU/Linux from another platform and need a quick step-by-step guide to help them get productive quickly.

Note that if you're setting up an Ubuntu server, the server-related stuff that follows (Apache, Tomcat, and MySQL) will be pretty much the same on a server as it is on a dev machine with a couple of differences. First, you'll want to have Tomcat autostart at boot on a server (which I don't do on my dev machine). Second, and this is a matter of personal preference, I tend to put Tomcat in my home directory on my dev box, but in /opt on the server.

So open up a terminal and let's the the easy apt-get stuff out of the way first.


INSTALL APACHE
sudo apt-get install apache2



INSTALL MYSQL
sudo apt-get install mysql-server mysql-client



INSTALL VIM & EMACS

sudo apt-get install vim emacs


You can also install Java and Tomcat via apt-get, but I prefer to download these items directly from their respective sources. We'll start with Java.


DOWNLOAD/INSTALL JAVA
  1. Open a browser and go to http://java.sun.com (sorry Oracle, I'm not using your URL until you turn this one off!)
  2. Mouse over "Downloads" and click on "Java for Developers"
  3. Scroll down a bit on the next page and click on the "Download JDK" button (you'll want the full JDK, not just the JRE)
  4. On the next page, click on the Platform button and choose either "Linux" (32-bit) or "Linux x64" (64-bit) as appropriate for your machine. Then scroll down and hit the "Continue" button.
  5. Under "Available Files" on the next page, click on the .bin file, not the .rpm.bin file.
  6. After the download completes (and I'm assuming you downloaded to your Downloads directory), open a terminal and do the following, hitting enter after every line (note the file name may be different based on the version you download):
    cd ~/Downloads
    chmod +x jdk-6u22-linux-i586.bin
    ./jdk-6u22-linux-i586.bin
  7. You'll see all the files extract, and you'll wind up with a jdk1.6.0_22 directory (or perhaps something different based on the version you downloaded). Personally I like to have Java in my /opt directory, so if you want to follow suit, type the following in your terminal (again hitting enter after each line):
    sudo mkdir /opt/java
    sudo mv jdk1.6.0_22 /opt/java

ADD JAVA ENVIRONMENT VARIABLES TO .bashrc


With Java in place, I like to set a couple of variables in .bashrc to make Java and Java applications a bit easier to work with. Navigate to your home directory (cd ~/) and open up .bashrc in vi or your favorite editor. Note that if you're new to GNU/Linux, the . before the file means it's hidden, so you won't see it in a plain old ls, but of course ls -a will show it.

With .bashrc open, scroll down to below the HISTSIZE and HISTFILESIZE lines (at least that's where I put this stuff), and you're going to add the following lines:

PATH=${PATH}:/opt/java/jdk1.6.0_22/bin
export PATH
JAVA_HOME=/opt/java/jdk1.6.0_22
export JAVA_HOME


Save the file, and then you'll have to log out of your terminal and log back in for these settings to take effect. Type exit and then hit enter to close your terminal, and then re-open the terminal. To make sure your settings took, type the following in your terminal, hitting enter after each line:

echo $PATH
echo $JAVA_HOME


You should see the Java directory included in your path, as well as see it for the value of JAVA_HOME.

Note that by editing .bashrc the changes only affect your user. If you want these settings available to all users, you'll be editing /etc/profile instead.


DOWNLOAD/INSTALL TOMCAT
With Java in place, let's grab Tomcat. You can of course substitute a different JEE server or servlet container if you're so inclined, but if you do you're on your own for the setup. ;-)
  1. Go to http://tomcat.apache.org in your browser.
  2. On the left-hand side under "Download" click on the latest version, which is currently Tomcat 7. (Note that Tomcat 7 is still technically in beta if you care about such things.)
  3. On the download page, scroll down a bit and under "Core" click on the .tar.gz link.
  4. After the file downloads (again assuming you have downloads go to your Downloads directory), in your terminal do the following (hit enter after each line):
    cd ~/Downloads
    tar -xvf apache-tomcat-7.0.2.tar.gz
  5. You'll see the files extract, and at the end of that process you'll have an apache-tomcat-7.0.2 directory in your Downloads directory. I prefer to have that in my home directory on my dev boxes, so type this in your terminal and hit enter (assuming you're still in your Downloads directory):
    mv apache-tomcat-7.0.2 ../
  6. Next to make Tomcat a bit easer to access, we'll add a symlink called tomcat that points to the real Tomcat directory. More terminal stuff:
    cd ../
    ln -s apache-tomcat-7.0.2 tomcat
With all that in place you can now access the Tomcat directory structure via ~/tomcat which I find pretty handy.


CONFIGURE TOMCAT
Next we need to configure Tomcat a bit, so in your favorite text editor, create a setenv.sh file in Tomcat's bin directory. If you want to do this in your terminal with vi, type the following:

cd ~/tomcat/bin
vi setenv.sh


What we'll be adding to this file is some settings that Tomcat will use when it starts up. Depending on your machine and/or preferences you may want to adjust the memory settings, but if you aren't sure what this stuff does, these are decent generic settings and you can always change them later.
Add these two lines to setenv.sh and then save the file:

export JAVA_HOME=/opt/java/jdk1.6.0_22
export CATALINA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=384m"


Again if you know what you're doing here, feel free to change your memory settings accordingly.
You should now be able to fire up Tomcat without any errors, so navigate to ~/tomcat/bin in your terminal and type ./startup.sh

You'll see a few lines spit out to the terminal, and if you don't see errors, open up a browser and go to http://localhost:8080 If you see the Tomcat welcome screen you're good to go.

Note that I won't be covering additional Tomcat configuration here, particularly hooking it into Apache, because there are a ton of other howtos and blog posts about that very topic. If you have specific questions in this area I'm happy to help, so comment below and I'll either comment or write another blog post as necessary.


ADDITIONAL CONFIGURATION
There's one additional bit of configuration I tend to do because particularly on a dev box, you'll run into a problem with this sooner or later if you abuse your machine like I do.

In a terminal (you and your terminal are friends by now, right?), or using your favorite text editor, you're going to edit /etc/security/limits.conf to increase the maximum number of files your user can have open. Open that file and add the following lines to the bottom of the file:

yourusername soft nofile 10000
yourusername hard nofile 10000



DOWNLOAD AND INSTALL SPRINGSOURCE TOOL SUITE/ECLIPSE
Unless you already have a favorite Java IDE, much as it's not in fashion to say so I'm a big fan of Eclipse. I personally have been using SpringSource Tool Suite (STS) for quite a while now, and it's a great Eclipse-based IDE for Java, Groovy, Grails, and of course CFML development once you throw in CFEclipse.

After downloading the Eclipse or STS of your choice, extract the files and then move them to your home directory (at least that's where I put them). This should be old hat by now--in your terminal do:

cd ~/Downloads
tar -xvf NAME_OF_FILE_HERE.tar.gz
mv NAME_OF_EXTRACTED_DIRECTORY_HERE ../


STS has a bit of a different directory structure since it comes with tcServer and Roo, so you'll actually go into the directory and be moving the STS directory into your home directory. Since it has a long name like springsource-tool-suite-sts-2.3.2-RELEASE or something like that, I tend to move and rename to something shorter, e.g.:

mv LONG_DIRECTORY_NAME_HERE ~/sts


Once things are moved, you can go into the directory that you moved to your home directory (which will be either sts or eclipse most likely), and then type either ./STS or ./eclipse to launch the program.


ADD LAUNCHER FOR STS/ECLIPSE
Although you can certainly start STS or Eclipse from the terminal if you like, most people tend to like to have a pretty button to click on to launch the program. The bad news is you have to add this yourself. The good news is it's very easy.
  1. Right click on the Ubuntu logo in the top left of the screen, and click on "Edit Menus."
  2. Click on "Programming," and then click on "New Item" on the right-hand side of the window.
  3. Leave the type drop-down set to "Application"
  4. For "Name" type whatever you want the name to be (e.g. Eclipse, SpringSource Tool Suite, whatever)
  5. For "Command" you need to tell the launcher not only where the executable is, but also where to find Java. So assuming your executable is ~/eclipse/eclipse you'd enter the following in the Command box:
    /home/yourusername/eclipse/eclipse -vm /opt/java/jdk1.6.0_22/bin
  6. You can leave the "Comment" box blank, but if you enter something here it's what will show up when you mouse over the icon for the application.
  7. Click on the "spring"-looking icon in the left-hand side of the Create Launcher box and you can choose an icon. Note that they're kind of in a weird format. STS comes with its own icon so you can navigate to the STS directory and select the icon.xpm file. If you're using Eclipse you can search for 'eclipse icon launcher ubuntu' on the interwebs and find stuff you can use.
  8. Click OK and your launcher will be added to the Programming menu when you click on Applications in the top left of the screen.
INSTALL CFECLIPSE

If you're a CFML developer you likely already know how to install CFEclipse and since Eclipse is Java based it's the same on every platform. If you're not familiar with installing CFEclipse, head on over to the web site and you'll find great instructions.

That's pretty much it! You're now a hard-core Java/CFML developer on GNU/Linux, other than all that pesky Tomcat/Apache configuration of course. As I said earlier if you need help with that, comment below and I'll do my best.

Enjoy!

3 comments:

Neurotic said...

Any reason who you don't add the Ubuntu partner repositories, and install Java that way? Or do you like to have tight control over which version of the JDK you have on the machine?

Matthew Woodward said...

Right Mark--I just like to know exactly which Java I'm getting and have the flexibility to manage Java myself. That's the only real reason.

Akbarsait said...

The steps are really helpful and handy. Thanks for posting.