Install Tomcat on Linux
Apache Tomcat is the most widely used open-source servlet container for running Java web applications —
the thing that actually executes your .war files. Installing it from the official tarball
gives you a clean, version-pinned server without tying it to the package manager. This guide installs
Tomcat 9 on Ubuntu, creates a dedicated unprivileged user to run it, and verifies everything end to end
with a request to localhost:8080. See the
Java installation guide first if you need a JDK on the machine.
Step 1: Install the JDK
Tomcat is written in Java, so it needs a Java runtime on the host. Tomcat 9 requires Java 8 or later. Check what is already installed:
$ java -version
If the command is not found, install the default JDK:
$ sudo apt-get update
$ sudo apt-get install default-jdk -y
Re-check the version before continuing — Tomcat will not start without a working Java:
$ java -version
Step 2: Create a Dedicated Tomcat User
Running a public service as root is a security hole — a compromised app then runs as root. The standard practice is an unprivileged, non-login system user that owns only Tomcat's files:
$ sudo groupadd tomcat
$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
-s /bin/false means this account cannot log into a shell.
Step 3: Download and Extract Tomcat 9
Download the tarball into /opt (the standard location for third-party software) and extract
it:
$ cd /opt
$ sudo wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.21/bin/apache-tomcat-9.0.21.tar.gz
Extract and normalize the directory name:
$ sudo tar -xvzf apache-tomcat-9.0.21.tar.gz
$ sudo mv apache-tomcat-9.0.21 /opt/tomcat
Grant ownership of everything to the tomcat user so it can read and write its own files:
$ sudo chown -R tomcat:tomcat /opt/tomcat
Step 4: Start Tomcat
Tomcat's startup script is catalina.sh in bin. Add that directory to the PATH
for the current terminal, then start the server:
$ cd /opt/tomcat/bin
$ export PATH=$PATH:$(pwd)
$ ./catalina.sh start
Expected output:
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
...
Tomcat started.
Verify the Installation
Tomcat listens on port 8080 by default. Verify from the same machine:
$ curl -I http://localhost:8080
Expected result — an HTTP/1.1 200 response:
HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
...
If you are on the server itself you can also open http://localhost:8080 in a browser. To
confirm the process is alive, check with netstat:
$ netstat -tulpn | grep 8080
The Java process listening on 8080 is your Tomcat. If you need to expose it externally, open the port in your firewall and make sure Tomcat binds to the right interface (see network interfaces).
Common Problems
Problem 1: "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
Cause: catalina.sh cannot find Java because the shell that starts it has
no Java in its PATH or no JAVA_HOME.
Solution: export the JDK location before starting:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 (adjust to your install — see
JAVA_HOME setup), then start again.
Problem 2: "Address already in use" / "Bind exception: Port 8080 already in use"
Cause: another process — often a second Tomcat, or an app like Jenkins — owns 8080.
Solution: find the owner with netstat -tulpn | grep 8080, stop it or
change Tomcat's port in /opt/tomcat/conf/server.xml (the <Connector
port="8080"> line), then restart.
Problem 3: Tomcat starts but pages show 404 / nothing listens
Cause: a failed deployment or the process died right after start; also possible if
files under /opt/tomcat are owned by root (the tomcat user cannot write).
Solution: check the startup log: tail -f /opt/tomcat/logs/catalina.out,
and verify ownership: sudo chown -R tomcat:tomcat /opt/tomcat.
Problem 4: remote clients cannot reach Tomcat although it works locally
Cause: firewall or the connector bound only to localhost.
Solution: open the port in your firewall (see
iptables rules), and check the connector address in
conf/server.xml.
Best Practices
- Always run Tomcat under its own unprivileged user, never root.
- Use
catalina.sh stopfor shutdown; kill -9 corrupts deployments. - Keep Java and Tomcat versions pinned and matched to your applications (Tomcat 9 → Java 8+).
- Check
/opt/tomcat/logs/catalina.outfirst when anything behaves oddly. - Run Tomcat as a systemd service so it survives reboots — wrap the catalina scripts in a
tomcat.serviceunit withUser=tomcat.
Conclusion
We installed the JDK, created an unprivileged tomcat user, downloaded and extracted Tomcat 9 into
/opt, started it with catalina.sh, and verified it answers on port 8080. The
structure — app in /opt/tomcat, service user, log in logs/ — is the same layout
you will see on production Tomcat boxes, so the skills transfer directly.