DEV Community

Cover image for How to Setup Jenkins Locally With Oracle VirtualBox VM
Gabriel
Gabriel

Posted on • Updated on

How to Setup Jenkins Locally With Oracle VirtualBox VM

Creating The Virtual Box VM

Download and Install Oracle VM Virtual Box

oracle virtual box

Download Ubuntu 22.04 VirtualBox(VDI) Image from Osboxes.org

Note: Extract the content of the downlod downloaded file

Osboxes

Open Oracle VM Virtual Box application and create new VM

  • Name: vm-jenkins
  • Base: Memory 2048
  • CPU: 2
  • Use Existing Virtual Hard Disk File: Import the VDI file downloaded earlier

create vb vm

Start Up the VM and Login

  • Username: osboxes
  • Password: osboxes.org

Open Terminal and update system



sudo apt update


Enter fullscreen mode Exit fullscreen mode

Installing Jenkins on VM

This is the Debian package repository of Jenkins to automate installation and upgrade. To use this repository, first add the key to your system



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


Enter fullscreen mode Exit fullscreen mode

Then add a Jenkins apt repository entry:



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


Enter fullscreen mode Exit fullscreen mode

Update your local package index, then finally install Jenkins:



sudo apt update
sudo apt install fontconfig openjdk-17-jre -y
sudo apt install jenkins -y


Enter fullscreen mode Exit fullscreen mode

Start Jenkins by using systemctl:



sudo systemctl enable jenkins
sudo systemctl start jenkins


Enter fullscreen mode Exit fullscreen mode

Since systemctl doesn’t display status output, we’ll use the status command to verify that Jenkins started successfully:



sudo systemctl status jenkins


Enter fullscreen mode Exit fullscreen mode

If everything went well, the beginning of the status output shows that the service is active and configured to start at boot(enabled):

Output



● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2024-02-25 18:55:21 EST; 10min ago
   Main PID: 779 (java)
      Tasks: 46 (limit: 2292)
     Memory: 273.7M
        CPU: 21.993s
     CGroup: /system.slice/jenkins.service
             └─779 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpP>


Enter fullscreen mode Exit fullscreen mode

Now that Jenkins is up and running, let’s adjust our firewall rules so that we can reach it from a web browser to complete the initial setup.

Opening the firewall

By default, Jenkins runs on port 8080. We’ll open that port using ufw:



sudo ufw allow 8080


Enter fullscreen mode Exit fullscreen mode

Note: If the firewall is inactive, the following commands will allow OpenSSH and enable the firewall:



sudo ufw allow OpenSSH
sudo ufw enable


Enter fullscreen mode Exit fullscreen mode

Check ufw’s status to confirm the new rules:



sudo ufw status


Enter fullscreen mode Exit fullscreen mode

You’ll notice that traffic is allowed to port 8080 from anywhere:



Status: active
To                         Action      From
--                         ------      ----
8080                       ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
8080 (v6)                  ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)


Enter fullscreen mode Exit fullscreen mode

Now we will look at how to reach the jenkins vm on the host machine by setting port forwarding.

Setting Up Port Forwarding

  1. Open VirtualBox VM settings.
  2. Navigate to the NAT network adapter settings.
  3. Click Advanced and then Port Forwarding.
  4. Add a new rule named "jenkins connection".
  5. Set Protocol to TCP.
  6. Enter Host IP as 127.0.0.1 and Host Port as 8080.
  7. Guest Port should also be set to 8080.
  8. Click OK to save the settings.

Setting Up Port Forwarding

Setting Up Jenkins

Open your browser and go to 127.0.0.1:8080 to access Jenkins, you will land on the getting started jenkins page:
Setting Up Jenkins

Use the terminal in your VM and run



sudo cat /var/lib/jenkins/secrets/initialAdminPassword


Enter fullscreen mode Exit fullscreen mode

Copy the 32-character alphanumeric password from the terminal and paste it into the Administrator password field on the Jenkins page. Then, click Continue.
Click on Install suggested plugins
Create your admin user
Set http://127.0.0.1:8080 as the jenkins URL
Click Save and Finish

Jenkins

THE END

Top comments (0)