Skip to main content

How To Install SonarQube On CentOS Servers

What is SonarQube?

SonarQube is an open source tool for source code quality management, code analysis etc. It is the most widely used tool for code coverage and analysis. We can also integrate it easily with Jenkins. So, in this article, we will install SonarQube on CentOS 7.x.

How To Install SonarQube On CentOS

Important: SonarQube requires at least 2GB of RAM to run efficiently. Please check the SonarQube official documentation to know the detailed prerequisites.

Step 1: Update your CentOS system

First, update the system packages by running “yum update” and reboot the server.

yum update
shutdown -r now

Step 2: Install the necessary packages

yum install wget unzip -y

Step 3:  Install Java

Now, install Java and set the environment variables.

yum install java-1.8.0-openjdk-devel.x86_64

You can check the java version by running the following command:

java -version

Next, we need to set up the Java environment variables.

cp /etc/profile /etc/profile_orig
echo -e "export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk" >> /etc/profile
echo -e "export JRE_HOME=/usr/lib/jvm/jre" >> /etc/profile
source /etc/profile

Now, verify that the Java environment variables are set up properly by running the following command:

echo -e "$JAVA_HOME\n$JRE_HOME"

Step 3: Install and configure MySQL Database

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server -y

Start MySQL service

systemctl start mysqld

Configure MySQL by running mysql_secure_installation.

mysql_secure_installation

Next, create a MySQL user and Database for SonarQube via MySQL CLI.

CREATE DATABASE sonarqube_db;
CREATE USER 'sonarqube_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON sonarqube_db.* TO 'sonarqube_user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Replace the “sonarqube_db“,”sonarqube_user” and “password” mentioned in the above command with your database name, database user and password respectively.

Step 4: Create a new user for running SonarQube

Create a new user for running SonarQube. This is because you cannot run the newer versions of elasticsearch as the root user.

useradd sonarqube

5.Download and install SonarQube

Download the latest version of SonarQube from here.

wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.5.zip

Extract it using the following command.

unzip sonarqube-6.7.5.zip
mv sonarqube-6.7.5 /opt/sonarqube
Since we are running SonarQube as a separate user, assign proper ownerships to SonarQube files.
chown sonarqube. /opt/sonarqube -R
Next, open the SonarQube configuration file “conf/sonar.properties” on your favorite text editor.
vi /opt/sonarqube/conf/sonar.properties
Enter the database details as shown below.
sonar.jdbc.username=sonarqube_user
sonar.jdbc.password=password
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube_db?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
Open the SonarQube startup script and specify the sonarqube user details.
vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
Add the following entry to it.
RUN_AS_USER=sonarqube

6. Starting SonarQube and Setting it as a Systemd service

You can now start SonarQube by running the following command.
/opt/sonar/bin/linux-x86-64/sonar.sh start
If you have done everything correctly, SonarQube will start listening on ports 9000. You can access it using
http://your_ip_address:9000
For setting SonarQube as a Systemd Service, create a new file “sonar.service” under “/etc/systemd/system/”.
vi /etc/systemd/system/sonar.service
Now, copy the below lines into it.
[Unit]
Description=SonarQube
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=sonarqube
Group=sonarqube
Restart=always

[Install]
WantedBy=multi-user.target
Now, enable SonarQube service to automatically start at boot time by running the following command.
systemctl enable sonar.service

Start SonarQube by running

systemctl start sonar.service

And stop SonarQube by running

systemctl stop sonar.service

The default username and password of SonarQube is admin and admin

So, in this tutorial, we have covered how to install and setup SonarQube on a CentOS server. We will discuss how to integrate it with Jenkins on another post. If you face any issues during the setup, please feel free to leave a comment below.

How To Install SonarQube On CentOS Servers
5 (100%) 4 votes

GeeksAlive

TheGeeksAlive is all about Linux, Server Administration, Web Hosting and other Open Source Technologies.