1. Overview

Jenkins is a free and open-source Java-based web application. It is a general-purpose automation server that is commonly used for task automation, such as GitOps and CI/CD. Due to its popularity, the Jenkins project distributes its release in a variety of formats such as Linux packages, WAR files, and Docker images. Moreover, these different distribution formats offer us different options to install and run the software.

In this tutorial, we’ll be looking at the different ways we can install and run Jenkins in Linux.

2. Using Package Manager

The Jenkins project maintains packages in most of the famous Linux distributions, such as Debian and Redhat. For the complete list of available Jenkins packages in other Linux package managers, we can refer to the official Jenkins installation guide.

Generally, the steps to install Jenkins with package managers are to add the repository, perform an update, and then install it. For the rest of this section, we’ll be demonstrating the steps using the apt package manager, but the steps are applicable for other supported Linux distributions as well.

2.1. Adding the Jenkins Repository

Before we can install Jenkins through the package manager, we’ll need to add the Jenkins Debian repository. Firstly, we’ll download the official PGP key from the Jenkins server using wget and save it to /usr/share/keyrings/jenkins-keyring.asc:

$ sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian/jenkins.io.key

Then, we add the Jenkins repository into the list:

$ echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee  /etc/apt/sources.list.d/jenkins.list > /dev/null

Finally, we run the apt-get update command to update our local package index:

$ sudo apt-get update -qq

2.2. Installing and Running Jenkins

To ensure we can run Jenkins, we’ll first need to install the Java Runtime Environment (JRE). Then, we can proceed to install Jenkins using apt-get install:

$ sudo apt-get install -y jenkins

Once the installation is completed, Jenkins should be started in the background listening on port 8080. Browsing to http://localhost:8080 and we should see the Jenkins getting started page. Furthermore, the installation script will also add the Jenkins into systemd as a service. To check its status, we can run the systemctl status jenkins command:

$ systemctl status jenkins
● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-03-12 09:17:29 CET; 21min ago
   Main PID: 5171 (java)
--TRUNCATED--

3. Installing and Running Jenkins with a WAR File

In addition to the Linux packages, the Jenkins project also distributes its software in the generic Web Application Resource (WAR) file format.

Usually, we’ll need a dedicated application server such as Tomcat and WildFly to serve a WAR file. However, the Jenkins WAR file also comes with the embedded Jetty server. This means that we can either deploy our WAR file on a dedicated application server such as Tomcat or as a standalone Java process.

Regardless of which approach we use for running Jenkins, we’ll need to first install the Java Runtime Environment (JRE) in our current system.

3.1. Downloading the Jenkins WAR File

To download the WAR file, we can go to its official distribution site and select the “Generic java package (.war)” option. Clicking the option should start the download immediately.

Alternatively, we can download the WAR file on the terminal using wget:

$ wget -O jenkins.war https://get.jenkins.io/war/{version}/jenkins.war

where {version} should be replaced with the version we want to download, such as version 2.338.

3.2. Running Jenkins Using an Embedded Jetty Server

To run the WAR file using its embedded Jetty server, we can use the java -jar command:

$ java -jar jenkins.war

Once we execute the command, we’ll see a stream of logs in the terminal. At this point, the Jenkins application would now be accessible through localhost at port 8080.

3.3. Deploying Jenkins on Tomcat Server

Alternatively, we can deploy the WAR file to a dedicated application server such as Tomcat, Websphere, and WildFly. For the demonstration purpose of this article, we’ll set up a Tomcat server to serve the WAR file.

Firstly, we download the Tomcat binary using its official download links with wget:

$ wget -O tomcat.tar.gz https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.59/bin/apache-tomcat-9.0.59.tar.gz

Then, we untar the Tomcat files into the tomcat directory using the tar command along with the –strip-component option to remove the parent folder:

$ mkdir tomcat && tar -xvzf tomcat.tar.gz -C tomcat --strip-component=1

Inside the bin folder of the Tomcat archive we’ve extracted, we’ll find startup.sh. We can run it to start the Tomcat service and expose it on port 8080:

$ tomcat/bin/startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

To deploy the WAR file, we copy the jenkins.war file into the *tomcat/*webapp folder:

$ cp jenkins.war ./tomcat/webapps
$ ls -halt ./tomcat/webapps
total 88M
drwxr-x--- 11 user user 4.0K Mar 14 13:46 jenkins
drwxr-x---  8 user user 4.0K Mar 14 13:46 .
-rw-rw-r--  1 user user  87M Mar 14 13:46 jenkins.war
drwxr-x---  6 user user 4.0K Mar 14 13:40 manager
drwxr-x---  6 user user 4.0K Mar 14 13:40 host-manager
drwxr-x---  7 user user 4.0K Mar 14 13:40 examples
drwxr-x--- 15 user user 4.0K Mar 14 13:40 docs
drwxr-x---  3 user user 4.0K Mar 14 13:40 ROOT
drwxrwxr-x  9 user user 4.0K Mar 14 13:40 ..

Running ls on the webapps folder after we copy the jenkins.war file, we’ll discover an unarchived jenkins folder in the same directory. This is because Tomcat automatically detects and unarchives WAR files in that directory.

Then, it serves the content of the unarchived WAR file on the context path matching the directory name. In our case, the Jenkins application would now be accessible on http://localhost:8080/jenkins.

4. Running Jenkins as a Docker Container

In addition to the WAR file and Linux packages, the Jenkins project also releases the Jenkins application in the form of a Docker image. Concretely, they release the Jenkins Docker image under the jenkins/jenkins image name. Contrary to the previous approaches, we don’t need to install JRE in our system because the JRE is packaged within the image.

It goes without saying that we’ll need to first install the docker application in order to run any Docker containers. Unfortunately, the installation of docker is out of the scope of this article. However, we can refer to the official installation guide in order to install the docker application on our local.

To run Jenkins as a Docker container, it is as simple as running the docker run command follows by the image path jenkins/jenkins:

$ docker run --name jenkins -p 8080:8080 -d jenkins/jenkins 

We specify the -p option in order to expose and map the container port 8080 to port 8080 on our local interface. By doing that, we’ll be able to access the Jenkins on port 8080 of localhost.

One thing to note about Docker containers is that they are ephemeral and do not persist data by default. In order to persist any data on the Jenkins, we’ll need to mount a local folder to the /var/jenkins_home directory:

docker run --name jenkins  -p 8080:8080 -d -v /opt/data/jenkins:/var/jenkins_home jenkins/jenkins

The command above includes the -v option, which mounts the local path /opt/data/jenkins to the /var/jenkins_home directory inside of the Jenkins container, where all the data lives.

5. Summary

In this tutorial, we’ve looked at the different ways we can run Jenkins on our Linux server.

We first demonstrated using the built-in package manager to install and run Jenkins. Then, we explored the more manual way of installing and running the Jenkins using its WAR file. Additionally, we’ve seen how we can either run the WAR file using the Tomcat server or as a standalone Java process. Finally, we’ve seen how we can run Jenkins as a Docker container using a few docker commands.