How to Install and Configure Jenkins on Ubuntu 20.04?

Install and Configure Jenkins on Ubuntu 20.04

If you're running Ubuntu 20.04 and want to install Jenkins easily, you're in the right place. In this guide, we'll walk you through the process of How to Install and Configure Jenkins on Ubuntu 20.04, step by step, along with commands and examples to make the journey smoother.

In the previous post, We have covered below topics, You can refer the link "Jenkins Introduction, Advantages, CI/CD Workflow Explained".

1. Introduction to Jenkins
2. Advantages of Jenkins
3. CI/CD Workflow Explained with Examples.

If you are interested in learning, Request you to go through the below recommended tutorial.

Steps to Install Jenkins on Ubuntu 20.04

Step 1: Update the System

The first step is to ensure your Ubuntu system is up to date. Open a terminal and run the following commands:
sudo apt-get update
This will update the package lists and upgrade the installed packages to their latest versions.

Step 2: Install Java

Jenkins requires Java to run. You can install OpenJDK, which is open-source and compatible with Jenkins:
sudo apt-get -y install openjdk-11-jdk
After the installation is complete, verify the Java version:
java -version
Output:
openjdk version "11.0.20.1" 2023-08-24
OpenJDK Runtime Environment (build 11.0.20.1+1-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.20.1+1-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Step 3: Add Jenkins Repository

You need to add the Jenkins repository to your system to install the latest version. Import the GPG key and add the Jenkins repository:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Step 4: Install Jenkins

Now, you can update the repositories to fetch latest packages:
sudo apt-get update
If you noticed below GPG error, We must add the key manually to make it work. If not, Ignore this step.
Reading package lists... Done
W: GPG error: https://pkg.jenkins.io/debian-stable binary/ Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 5BA31D57EF5975CA
E: The repository 'http://pkg.jenkins.io/debian-stable binary/ Release' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
Resolve the above GPG error using below command.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5BA31D57EF5975CA
Output:
Executing: /tmp/apt-key-gpghome.uLnySvtJYM/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 5BA31D57EF5975CA
gpg: key 5BA31D57EF5975CA: public key "Jenkins Project <jenkinsci-board@googlegroups.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1
Now try installing Jenkins using apt-get command.
sudo apt-get -y install jenkins

Step 5: Start and Enable Jenkins

Once Jenkins is installed, start the service and enable it to start on boot:
sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 6: Check Jenkins Status

To verify that Jenkins is running, use the following command:
sudo systemctl status jenkins
You should see a message indicating that Jenkins is active and running.

Step 7: Unlock Jenkins

Jenkins is initially locked, and you need to retrieve the initial admin password to unlock it. Run this command to get the password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
This command will display the password you need to unlock Jenkins. Copy it.

Step 8: Access Jenkins Web Interface

Open a web browser and enter your server's IP address followed by port 8080, like this: http://your_server_ip:8080.

You will be prompted to enter the initial admin password obtained in Step 7. Paste the password and click "Continue."

Step 9: Install Recommended Plugins

Select the "Install suggested plugins" option. Jenkins will begin downloading and installing the required plugins.

Step 10: Create an Admin User

After the plugins are installed, you'll be prompted to create an admin user. Fill in the required details.

Step 11: Start Using Jenkins

Once you've completed the setup, Jenkins is ready to use. You can start creating and managing your Jenkins jobs to automate your development workflows.

Additional Configurations on Jenkins:

Any Users created in jenkins console are only to manage jenkins through web browser. Bydefault, user 'jenkins' only runs everything in background even if you have loggedin using any users on portal and this user "jenkins" is created during the installation at the Operating system level, but it is a non-root user.
cat /etc/passwd | grep -i jenkins
Output:
jenkins:x:114:118:Jenkins,,,:/var/lib/jenkins:/bin/bash
So When user "jenkins" doesnt have sufficient previleges at the Operating system level, this user cannot perform all administrative level tasks. So we must configure sudo to gain the administrative level access.

SUDO Access:

Edit /etc/sudoers file and add below entry to allow user "jenkins" to use sudo with nopasswd prompt and also disable requiretty option.
jenkins ALL=(ALL) NOPASSWD: ALL
Defaults !requiretty

Change Jenkins user shell:

Verify the "jenkins" user shell. If it is already specified with "/bin/bash", You can ignore this. If not, we must change the user shell, this would help us to perform any tasks using CLI.
cat /etc/passwd | grep -i jenkins
Output:
jenkins:x:995:992:Jenkins Automation Server:/var/lib/jenkins:/bin/bash

Restart Service:

sudo systemctl restart jenkins

Post a Comment

0 Comments