How to install SonarQube on Ubuntu 20.04 LTS
How to install SonarQube on Ubuntu 20.04 LTS

How To Install SonarQube on Ubuntu 20.04 LTS

On this tutoria, we will learn how to install SonarQube Version 9.0.1 on Ubuntu 20.04 operating system.

Introduction

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

Prerequisites

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 :

 

The details of installation will be detail discussed below.

1. Install OpenJDK 11

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

Output :

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
  mecab-ipadic-utf8 mecab-utils
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  ca-certificates-java java-common
Suggested packages:
  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) ...

2. Install and Configure Database (PostgreSQL)

SonarQube requires a database system to store and manage its data. From 2016, SonarQube only supports PostgreSQL and dropped support for MySQL.

2.1 Install PostgreSQL 13 on Ubuntu 20.04 LTS

On this tutorial we will use PostgreSQL version 13 for SonarQube database. The PostgreSQL 13 installation on Ubuntu 20.04 has been discussed on How To Install PostgreSQL 13 On Ubuntu 20.04 LTS article.

2.2 Configuring SonarQube Database

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
postgres@app01:~$ psql
psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))
Type "help" for help.

postgres=# ALTER USER sonar WITH ENCRYPTED password 'Sonar2021';
ALTER ROLE
postgres=# CREATE DATABASE sonarqube OWNER sonar;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;
GRANT
postgres=# \q

3. Download and Install SonarQube

3.1 Download SonarQube Distribution File

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 :

$ sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.1.46107.zip

Output :

mpik@app01:~$ sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.1.46107.zip
[sudo] password for mpik: 
--2021-09-17 22:29:41--  https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.1.46107.zip
Resolving binaries.sonarsource.com (binaries.sonarsource.com)... 91.134.125.245
Connecting to binaries.sonarsource.com (binaries.sonarsource.com)|91.134.125.245|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 290201762 (277M) [application/zip]
Saving to: ‘sonarqube-9.0.1.46107.zip’

sonarqube-9.0.1.46107.zip        100%[=========================================================>] 276.76M   114KB/s    in 71m 22s 

2021-09-17 23:41:04 (66.2 KB/s) - ‘sonarqube-9.0.1.46107.zip’ saved [290201762/290201762]
Download SonarQube Download
Download SonarQube Download

3.2 Unzip SonarQube Source File

By using zip command line, we will unzip the downloaded file.

$ sudo unzip sonarqube-9.0.1.46107.zip

Output :

mpik@app01:~$ sudo unzip sonarqube-9.0.1.46107.zip
[sudo] password for mpik: 
Archive:  sonarqube-9.0.1.46107.zip
   creating: sonarqube-9.0.1.46107/
   creating: sonarqube-9.0.1.46107/bin/
   creating: sonarqube-9.0.1.46107/bin/jsw-license/
  inflating: sonarqube-9.0.1.46107/bin/jsw-license/LICENSE.txt  
   creating: sonarqube-9.0.1.46107/bin/windows-x86-64/
  inflating: sonarqube-9.0.1.46107/bin/windows-x86-64/StopNTService.bat  
   creating: sonarqube-9.0.1.46107/bin/windows-x86-64/lib/
  inflating: sonarqube-9.0.1.46107/bin/windows-x86-64/lib/wrapper.dll  
  inflating: sonarqube-9.0.1.46107/bin/windows-x86-64/StartSonar.bat  
  inflating: sonarqube-9.0.1.46107/bin/windows-x86-64/wrapper.exe  
  inflating: sonarqube-9.0.1.46107/bin/windows-x86-64/StartNTService.bat  
   creating: sonarqube-9.0.1.46107/bin/macosx-universal-64/
   creating: sonarqube-9.0.1.46107/bin/macosx-universal-64/lib/

. . .

inflating: sonarqube-9.0.1.46107/lib/jdbc/postgresql/postgresql-42.2.19.jar  
   creating: sonarqube-9.0.1.46107/lib/jdbc/h2/
  inflating: sonarqube-9.0.1.46107/lib/jdbc/h2/h2-1.4.199.jar  
  inflating: sonarqube-9.0.1.46107/lib/sonar-shutdowner-9.0.1.46107.jar  
   creating: sonarqube-9.0.1.46107/elasticsearch/plugins/

