sodcalculator

10 Tech How-To

Install Tomcat Ubuntu: Step-by-Step Guide

Guide 10-4842 · Updated · Step by step

This guide walks you through installing Apache Tomcat on Ubuntu, covering both the default apt package and the manual tarball method. You’ll learn how to set up Tomcat, configure users, enable the service, and access the manager apps, with troubleshooting tips for common issues.

What Is Apache Tomcat?

Apache Tomcat is an open-source Java servlet container that implements the Jakarta Servlet, Jakarta Server Pages, and Jakarta WebSocket specifications. It’s the most widely used web server for running Java-based web applications. Tomcat is not a full Java EE application server (like WildFly or GlassFish), but for many web applications, it provides everything needed to serve dynamic content.

Tomcat is written in Java, so it runs on any operating system with a Java Runtime Environment (JRE). On Ubuntu, you typically install OpenJDK (the open-source implementation of Java) and then Tomcat on top of it. Tomcat listens on port 8080 by default (HTTP), with the shutdown port set to 8005 and the AJP port at 8009.

Prerequisites: Java and Ubuntu Version

Before installing Tomcat, you need Java. Tomcat 10 and later require Java 11 or newer. Tomcat 9 works with Java 8 and up. The easiest way to check if Java is installed is to run:

java -version

If Java is not installed, install OpenJDK with:

sudo apt update
sudo apt install openjdk-11-jdk

For Tomcat 10, use OpenJDK 11 or 17. Ubuntu 20.04 LTS and 22.04 LTS both have OpenJDK 11 and 17 in their default repositories. You can verify the installation with java -version again.

Method 1: Installing Tomcat via APT (Ubuntu Repository)

The fastest way to install Tomcat on Ubuntu is to use the default apt repository. This installs Tomcat 9 on Ubuntu 20.04 and 22.04 (as of the time of writing).

  1. Update the package index: sudo apt update
  2. Install Tomcat 9: sudo apt install tomcat9 tomcat9-admin (the tomcat9-admin package includes the manager and host-manager web apps).
  3. Check the service status: sudo systemctl status tomcat9
  4. If it’s not running, start it: sudo systemctl start tomcat9
  5. Enable it to start on boot: sudo systemctl enable tomcat9

The apt package creates a system user tomcat and places files in /var/lib/tomcat9 (webapps), /etc/tomcat9 (configuration), and /var/log/tomcat9 (logs). The service runs as the tomcat user, which is a good security practice.

Method 2: Manual Installation from the Tarball

If you need a specific Tomcat version (e.g., Tomcat 10 or 11) or want full control over the installation location, download the binary distribution from the Apache Tomcat website.

  1. Download the latest Tomcat (e.g., 10.1.x) as a .tar.gz file from tomcat.apache.org.
  2. Extract it to /opt/tomcat: sudo mkdir /opt/tomcat && sudo tar xzvf apache-tomcat-10.1.34.tar.gz -C /opt/tomcat --strip-components=1
  3. Create a system user: sudo useradd -r -s /bin/false tomcat
  4. Give ownership to the tomcat user: sudo chown -R tomcat:tomcat /opt/tomcat
  5. Create a systemd service file at /etc/systemd/system/tomcat.service with the following content:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_BASE=/opt/tomcat"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Then reload systemd and start Tomcat:

sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat

Configuring Tomcat Users and Manager Access

To access the Manager and Host Manager web applications, you need to define a user with the appropriate roles. Edit /etc/tomcat9/tomcat-users.xml (or /opt/tomcat/conf/tomcat-users.xml for the manual install) and add a user like this:

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="strong-password" roles="manager-gui,admin-gui"/>

Restart Tomcat to apply the changes: sudo systemctl restart tomcat9 (or tomcat). Then access the manager at http://localhost:8080/manager/html and the host manager at http://localhost:8080/host-manager/html.

If you get a 403 Access Denied when accessing the manager from a remote machine, you need to edit the context file /etc/tomcat9/Catalina/localhost/manager.xml (or /opt/tomcat/conf/Catalina/localhost/manager.xml) and add a Valve that allows your IP:

<Context privileged="true">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1|192\.168\.1\.\d+" />
</Context>

Testing Your Tomcat Installation

To verify Tomcat is running, open a browser and go to http://localhost:8080. You should see the default Tomcat homepage with a message like “If you’re seeing this, you’ve successfully installed Tomcat.”

From the command line, you can also use curl:

curl http://localhost:8080 | head

If you see HTML output, Tomcat is responding. To stop Tomcat, use sudo systemctl stop tomcat9 (or tomcat).

Common Problems and How to Fix Them

  • Port 8080 already in use: Run sudo netstat -tulpn | grep 8080 to find the process. Change Tomcat’s port in server.xml (Connector port attribute) or stop the conflicting service.
  • Tomcat won’t start: Check logs at /var/log/tomcat9/catalina.out or /opt/tomcat/logs/catalina.out. Common causes are a missing Java installation or incorrect JAVA_HOME in the service file.
  • Manager returns 403: As explained above, restrict access via a RemoteAddrValve or add your IP to the allow list.
  • OutOfMemoryError: Increase heap size by setting CATALINA_OPTS="-Xms512m -Xmx1024m" in /etc/default/tomcat9 (or in the service file for manual install).
  • Tomcat runs, but webapp gives 404: Deploy your .war file to the webapps directory and ensure it’s named correctly (e.g., myapp.war will be accessible at /myapp).

Tomcat vs. Other Java Application Servers

Tomcat is often compared to full Java EE servers like WildFly, GlassFish, or Payara. The main difference is scope: Tomcat implements only the Servlet and JSP specifications, while full servers support EJB, JMS, CDI, and other enterprise APIs. For most web applications built with Spring Boot or plain servlets, Tomcat is sufficient and lighter. If you need EJB or JTA, consider WildFly or Payara.

Another alternative is Jetty, which is even more lightweight and often embedded in applications. Tomcat remains the most common choice for standalone Java web applications.

Security Considerations

When running Tomcat in production, follow these basic security practices:

  • Run Tomcat as a non-root user (the apt package does this automatically).
  • Disable the default manager and host-manager apps if you don’t need them, or restrict access by IP.
  • Set strong passwords for any user with manager roles.
  • Keep Tomcat and Java up to date with security patches.
  • Use a reverse proxy like Nginx or Apache to handle HTTPS, and block direct access to Tomcat’s AJP port (8009) from the internet.

Frequently Asked Questions

How do I uninstall Tomcat on Ubuntu?

For the apt package, run sudo apt remove tomcat9 and optionally sudo apt purge tomcat9 to remove configuration files. For a manual install, stop the service (sudo systemctl stop tomcat), disable it (sudo systemctl disable tomcat), delete the service file (sudo rm /etc/systemd/system/tomcat.service), and remove the installation directory (sudo rm -rf /opt/tomcat).

Can I run multiple versions of Tomcat on the same server?

Yes. Each Tomcat instance needs its own CATALINA_HOME and CATALINA_BASE directories, and must use different ports (e.g., 8080, 8081). You can run the apt-installed Tomcat 9 and a manual Tomcat 10 simultaneously by changing the port in the second instance’s server.xml.

How do I deploy a WAR file to Tomcat?

Copy the .war file to the webapps directory (e.g., /var/lib/tomcat9/webapps for apt, or /opt/tomcat/webapps for manual). Tomcat will auto-deploy it within a few seconds. Alternatively, use the Manager web app’s “Deploy” section to upload the WAR file.

Why does Tomcat use port 8080 instead of 80?

Port 80 is the default HTTP port and requires root privileges to bind on Linux. Tomcat runs as a non-root user for security, so it uses 8080. To serve on port 80, use a reverse proxy like Nginx or configure Tomcat to run with authbind (not recommended).

What is the difference between Tomcat 9 and Tomcat 10?

Tomcat 10 moved from the Java EE namespace (javax.*) to the Jakarta EE namespace (jakarta.*). This means applications built for Tomcat 9 (using javax.servlet) won’t run on Tomcat 10 without code changes. If you have legacy applications, stick with Tomcat 9. For new projects, Tomcat 10 or 11 is the way to go.

How do I change the Tomcat port?

Edit the server.xml file (in /etc/tomcat9 or /opt/tomcat/conf) and change the port attribute in the Connector element to your desired port. Then restart Tomcat.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →