Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

Installing Tomcat 6 on Ubuntu

If you are running Ubuntu and want to use the Tomcat servlet container, you should not use the version from the repositories as it just doesn't work correctly. Instead you'll need to use the manual installation process that I'm outlining here.

Before you install Tomcat you'll want to make sure that you've installed Java. I would assume if you are trying to install Tomcat you've already installed java, but if you aren't sure you can check with the dpkg command like so:

dpkg –get-selections | grep sun-java

This should give you this output if you already installed java:

sun-java6-bin                                   install
sun-java6-jdk                                   install
sun-java6-jre                                   install

If that command has no results, you'll want to install the latest version with this command:

sudo apt-get install sun-java6-jdk

Installation

Now we'll download and extract Tomcat from the apache site. You should check to make sure there's not another version and adjust accordingly.

wget http://apache.hoxt.com/tomcat/tomcat-6/v6.0.14/bin/apache-tomcat-6.0.14.tar.gz

tar xvzf apache-tomcat-6.0.14.tar.gz

The best thing to do is move the tomcat folder to a permanent location. I chose /usr/local/tomcat, but you could move it somewhere else if you wanted to.

sudo mv apache-tomcat-6.0.14 /usr/local/tomcat

Tomcat requires setting the JAVA_HOME variable. The best way to do this is to set it in your .bashrc file. You could also edit your startup.sh file if you so chose.

The better method is editing your .bashrc file and adding the bolded line there. You'll have to logout of the shell for the change to take effect.

vi ~/.bashrc

Add the following line:

export JAVA_HOME=/usr/lib/jvm/java-6-sun

At this point you can start tomcat by just executing the startup.sh script in the tomcat/bin folder.

Automatic Starting

To make tomcat automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown.

sudo vi /etc/init.d/tomcat

Now paste in the following:

# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

export JAVA_HOME=/usr/lib/jvm/java-6-sun

case $1 in
start)
        sh /usr/local/tomcat/bin/startup.sh
        ;;
stop)  
        sh /usr/local/tomcat/bin/shutdown.sh
        ;;
restart)
        sh /usr/local/tomcat/bin/shutdown.sh
        sh /usr/local/tomcat/bin/startup.sh
        ;;
esac   
exit 0

You'll need to make the script executable by running the chmod command:

sudo chmod 755 /etc/init.d/tomcat

The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.

sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat

Tomcat should now be fully installed and operational. Enjoy!

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 10/11/07 and tagged with: Linux

Comments (31)

  1. Seth

    I think another part of this install (probably for any app that generates logs) is to add an entry to logrotate. Otherwise you get gobs and gobs of logs, especially on a production machine. Don't ask me how I know this. I just know it. (/me hangs head in shame)

    How about an article about logrotate?

  2. The Geek

    Great suggestion! Will add it to the queue…

  3. steve meeks

    Thanks for the how to: It worked flawlessly, and I just upgraded to Gutsy Gibbon last night. For some reason, I hadn't been able to get Tomcat to work for awhile now, and I found this article today. Thanks again. If it weren't for people who go out of there way to share this kind of knowledge, I would have never discovered the wonderful world of Linux and Open Source technology. Thanks again.

    Steve

  4. Aaron

    Hi there, thanks for your great tutorial. But I am having a little problems.

    1. NoClassDefinition: When I want to shutdown tomcat. It always gives me that error. But when i startup there are no problems at all.

    2. After I followed all your instructions. How do I know if the installation works? I tried moving to http://10.0.106.21/ (which is the IP address of the server), Tomcat does not seem to load, I got a 404 error. Any idea why is this so?

    Thanks
    aaron

  5. Seth

    @aaron:
    1. Does it say which class it can't find? Can you post your full stacktrace? That'll help track down the problem.

    2. Try http://127.0.0.1:8080 to view it after it's running. It doesn't run on port 80 by default.

  6. Dan

    I have the same problem that Aaron has. JRE and JDK are installed and the variables are set.

    ~ sudo /opt/apache-tomcat/bin/catalina.sh run
    Using CATALINA_BASE: /opt/apache-tomcat
    Using CATALINA_HOME: /opt/apache-tomcat
    Using CATALINA_TMPDIR: /opt/apache-tomcat/temp
    Using JRE_HOME: /usr/lib/jvm/java-6-sun
    Exception in thread "main" java.lang.NoClassDefFoundError:

    I don't know how to get full stacktrace. Any help is much appreciated.
    Dan

  7. Seth

    You logs should be in /usr/local/tomcat/logs/ if you used the directions above. Looks like yours will be in /opt/apache-tomcat/logs.

    There should be a file named localhost.YYYY-MM-DD.log in there (where YYYY-MM-DD is the year, month, day respectively).

    Peruse that file and there should be a stack trace in there.

    If there isn't, do the following from the command-line:
    > cd /opt/apache-tomcat/logs
    > grep -l Exception *.log

    That should list all the files that have a stack trace in them. Look at those files and it should say where the error is occuring.

    HTH,
    SETH

  8. Dan

    Thank you for your help but i can't find any of these log files.
    The folder /opt/apache-tomcat/bin/log/ is empty and in /opt/apache-tomcat/logs/ is only one file catalina.out. In catalina.out is only one line Exception in thread "main" java.lang.NoClassDefFoundError:
    Is there a way get more information out of tomcat. That stacktrace, if that is one, seems not very helpful.

    Dan

  9. Seth

    Weird. It's not even finding the Bootstrap class to start Tomcat. I thought the error was happening further down the chain.

    Add the following line to catalina.sh:

    echo "CLASSPATH: $CLASSPATH"

    it should go around line 232 after the "run" command check

    elif [ "$1″ = "run" ]; then
    shift;
    ### NEW COMMAND ON NEXT LINE
    echo "CLASSPATH: $CLASSPATH"

    Send the output along. That'll help find the problem.

  10. Dan

    I got the following line
    CLASSPATH: :/opt/apache-tomcat/bin/bootstrap.jar:/opt/apache-tomcat/bin/commons-logging-api.jar
    The bootstrap.jar is where it suppose to be but the commons-logging-api.jar is nowhere to be found on my system. Can this cause the problem?

  11. Seth

    Not having the commons-logging-api.jar shouldn't matter unless you've configured something from the default.

    What happens when you run:

    javap -classpath "/opt/apache-tomcat/bin/bootstrap.jar" org.apache.catalina.startup.Bootstrap

    It should print out the methods of the class.

  12. Dan

    That works.

    ~ javap -classpath "/opt/apache-tomcat/bin/bootstrap.jar" org.apache.catalina.startup.Bootstrap
    Compiled from "Bootstrap.java"
    public final class org.apache.catalina.startup.Bootstrap extends java.lang.Object{
    protected static final java.lang.String CATALINA_HOME_TOKEN;
    protected static final java.lang.String CATALINA_BASE_TOKEN;
    protected java.lang.ClassLoader commonLoader;
    protected java.lang.ClassLoader catalinaLoader;
    protected java.lang.ClassLoader sharedLoader;
    public org.apache.catalina.startup.Bootstrap();
    public void init() throws java.lang.Exception;
    public void init(java.lang.String[]) throws java.lang.Exception;
    public void start() throws java.lang.Exception;
    public void stop() throws java.lang.Exception;
    public void stopServer() throws java.lang.Exception;
    public void stopServer(java.lang.String[]) throws java.lang.Exception;
    public void setAwait(boolean) throws java.lang.Exception;
    public boolean getAwait() throws java.lang.Exception;
    public void destroy();
    public static void main(java.lang.String[]);
    public void setCatalinaHome(java.lang.String);
    public void setCatalinaBase(java.lang.String);
    public static java.lang.String getCatalinaHome();
    public static java.lang.String getCatalinaBase();
    static {};
    }

  13. Dan

    I downloaded Tomcat 5.5 and it works just fine right out of the box. That is weird.

  14. Dex

    Thanx man…i tried Tomcat6.0.14 with above instruction.It worked 100%

  15. Jose C. Lacal

    Great article. Some additional pieces of the puzzle:

    chmod +x ./bin/*.sh

    Error messages such as this are fixed with the above command:
    "The BASEDIR environment variable is not defined correctly"

    And Tomcat 6.0.14 runs on :8080 and not :8180

  16. Didik Susilo

    I've tried this tutorial to Xubuntu 7.10 with apache2.
    It's working….100%
    This is a great tutorial…..thanks

  17. llsutherland

    I'm a newbie to Linux. This article was excellent! Thank you.

  18. Fred

    Hey guys - cool howto, but one question remains - now tomcat starts as root. How can i change it to let it run as different user?

  19. Gustavo

    tomcat is runing ok, but, i cant open http://localhost:8080/manager/html, how to configure tomcat's users?

  20. Jörgen Persson

    Thanks for the article.
    Haven't tried i yet but I'm sure it works. I have a question though.

    How do you set up two different tomcats, e.g. one dev and one prod, on the same computer to autostart? Is it just do create two different scripts in init.d? Like tomcat-dev and tomcat prod and have them point to their respective environments?

  21. Jörgen Persson

    Gustavo, in CATALINA_BASE/conf/tomcat-users.xml, add the following lines

    restart tomcat and launch http://localhost:8080/admin

  22. Jörgen Persson

    hmm, the lines were removed when posting, trying again

    <role name="admin"/>
    <user name="admin" password="deep_dark_secret" roles="admin"/>

  23. Erik Alm

    Thanks for the article!

    However, instead of creating the symlinks by hand you should take a look at "update-rc.d"…

  24. Kevin

    How would I unstall it?

  25. Terry

    "you should not use the version from the repositories as it just doesn't work correctly." -= all very well when you wrote this. Please note this article was written in October of 2007. Ubuntu is ever being updated. new release is out, Hardy Heron 8.04 !
    Maybe this Tomcat you speak of will work now.

  26. The Geek

    @Terry

    Good point… I'm going to test that out and see if the repo version works correctly. This guide is still relatively useful if you wanted to do a manual install, of course.

  27. Mark

    @Terry

    The default installation of Tomcat5.5 from the repos in 8.04 is borked. I followed the instructions above and have TC6.0.14 running perfectly thanks to The Geek :)

  28. The Geek

    @Mark

    Thanks for testing that out, I hadn't had a chance… glad it worked for you :)

  29. Micheál

    Hi,

    Thanks for the howto - very straightforward and pretty idiot -proof, which doesn't say much for me as I have a slight problem :)

    I set everything up, and all went well. I had cause to restart my machine over the weekend, and tomcat started automatically, great stuff so far. The problem occurs when I need to restart Tomcat having updated classes. The log files are created using the 'root' user (so owner and group is root), so I get a 'permission denied error when tomcat tries to access them if my normal user executes the ./shutdown script.

    Can you tell me how to change things so that, when starting the machine, tomcat runs under the regular user, rather than under root? Or is that not a good idea?

    Thanks,

    Micheál

  30. Michael Mrozek

    The argument to dpkg in the first step should be –get-selections (two dashes)

  31. Abhi

    Tomcat is not starting automatically even I followed the last steps necessary for making it starting at startup.I have to manually starting it up.


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.