SonarQube is Code Quality testing solution which lets you analyse the quality of your code, detect bugs and much more to improve overall health of your code.
SonarQube comes in 2 variants. It can be accessed online using the URL https://sonarqube.com/ and it can also be hosted on your own server. In this tutorial, I am demonstrating how you can install & setup SonarQube on your own Ubuntu server to check your code’s quality 🙂
Lab Description : –
Ubuntu 14.04 64 bit server with 2 GB RAM.
MySQL version 5.6.33 with InnoDB storage engine.
SonarQube version 6.2.
My PHP project located at DocumentRoot. You can choose any location for code analysis.
Please note that SonarQube needs atleast 2 GB of RAM, so please make sure you have enough of it.
Steps to be followed : –
SonarQube by default uses its internal H2 database but we will be using MySQL for this. Choose any database of your choice.
1. Download SonarQube & SonarQube Scanner.
Use the links provided to download both the products.
2. Unpack them.
Unpack both of them to any location where you can locate them easily. I chose my home-directory for it. It will create 2 directories sonarqube-6.2 & sonar-scanner-2.8.
root@shashank-dbserver:/home/shashank# unzip Downloads/sonarqube-6.2.zip root@shashank-dbserver:/home/shashank# unzip Downloads/sonar-scanner-2.8.zip
It will be good if you create aliases for above 2 directories or add them to your PATH.
3. Create MySQL Database & User.
Create a new database called sonar in MySQL (or any other DBMS of your choice). Then create a user sonarqube & grant it all privileges for sonar database.
mysql> create database sonar; Query OK, 1 row affected (0,01 sec) mysql> use sonar; Database changed mysql> CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'sonarqube'; Query OK, 0 rows affected (0,02 sec) mysql> GRANT ALL PRIVILEGES ON sonar.* to 'sonarqube'@'localhost'; Query OK, 0 rows affected (0,00 sec) mysql> flush privileges; Query OK, 0 rows affected (0,00 sec)
4. Edit sonar.properties file.
Edit this file inside
sonarqube-6.2/conf directory to enter database details. Make sure to put user-name & password you created in last step. Below is the snippet. Edit values accordingly.
# DATABASE # # IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for # production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer. # User credentials. # Permissions to create tables, indices and triggers must be granted to JDBC user. # The schema must be created first. sonar.jdbc.username=sonarqube sonar.jdbc.password=sonarqube #----- Embedded Database (default) # H2 embedded database server listening port, defaults to 9092 #sonar.embeddedDatabase.port=9092 #----- MySQL 5.6 or greater # Only InnoDB storage engine is supported (not myISAM). # Only the bundled driver is supported. It can not be changed. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
5. Create & Edit sonar-project.properties file.
Create this file inside your code’s project & enter values accordingly. See below snippet. Give your project a unique Project Key for SonarQube to uniquely identify it.
# must be unique in a given SonarQube instance sonar.projectKey=exclaimadeasy # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=ExClaiMadEasy sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # Since SonarQube 4.2, this property is optional if sonar.modules is set. # If not set, SonarQube starts looking for source code from the directory containing # the sonar-project.properties file. sonar.sources=. # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8
6. Start SonarQube.
Start it by executing
sonarqube-6.2/bin/sonar.sh start command.
root@shashank-dbserver:/home/shashank/sonarqube-6.2/bin/linux-x86-64# ./sonar.sh start Starting SonarQube... Started SonarQube.
7. Access SonarQube via browser.
Open your browser & enter localhost:9000. Then click login at the top-right corner. Credentials are admin/admin.
8. Start SonarQube Scanner to analyse your code.
Execute below command to start SonarQube scanner from within your project directory.
It will start scanning your project’s code. Once its done scanning, you will see output similar to below. Click the link provided there to see your report.
INFO: Analysis report uploaded in 240ms INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/exclaimadeasy INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AVqskPfd6DjWymbXBiOQ INFO: Task total time: 18.806 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------
Video Tutorial : –
I have also posted a video on my YouTube channel to demonstrate all the steps. You can watch it below.
Caveats : –
Video shown above only demonstrates the basic code analysis, even though my project is PHP based. For PHP projects (or any other non-default languages), please download the plugin(s) and place that in
SonarQube_HOME/extensions/plugins directory. After that restart SonarQube by executing
sonar.sh restart command. PHP plugin can be downloaded from https://docs.sonarqube.org/display/PLUG/SonarPHP
Also, in the video above, I missed to uncomment MySQL jdbc conncection URL but same can be seen uncommented in snippet I pasted in step 3 😉
I hope you liked this post. See you later 🙂