On this tutoria, we will learn how to install SonarQube Version 9.0.1 on Ubuntu 20.04 operating system.
When a developer develops an application, there will be various steps that must be passed, one of which is coding. A developer will be very happy when he finds his work error and bug free. One of the efforts to make an application free from errors and bugs is to use tools that can help analyze and provide information on where the error is if it occurs. One of the tools that can be used for this purpose is SonarQube.
SonarQube can analyze a wide range of code in different programming languages through plugins. This guide explains how to install SonarQube on Ubuntu 20.04 LTS. This guide will use SonarQube Community Edition which is free and opersource.
SonarQube Installation on Ubuntu 20.04 LTS
Before we are going to the SonarQueb installation process we have to prepare the environment first as mentioned below :
Ubuntu 20.04 System with updated repository with 4GB RAM and 2vCPU cores
root or ordinary account with sudo privilege
Sufficient disk space and good internet access
On this tutorial we will examine how to install SonarQube Version 9.0.1, the installation will be consist of several steps, namely :
SonarQuber requires Java to run properly. Java is one of the requirements to install and set up SonarQube on Ubuntu 2004 LTS. On this tutorial, we will install OpenJDK 11. To install OpenJDK 11 we will submit the following command line :
$ sudo apt install openjdk-11-jre-headless
mpik@app01:~$ sudo apt install openjdk-11-jre-headless
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libevent-core-2.1-7 libevent-pthreads-2.1-7 libmecab2 libzip5 linux-headers-5.4.0-26 linux-headers-5.4.0-26-generic
linux-image-5.4.0-26-generic linux-modules-5.4.0-26-generic linux-modules-extra-5.4.0-26-generic mecab-ipadic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
default-jre fonts-dejavu-extra fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
ca-certificates-java java-common openjdk-11-jre-headless
0 upgraded, 3 newly installed, 0 to remove and 402 not upgraded.
Need to get 37.3 MB of archives.
After this operation, 171 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 java-common all 0.72 [6,816 B]
Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openjdk-11-jre-headless amd64 11.0.11+9-0ubuntu2~20.04 [37.2 MB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal/main amd64 ca-certificates-java all 20190405ubuntu1 [12.2 kB]
Fetched 37.3 MB in 3min 15s (191 kB/s)
Selecting previously unselected package java-common.
(Reading database ... 238658 files and directories currently installed.)
Preparing to unpack .../java-common_0.72_all.deb ...
Unpacking java-common (0.72) ...
Selecting previously unselected package openjdk-11-jre-headless:amd64.
Preparing to unpack .../openjdk-11-jre-headless_11.0.11+9-0ubuntu2~20.04_amd64.deb ...
Unpacking openjdk-11-jre-headless:amd64 (11.0.11+9-0ubuntu2~20.04) ...
Selecting previously unselected package ca-certificates-java.
Preparing to unpack .../ca-certificates-java_20190405ubuntu1_all.deb ...
Unpacking ca-certificates-java (20190405ubuntu1) ...
Setting up java-common (0.72) ...
Setting up openjdk-11-jre-headless:amd64 (11.0.11+9-0ubuntu2~20.04) ...
On this stage we will configure SonarQube database. We will create a new database user for SonarQube need. We will log in to PostgreSQL database and do a DML task for creating a new database user called as sonar.
mpik@app01:~$ sudo su - postgres
[sudo] password for mpik:
postgres@app01:~$ createuser sonar
psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))
Type "help" for help.
postgres=# ALTER USER sonar WITH ENCRYPTED password 'Sonar2021';
postgres=# CREATE DATABASE sonarqube OWNER sonar;
postgres=# GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;
On this stage, we will download and install SonarQube on Ubuntu 22.04 operating system. We will locate the download URL from the SonarQube official download page, then download the SonarQube distribution files, by subumitting command line :
On this stage, we will configure SonarQube application by editing its configuration file. The file is located at /opt/sonarqube/conf/sonar.properties and /opt/sonarqube/bin/linux-x86-64/sonar.sh.
mpik@app01:/opt$ sudo vi /opt/sonarqube/conf/sonar.properties
# Property values can:
# - be overridden by environment variables. The name of the corresponding environment variable is the
# upper-cased name of the property where all the dot ('.') and dash ('-') characters are replaced by
# underscores ('_'). For example, to override 'sonar.web.systemPasscode' use 'SONAR_WEB_SYSTEMPASSCODE'.
# - be encrypted. See https://redirect.sonarsource.com/doc/settings-encryption.html
# - The embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are Oracle, PostgreSQL and Microsoft SQLServer.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#----- Oracle 12c/18c/19c
# The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/.
# Only the thin client is supported, and we recommend using the latest Oracle JDBC driver. See
# https://jira.sonarsource.com/browse/SONAR-9758 for more details.
# If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#----- PostgreSQL 9.6 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
mpik@app01:/opt$ sudo vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
# NOTE - This will set the user which is used to run the Wrapper as well as
# the JVM and is not useful in situations where a privileged resource or
# port needs to be allocated prior to the user being changed.
We will create a systemd service file to start SonarQube at system boot
On this stage, we will create a systemd service file to start SonarQube appliction at system boot. In this tutorial we will create a new file and named it as otosonar.service (the file name is depend on our need) and location will be at /etc/systemd/system directory.