How to Install SonarQube on CentOS 8

n this article, We are going to perform, How to Install SonarQube on CentOS 8, Install and Setup PostgreSQL 14 Database For SonarQube,Configure SonarQube.

Introduction

SonarQube is an opensource web based tool to manage code quality and code analysis. It is most widely used in continuous code inspection which performs reviews of code to detect bugs, code smells and vulnerability issues of programming languages.

Prerequisites

  • CentOS 8 with minimum 2GB RAM and 1 CPU.
  • PostgreSQL Version 9.3 or higher
  • SSH access with sudo privileges
  • Firewall Port: 9000

#1:Update and install required tools

In this step, ensure that your server is well updated as well as install all tools you will require during the installation process.

Step 1: Update your system packages:

sudo yum update

Step 2: Install tools

sudo yum install vim wget curl -y
sudo yum install wget unzip -y

Step 3: To Increase the vm.max_map_count kernal ,file discriptor and ulimit permanently .

Open the below config file and Insert the below value as shown below,

sudo vim /etc/security/limits.conf          
sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

#2:Create user for SonarQube

Step 1: Create user for sonar

sudo useradd sonar

Step 2: Set password for the user:

sudo passwd sonar

#3:Install Java on centos 8

Step 1: Install java using below command:

sudo yum install java-11-openjdk-devel

Step 2: Set default JDK

To set default JDK or switch to OpenJDK enter below command,

sudo update-alternatives --config java

Type  1 to switch OpenJDK 11.

Step 3: To check Java version

java –version

#4:Install and Setup PostgreSQL 14 Database For SonarQube

Step 1: Update System Package:

sudo yum install -y

Step 2: Install the PostgreSQL Repository

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Once the repository has been added disable default PostgreSQL module

sudo dnf -qy module disable postgresql

Step 3: Install the PostgreSQL 14 database Server by using following command:

sudo dnf -y install postgresql14 postgresql14-server

Step 4: Initialize the Postgres database.

sudo /usr/pgsql-14/bin/postgresql-14-setup initdb

Step 5: Start PostgreSQL Database server

sudo systemctl start postgresql-14

Step 6: Enable it to start automatically at System Startup

sudo systemctl start postgresql-14

Step 7: Change the password for the default PostgreSQL user.

sudo passwd postgres

Step 8: Switch to the postgres user.

su -- postgres

Step 9: Create a new user by typing:

createuser sonar

Step 10 : Switch to the PostgreSQL shell.

psql

Step 11: Set a password for the newly created user for SonarQube database.

ALTER USER sonar WITH ENCRYPTED password 'sonar';

Step 12: Create a new database for PostgreSQL database by running:

CREATE DATABASE sonarqube OWNER sonar;

Step 13: grant all privileges to sonar user on sonarqube Database.

grant all privileges on sonarqube  to sonar;

Step 14: Exit from the psql shell:

\q

Step 15: Switch back to the sudo user by running the exit command.

exit

#5:Download and Install SonarQube on CentOS 8

Step 1: Download sonaqube installer files archive To download latest version of visit SonarQube download page.

cd /tmp
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.1.0.47736.zip

Step 2:Unzip the archive setup to /opt directory

sudo unzip sonarqube-9.1.0.47736.zip -d /opt

Step 3: Move extracted setup to /opt/sonarqube directory

sudo mv /opt/sonarqube-9.1.0.47736 /opt/sonarqube

#6:Configure SonarQube

Step 1: Create a group as sonar

sudo groupadd sonar

Step 2: Now add the user with directory access.

sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonar sonar

Step 3: Give the ownership permission to sonar user and group.

sudo chown -R sonar:sonar /opt/sonarqube

Step 4: Open the SonarQube configuration file using your favorite text editor.

sudo vim /opt/sonarqube/conf/sonar.properties Find the following lines.
#sonar.jdbc.username=
#sonar.jdbc.password=

Uncomment and Type the PostgreSQL Database username and password which we have created in above steps.

sonar.jdbc.username=your_user
sonar.jdbc.password=your_password

Step 5: Edit the sonar script file and set RUN_AS_USER

sudo vim /opt/sonarqube/bin/linux-x86-64/sonar.sh
RUN_AS_USER=sonar

#7:Start SonarQube

Step 1: Now to start SonarQube we need to do following: Switch to sonar user

sudo su sonar

Step 2: Move to the script directory

cd /opt/sonarqube/bin/linux-x86-64/

Step 3: Run the script to start SonarQube

./sonar.sh start

Step 4: To check if sonaqube is running enter below command,

./sonar.sh status

#8:SonarQube logs:

To check sonarqube logs, navigate  to /opt/sonarqube/logs/sonar.log directory

sudo mv /opt/sonarqube/logs/sonar.20220127.log /opt/sonarqube/logs/sonar.log
tail /opt/sonarqube/logs/sonar.log

#9:Configure Systemd service

Step 1: Create a systemd service file for SonarQube to run as System Startup.

sudo nano /etc/systemd/system/sonar.service

add below lines

[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonar
Group=sonar
Restart=always
LimitNOFILE=65536
LimitNPROC=4096
[Install]
WantedBy=multi-user.target

Save and close the file. Now stop the sonarqube script earlier we started to run using as daemon. Start the Sonarqube daemon by running:

sudo systemctl start sonar

Enable the SonarQube service to automatically  at boot time System Startup.

sudo systemctl enable sonar

check if the sonarqube service is running,

sudo systemctl status sonar

We have covered How to Install SonarQube on CentOS 8.

#10:Access SonarQube

To access the SonarQube using browser type server IP followed by port 9000.

http://server_IP:9000 OR http://localhost:9000

Login to SonarQube  with default administrator username and password is admin.

Finally, We have successfully performed all steps to install sonarqube setup.

Conclusion:

In this article, We have performed ,How to Download and Install SonarQube on CentOS 8 with Configure Sonarqube, Creating Systemd for SonarQube Service.

Related Articles:

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

3 thoughts on “How to Install SonarQube on CentOS 8”

  1. This was a great help and thank you for putting it out for us newbies.

    The only change I had to make was replacing the command
    grant all privileges on sonarqube to sonar;
    with
    grant all privileges on database sonarqube to user sonar;

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap