Install Sonarqube with PostgreSQL

Hi! I’m about to introduce you a small step-by-step HowTo of Sonarqube + PostgerSQL installation on CentOS Minimal.

What is SonarQube?

SonarQube is an open source platform 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 language including Java (including Android), C#, PHP, JavaScript, C/C++, COBOL, PL/SQL, PL/I, ABAP, VB.NET, VB6, Python, RPG, Flex, Objective-C, Swift, Web and XML.

First Steps

First, you need to prepare a Linux server as a platform for SonarQube application. For example, you can use Vagrant for that purposes. In my case I use CentOS 7 Minimal:

[dmitriy.unterov@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[dmitriy.unterov@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

For complete this tutorial we need to install the following packages:

  • java
  • wget
  • unzip
sudo yum install wget java unzip

Great! Next we have to download and unzip a SonarQube package. You can get a link to tha latest Package on the official site.

wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.zip
unzip sonarqube-6.7.zip
ls
cd sonarqube-6.7

Nice. But we still have no DB! Let’s fix it!

PostgreSQL

Again, installation of packages in Linux (if they exist in repository =) is easy as 1-2-3.

sudo yum install postgresql-server postgresql-contrib
sudo postgresql-setup initdb

On that moment we need to configure postresql with the following:

sudo vi /var/lib/pgsql/data/pg_hba.conf

Make sure, that you changed METHOD to md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident
#host    replication     sonar           127.0.0.1/32            ident

And finally we have to start postgres daemon and enable it with systemctl:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Make Them Work Together!

Ok, for now we have all components we need. But they still don’t work. To fix it we need to create a ‘user’ and base for SonarQube instance. Just FYI: During PostgreSQL installatin user ‘postgres’ was created. So, let’s create a password for it and do the following:

passwd postgres
su - postgres
createuser sonar
psql
ALTER USER sonar WITH ENCRYPTED password 'sonar';
CREATE DATABASE sonar WITH ENCODING 'UTF8' OWNER sonar TEMPLATE=template0;
\q

Done! Next, we are going to config SonqrQube by editing ./conf/sonar.propertie file (I hope you are still in the root of sonarqube install directory?)

vi conf/sonar.properties

You have to uncomment a number of strings and edit a couple of them:

# DATABASE
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
# WEB SERVER
sonar.web.host=0.0.0.0
sonar.web.port=9000

By the way, if you use a CentOS firewall you may type the following:

sudo firewall-cmd --add-port=9000/tcp --permanent
sudo firewall-cmd --reload

It will permanently add TCP port 9000 to the accepted ports’ list.

The Last Step

Now we are ready to the Triumph!

./bin/linux-x86-64/sonar.sh start

Please note, that you should run it without a sudo. Now open a web browser and type

http://yourserverip:9000 (you can get you ip with ip a command)

You should see the sonarqube’s login page like this

SonarQubes's main screen

As a conclusion, I want to say that is not an official manual or a full guide. It is a small HowTo (and a basically a memo for me). But if it was useful to you - you’re welcome!

See you!.

Written on December 10, 2017