Install and Configure Jenkins

Installing and Configuring Jenkins on Ubuntu

Prerequisites

Before you start, make sure you have the following:

  • A server running Ubuntu 22.04 or later (I'm using Ubuntu 22.04 on an EC2 t2.medium instance)
  • sudo access to the server
  • A web browser

Step 1: Update the package list

First, update the package list by running the following command:

sudo apt update

Step 2: Install Java

Next, install the latest version of OpenJDK 17 by running the following command:

sudo apt install openjdk-17-jre-headless

Step 3: Add Jenkins repository key to your system

Add the Jenkins repository key to your system by running the following commands:

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo gpg --dearmor --yes -o /usr/share/keyrings/jenkins-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.gpg] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null

This will add the Jenkins repository key to the APT keyring and add the Jenkins repository to the list of package sources.

Step 4: Install Jenkins

Install Jenkins by running the following commands:

sudo apt update
sudo apt install jenkins

Step 5: Start Jenkins

Start Jenkins by running the following command:

sudo systemctl start jenkins

Step 6: Check Jenkins status

Check the status of Jenkins by running the following command:

sudo systemctl status jenkins

Make a note of the initial password in the output. You can also find the initial password in the file /var/lib/jenkins/secrets/initialAdminPassword.

Step 7: Enable firewall

Enable the firewall by running the following commands:

sudo ufw allow OpenSSH
sudo ufw enable

Step 8: Open port 8080

Open port 8080 by running the following command:

sudo ufw allow 8080

Step 9: Check firewall status

Check the status of the firewall by running the following command:

sudo ufw status

Step 10: Reboot the server

Reboot the server by running the following command:

sudo reboot

Step 11: Test Jenkins installation

After rebooting the server, test the Jenkins installation by visiting http://<your_server_ip_or_domain>:8080 in your web browser. The domain is the Public IPv4 DNS from the EC2 console.

Step 12: Complete Jenkins setup

Enter the initial password from step 6 and click "Continue". Then follow the prompts to complete the Jenkins setup process.

Step 13: Change the admin password

After completing the setup process, log in to Jenkins using the admin account and the initial password. Then, change the admin password to a strong, secure password.

Step 14: Add a worker node

To add a worker node, go to "Manage Jenkins" > "Manage Nodes and Clouds" in the Jenkins web interface. Then click "New Node" and follow the prompts to set up the node. Be sure to set the directory to /var/jenkins or another appropriate location.

Step 15: Setup swap space

Set up swap space by running the following commands:

sudo swapon --show
free -h
df -h
sudo f
sudo fallocate -l 5G /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
free -h
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 16: Start the worker node

To start the worker node, go to "Manage Jenkins" > "Manage Nodes and Clouds" in the Jenkins web interface. Then click on the worker node and copy the agent run command. For example:

java -jar agent.jar -jnlpUrl http://<your_server_ip_or_domain>:8080/manage/computer/Worker%20Node/jenkins-agent.jnlp -secret <secret-from-worker-ui> -workDir "/var/jenkins" &

Customize the command as needed for your specific setup, such as setting environment variables or adding labels.

Congratulations! You have now installed and configured Jenkins on your Ubuntu server.