Setup SonarQube in Ubuntu 18.04.

share on:
Setup SonarQube in Ubuntu 18.04.

According to wikipedia:

SonarQube (formerly Sonar)[1] is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments,bugs, and security vulnerabilities.[

We will install and setup sonarqube in ubutnu 18.04 instance and use apache to reverse proxy to this sonarqube instance to access it over port 80.

Step 1: create a user for SonarQube.

useradd -M -r -s /usr/sbin/nologin sonar

Setup system. Install java 8 and other required versions.

apt-get update
apt-get upgrade -y
apt-get install unzip wget net-tools openjdk-8-jre -y

Step 2: Install PostgreSQL in Ubntu18.04

Add PostgreSQL repository in Ubuntu 18.04.

apt-get update
echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - | sudo apt-key add - 
sudo apt-get update 

Install PostgreSQL version of your choice.

apt-get install postgresql-11 -y

Create a user for SonarQube in postgresql.
su - postgres
createuser sonar

Create a database and make user ‘sonar’ owner of that database.

ALTER USER sonar WITH ENCRYPTED password '<Very strong password>';

Step 3 — Download and Install SonarQube 7.5.

Start by changing the current working directory to the SonarQube installation directory:

cd /opt/sonarqube

You can visit SonarQube downloads page and get the download link for SonarQube 7.5. We are going to set 7.5 as its compatible with Java 8.


Unzip the file:

unzip -d /opt/

Change the name of /opt/sonarqube-7.5 to /opt/sonar

cd /opt
mv sonarqube-7.5 sonar

Finally, update the permissions so that the sonar user will own these files, and be able to read and write files in this directory:

chown -R sonar:sonar /opt/sonar

Now we can start configuring the SonarQube server.

Make changes to /opt/sonar/conf/ Add these lines at the end of file.

sonar.jdbc.password=(sonar user's postgres password)

These settings will make SonarQube run in server mode on port 30000 on localhost.

Step 4. Manage SonarQube service with Systemd.

vi /etc/systemd/system/sonar.service

Description=SonarQube service


ExecStart=/opt/sonar/bin/linux-x86-64/ start
ExecStop=/opt/sonar/bin/linux-x86-64/ stop



Start SonarQube service.

systemctl daemon-reload
systemctl enable sonar
systemctl start sonar && systemctl status sonar

Step 5: Install and setup Apache as Reverse proxy for SonarQube.

apt-get update
apt-get install apache2
a2enmod rewrite proxy proxy_http headers

Configure Virtualhost for SonarQube in Apache. Change the name of domain in virtualhost configuration below.

cd /etc/apache2/sites-available

<VirtualHost *:80>

    ProxyRequests Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode

    ProxyPass / http://localhost:30000/ nocanon
    ProxyPassReverse / http://localhost:30000/
    ProxyPassReverse /

    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
    ErrorLog /var/log/apache2/sonar01-dev.www.debyum.com_error.log
    CustomLog /var/log/apache2/sonar01-dev.www.debyum.com_forwarded.log common_forwarded
    CustomLog /var/log/apache2/sonar01-dev.www.debyum.com_access.log combined env=!dontlog
    CustomLog /var/log/apache2/ combined


Enable the VirtualHost.

a2ensite sonar
systemctl reload apache2

Now you should be able to access sonarqube by visiting the url http://sonar01.<yourdomain> in your browser.

Thanks for reading. Hope this post on setup sonarqube in ubutnu 18.04 was helpful to you. Please leave your feedback.

share on:


Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: