SonarQube is an Open-Source, Java based Code Analysis Tool. It is used to detect bugs, security vulnerabilities, and other code quality issues. It uses database like MS SQL, Oracle or PostgreSQ for storing analysis results. So, in this tutorial we will use open-source database PostgreSQL.

Pre-requistes

  • Instance with at least 2 GB RAM
  • Install Java

    To configure the SonarQube server, you need to install Java. To install Java, follow the command below:
    1
    sudo apt-get update && sudo apt-get install default-jdk -y

    After installing Java, you can verify that it is installed by running the following command:

    1
    java -version

    Install PostgreSQL

    To install PostgreSQL, follow the instructions below:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

    sudo apt-get update

    sudo apt-get -y install postgresql postgresql-contrib

    sudo systemctl start postgresql

    sudo systemctl enable postgresql

    Login as postgres user

    To login as postgres user, run the following command:
    1
    sudo su - postgres

    Create sonar user

    To create a soanr user, run the following command:
    1
    createuser sonar

    Create sonar database

    Switch to sql shell by entering the following command:
    1
    psql

    Then you need to Execute the below three lines (one by one) to create a sonar database and grant access to sonar user:

    1
    2
    3
    4
    5
    ALTER USER sonar WITH ENCRYPTED password '<your password>';

    CREATE DATABASE sonarqube OWNER sonar;

    GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;

    Then exit from the sql shell by entering the following command:

    1
    \q

    Then exit from the postgres user by entering the following command:

    1
    exit

    Download and Install SonarQube

    To download SonaQube, follow the instructions below:
    1
    sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-<version>.zip

    You can find any available version of SonarQube by following this link

    Then you need to unzip the downloaded file:

    1
    2
    3
    4
    5
    sudo apt-get -y install unzip

    sudo unzip sonarqube*.zip -d /opt

    sudo mv /opt/sonarqube-<version> /opt/sonarqube -v

    Create Group and User

    Now me need to create a group and a user. To create a group, run the following command:
    1
    sudo groupadd sonarGroup

    Now add the user with directory access. To do it run following commands:

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

    Then change the ownership of the directory:

    1
    sudo chown sonar:sonarGroup /opt/sonarqube -R

    Configure SonarQube

    Now we need to modify sonar.properties file and add the database user and it's password. To do it run following commands:
    1
    sudo vim /opt/sonarqube/conf/sonar.properties

    Then uncomment the following lines and add values for them

    1
    2
    sonar.jdbc.username=sonar
    sonar.jdbc.password=<password that you have created in create sonar database step>

    Next, we need to add the below lines to the sonar.properties file:

    1
    sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube

    Then press ESC and and enter :wq! to come out of the above screen

    Edit the sonar script file

    Run below command:
    1
    sudo vim /opt/sonarqube/bin/linux-x86-64/sonar.sh

    Then add the below lines to the sonar.sh file:

    1
    RUN_AS_USER=sonar

    Create Sonar as a service

    Purpose of this step is allow sonarqube to start automatically when you restart the server. To do it run following commands:
    1
    sudo vim /etc/systemd/system/sonar.service

    Tyhen add the following lines to the sonar.service file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    [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
    LimitNOFILE=131072
    LimitNPROC=8192
    User=sonar
    Group=sonarGroup
    Restart=always

    [Install]
    WantedBy=multi-user.target

    Then press ESC and and enter :wq! to come out of the above screen.

    Kernel System changes

    Now we need to change the kernel system settings. To do it run following commands:
    1
    sudo vim /etc/sysctl.conf

    Then add the following lines to the sysctl.conf file:

    1
    2
    vm.max_map_count=262144
    fs.file-max=65536

    Then press ESC and and enter :wq to come out of the above screen.

    Next, we’re going to edit limits.con. Open that file with the command:

    1
    sudo vim /etc/security/limits.conf

    Then add the following lines to the limits.conf file:

    1
    2
    sonar   -   nofile   65536
    sonar - nproc 4096

    Then press ESC and and enter :wq! to come out of the above screen.

    Reload SonarQube

    Now we need to reload system level changes without server boot to do it run follwing command:
    1
    sudo sysctl -p

    Start SonarQube

    Now we need to start SonarQube. To do it run following command:
    1
    2
    3
    sudo systemctl start sonar

    sudo systemctl enable sonar

    Check SonarQube Status

    Now we need to check SonarQube status. To do it run following command:
    1
    sudo systemctl status sonar

    To check the Sonar logs to make sure there is no error:

    1
    tail -f /opt/sonarqube/logs/sonar*.log

    Access the web interface

    To acces the web interface, you ned to navigate to
    1
    http://your_sonarqube_public_dns_name:9000/

    Video Tutorial

    You can find the YouTube tutorial from this link.

    Conclusion

    In this tutorial, we learned how to install SonarQube on Ubuntu. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail.

    Happy Coding