Installing Tomcat on Ubuntu

Tomcat is a popular web server for running Java applications. With Runbooks, you can create a runbook as part of a routine operations task to install Tomcat on your deployment targets.

Create the runbook

To create a runbook to install Tomcat on an Ubuntu machine:

  1. From your project’s overview page, navigate to Operations ➜ Runbooks, and click ADD RUNBOOK.
  2. Give the runbook a Name and click SAVE.
  3. Click DEFINE YOUR RUNBOOK PROCESS, and then click ADD STEP.
  4. Click Script, and then select the Run a Script step.
  5. Give the step a name.
  6. Choose the Execution Location on which to run this step.
  7. In the Inline source code section, select Bash and add the following code:

if [[ -f $TOMCAT_INSTALL_STARTUP ]]; then
  echo "Tomcat already installed."



sudo apt-get update
echo "Installing Java..."
sudo apt install default-jdk -y

echo "Installing jq..."
sudo apt install jq -y

LATEST_TOMCAT=$(curl -s '' | jq -r .[].name | grep -v '-' | head -1)

echo "Creating tomcat group ..."
sudo groupadd $TOMCAT_GROUP -r

if [[ ! -d /opt/tomcat ]]; then
echo "Making tomcat folder"
sudo mkdir /opt/tomcat

echo "Creating tomcat Linux user ..."
sudo useradd -r -m -d /opt/tomcat -s /bin/false -g $TOMCAT_GROUP $TOMCAT_USER

echo "Downloading Tomcat version $LATEST_TOMCAT..."
wget$LATEST_TOMCAT/bin/apache-tomcat-$LATEST_TOMCAT.tar.gz -P /tmp

echo "Extracting Tomcat..."
sudo tar xf /tmp/apache-tomcat-$LATEST_TOMCAT.tar.gz -C /opt/tomcat

echo "Creating symbolic link..."
sudo ln -s /opt/tomcat/apache-tomcat-$LATEST_TOMCAT /opt/tomcat/latest
sudo chown -RH $TOMCAT_USER: /opt/tomcat/latest
sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

echo "Creating Tomcat service file..."
cat >> tomcat.service <<EOL
Description=Tomcat 9 servlet container



Environment=" -Djava.awt.headless=true"

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"



sudo mv tomcat.service /etc/systemd/system/tomcat.service
echo "Adding management user to tomcat-users.xml..."

# Add management user
sudo cat > /opt/tomcat/latest/conf/tomcat-users.xml <<EOF
<tomcat-users xmlns=""
  xsi:schemaLocation=" tomcat-users.xsd"
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<user username="${TOMCAT_ADMIN_USER}" password="${TOMCAT_ADMIN_PASSWORD}" roles="tomcat,manager-script,manager-gui"/>

echo "Starting Tomcat..."
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat

echo "Altering firewall rules..."
sudo ufw allow 8080/tcp


echo "Process complete"

The script checks to see if Tomcat is already installed by looking to see if the Tomcat script exists. If the file is present it will skip the install. If it isn’t installed, then the script will:

  • Install Java.
  • Create a local user to run tomcat.
  • Download and install the latest version of Apache Tomcat.
  • Create a Tomcat systemd service file.
  • Add a Tomcat management user.
  • Start the Tomcat service.
  • Open port 8080 on the firewall.

Add the variables

The script expects the following variables to be created:

Variable NameVariable TypeDescriptionExample
Runbook.InstallTomcat.Tomcat.UserTextUser to run Tomcat servicetomcat
Runbook.InstallTomcat.Tomcat.GroupTextTomcat grouptomcat
Runbook.InstallTomcat.Tomcat.AdminUserTextTomcat admin usertomcatadmin
Runbook.InstallTomcat.Tomcat.AdminPasswordSensitiveTomcat admin user passwordSuperSecretPa$$word

The variables can be created by navigating to Project ➜ Variables and adding them there.


We have a Pattern - Rolling Space on our Samples instance of Octopus. You can sign in as Guest to take a look at this example and more Runbooks in the PetClinic Infrastructure project.

Learn more

Help us continuously improve

Please let us know if you have any feedback about this page.

Send feedback

Page updated on Sunday, January 1, 2023