3.3 Move Unzip File

After unzipping the source distribution file, then we will move unzipped files to /opt/sonarqube directory.

$ sudo mv sonarqube-9.0.1.46107 /opt/sonarqub/

4. Add SonarQube Group and User

We need a dedicated user system and group for SonarQube, which can not run as the root user. To fulfill this requirement, we do the following tasks.

  1. Create a sonar group
  2. Create a sonar user and set /opt/sonarqube as the home directory.
  3. Grant the sonar user access to the /opt/sonarqube directory.
mpik@app01:~$ sudo groupadd sonar
[sudo] password for mpik: 
mpik@app01:~$ sudo useradd -d /opt/sonarqube -g sonar sonar
mpik@app01:~$ sudo chown sonar:sonar /opt/sonarqube -R
mpik@app01:/opt$ sudo chown sonar:sonar /opt/sonarqube -R
mpik@app01:/opt$ ls -ltr
total 4
drwxr-xr-x 11 sonar sonar 4096 Sep 20 19:45 sonarqube

5. Configure SonarQube

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

#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT:
# - 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.
sonar.jdbc.username=sonar
sonar.jdbc.password=Sonar2021

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=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
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE


#----- PostgreSQL 9.6 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
#sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
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.
RUN_AS_USER=sonar
We will create a systemd service file to start SonarQube at system boot

6. Creating SystemD Service

6.1 Create SystemD Service File

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.

$ sudo vi /etc/systemd/system/otosonar.service
[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

User=sonar
Group=sonar
Restart=always

LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

6.2 Enable, Start and Monitor SonarQube Service Status

On this stage, we will manage SonarQube service. We will enable, start and monitor SonarQube service by submitting command line.

$ sudo systemctl enable otosonar
$ sudo systemctl start otosonar
$ sudo systemctl status otosonar

Output :

mpik@app01:/opt$ sudo systemctl enable otosonar
Created symlink /etc/systemd/system/multi-user.target.wants/otosonar.service → /etc/systemd/system/otosonar.service.
mpik@app01:/opt$ sudo systemctl start otosonar
mpik@app01:/opt$ sudo systemctl status otosonar
● otosonar.service - SonarQube service
     Loaded: loaded (/etc/systemd/system/otosonar.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-09-20 19:57:53 PDT; 5s ago
    Process: 5409 ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start (code=exited, status=0/SUCCESS)
   Main PID: 5461 (wrapper)
      Tasks: 50 (limit: 9436)
     Memory: 736.3M
     CGroup: /system.slice/otosonar.service
             ├─5461 /opt/sonarqube/bin/linux-x86-64/./wrapper /opt/sonarqube/bin/linux-x86-64/../../conf/wrapper.conf>
             ├─5474 java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.path=./lib -clas>
             └─5573 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFra>

Sep 20 19:57:52 app01.otodiginet.com systemd[1]: Starting SonarQube service...
Sep 20 19:57:52 app01.otodiginet.com sonar.sh[5409]: Starting SonarQube...
Sep 20 19:57:53 app01.otodiginet.com sonar.sh[5409]: Started SonarQube.
Sep 20 19:57:53 app01.otodiginet.com systemd[1]: Started SonarQube service.
SonarQube Service
SonarQube Service

7. Modify Kernel System Limits

To ensure SonarQube runs smoothly, we need some parameters to adjust. These parameters are located at /etc/sysctl.conf file. We will add the following lines.

# SonarQube parameters
vm.max_map_count=262144
fs.file-max=65536
ulimit -n 65536
ulimit -u 4096

8. Access SonarQube Web Portal

We could access SonarQube application by using web browser. We will use server’s IP address or hostname on port 9000. In this tutorial we will use hostname (app01.otodiginet.com:9000).

SonarQube Login page
SonarQube Login page
Update Password for SonarQube web interface
Update Password for SonarQube web interface
SonarQube Main Page
SonarQube Main Page

Until this step, we have come to the end of the tutorial. The SonarQube application has been successfully installed on the Ubuntu 20.04 LTS operating system.

Conclusion

In this tutorial we have discussed how to install SonarQube on Ubuntu 20.04 LTS operating system. I hope this tutorila will be helpful.

(Visited 621 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *