It’s been very simple to run a static code analysis tool on any project developed in any language. Here I am targeting a PHP project and SonarQube Installation on Ubuntu OS.
This is a step by step guide to install and run SonarQube on Ubuntu.
Prerequisite:
- Oracle JRE 8 onwards or OpenJDK 8 onwards
sudo apt-get install oracle-java8-installer |
- Database
- MS Sql Server :
- 2012 (MSSQL Server 11.0) with bundled Microsoft JDBC driver.
- 2014 (MSSQL Server 12.0) with bundled Microsoft JDBC driver.
- MySql :
- 5.6
- 5.7
- Note : Only innoDB is supported not MyIsam
- Oracle :
- 11G with Oracle 11.2.x drivers
- 12C with Oracle 11.2.x drivers
- MS Sql Server :
- Hardware Requirements
- The SonarQube server requires at least 2GB of RAM to run efficiently and 1GB of free RAM for the OS.
Installation:
- Download and unzip the SonarQube distribution (let’s say in “C:\sonarqube” or “/opt/sonarqube”)
- Start the SonarQube server:
- On other operating system,Open the console and execute:
bash /opt/sonarqube/bin/[OS]/sonar.sh console |
Note : The above command will not register sonar as service. So once the user will press ^C , process will be terminated.
- To register sonar as a service you will first need to setup the sonar server.
Creating Mysql database
- Install the mysql database
- Open the command prompt to login mysql database using following command
Mysql -u <username> -p<password> |
- Create a new database to store sonar results.
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER ‘sonar’ IDENTIFIED BY ‘sonar’; GRANT ALL ON sonar.* TO ‘sonar’@’%’ IDENTIFIED BY ‘sonar’; GRANT ALL ON sonar.* TO ‘sonar’@’localhost’ IDENTIFIED BY ‘sonar’; FLUSH PRIVILEGES; |
Editing Properties file
- Edit the ‘conf/sonar.properties’ file and remove the # against the following line to use embedded Mysql server.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance |
- Uncomment and use mysql credentials for below lines
sonar.jdbc.username=sonar
sonar.jdbc.password=password |
Register Sonarqube as service
Copy ‘sonar.sh’ to ‘etc/init.d/sonar’ and modify it according to your platform.
sudo cp /opt/sonar/bin/linux-x86-64/sonar.sh /etc/init.d/sonar
sudo gedit /etc/init.d/sonar |
Insert two new lines:
SONAR_HOME=/opt/sonarqube
PLATFORM=linux-x86-64 |
Modify the following lines:
WRAPPER_CMD=”${SONAR_HOME}/bin/${PLATFORM}/wrapper”
WRAPPER_CONF=”${SONAR_HOME}/conf/wrapper.conf” … PIDDIR=”/var/run” Register as a Linux service: sudo update-rc.d -f sonar remove sudo chmod 755 /etc/init.d/sonar sudo update-rc.d sonar defaults |
Start SonarQube server either by typing the direct command:
sudo /opt/sonar/bin/linux-x86-64/sonar.sh start |
or by typing the service command:
sudo /etc/init.d/sonar start |
Visit SonarQube web page at http://localhost:9000/sonar
Stop SonarQube server either by typing the direct command:
sudo /opt/sonar/bin/linux-x86-64/sonar.sh stop |
or by typing the service command:
sudo /etc/init.d/sonar stop |
After Sonarqube started
SonarQube will start creating database tables. Then web process will up.
Installing Sonar-Scanner to run the analysis
- Download the sonar-scanner from the link or wget command using command prompt.
- Unzip the zip file and place the sonar-scanner folder into /opt
- Use command ‘sudo nano /opt/sonar-scanner/conf/sonar-scanner.properties’ to edit the configuration file.
- Un comment sonar.host.url and update the URL of sonarqube as value i.e., sonar.host.url=http://<url>:9000/sonar/
- Create a ‘sonar-project.properties’ file into root folder of project using following code
# must be unique in a given SonarQube instance
sonar.projectKey=<project key given in sonar portal while adding project into it> # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=<Project name> sonar.projectVersion=<Project version (this is important because after every analysis sonarqube compares with previous verion)> # 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=. sonar.exclusions=**/*.js, **/stats.php # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8 |
- Now user need to go to root folder of project and run following command to run the code analysis
sudo bash /opt/sonar-scanner/bin/sonar-scanner |
Cheers!