October 21, 2021
How to install SonarQube on Ubuntu 20.04 LTS

How To Install SonarQube on Ubuntu 20.04 LTS

On this tutorial we will learn how to install SonarQube Community edition version 9.0.1 on Ubuntu 20.04 LTS operating system

Introduction

SonarQube® is an automatic code review tool to detect bugs, vulnerabilities, and code smells in our code. It can integrate with our existing workflow to enable continuous code inspection across the project branches and pull requests. SonarQube can analyze a wide range of code in different programming languages. SonarQuber consists of several versions, namely Community Edition, Developer Edition, Enterprise Edition and Data Center Edition. This tutorial will learn how to install SonarQube Community Edition version 9.0.1 which is free and opersource on Ubuntu 20.04 LTS operating system.



Install SonarQube On Ubuntu 20.04 LTS

On this tutorial we will learn how to install SonarQube Version 9.0.1, the installation will be consist of several steps, namely :

Prerequisite

SonarQube server requires Java version 11 to run properly, so we have to prepare Java 11 on the system. We will verify, the Java installed on our system by typing command line : java -version.

mpik@app01:~$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

If there is no java 11 installed on our system, then we have to install java 11 first. For Java 11 installation on Ubuntu 20.04 system, can be found on https://otodiginet.com/software/how-to-install-java-openjdk-11-on-ubuntu-20-04-lts/ article.

1. Installing And Configuring Database (PostgreSQL).

SonarQube requires a database to store its data. There are several databases that support SonarQube, including: PostgreSQL, Oracle and Microsoft SQL Server. For our tutorial, we will be using PostgreSQL.

1.1 Install PostgreSQL 13 on Ubuntu 20.04 LTS

If we don’t have a PostgreSQL database on our system, we will install it first. The article on installing PostgreSQL on Ubuntu 20.04 has been discussed in https://otodiginet.com/database/how-to-install-postgresql-13-on-ubuntu-20-04-lts/ article. Incidentally on our system, we have installed PostgreSQL so we just have to use it.

1.2 Create System and Database user (called as sonar)

On this step, we will be using user postgres to execute the script. The user postgres is default admin user for PostgreSQL. We will create a new user called as sonar and set it the password and also creating a username called as sonarqube , by following command :

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

At the last, we have a user called as sonar, even for user system and database user.

2. Creating New Group and User For SonarQube

At this step, we will create a dedicated user and group for SonarQube.

  1. Creating new group :
    sudo groupadd sonar
  2. Adding sonar user to the group and grant user access
    sudo useradd -d /opt/sonarqube -g sonar sonar
    sudo chown sonar:sonar /opt/sonarqube -R
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

3. Download and Extract SonarQube

At this section we will download, extract and install SonarQube on the system. As mentioned above, we will use SonarQube community edition version 9.0.1.

SonarQube Edition

3.1 Download SonarQube

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]

3.2 Extract SonaQube

We will extract/unzip the SonarQube source and moving it to the /opt/sonarquber/ directory.

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/
..

mpik@app01:~$ sudo mv sonarqube-9.0.1.46107 /opt/sonarqube/

4. Configuring SonarQube

The SonarQube configuration file is located on /opt/sonarqube/conf/sonar.properties and /opt/sonarqube/bin/linux-x86-64/sonar.sh files. The parameters that we will adjust on file /opt/sonarqube/conf/sonar.properties are : User credentials and database which we are using.

# 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
#----- 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

while in the /opt/sonarqube/bin/linux-x86-64/sonar.sh file the parameters we will change are, RUN_AS_USER.

# 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

5. Setting Systemd Service

After all parameter are set, then we will set the Sonarquber service which is called by the system. We will create a systemd service file to manage SonarQube service (start, stop and check the status). For this purpose we will create a new file 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

The content of the file is as shown below :

[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

After all are set, then we should enable and starting it, by typing command line :

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

Then check the service status, by submitting command line :

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 is already running

6. Modify Kernell System

As noted in the documentation of SonarQube version 9.0.1 , that we have to modify kernel system limits on /etc/sysctl.conf file, for parameters :

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

To take effect, then we have to reboot the system.

7. Accessing SonarQube Website Console

After all set, then we will access the SonarQube website console, which is located on URL : http://ip_address_or_hosntanem:9000. Log in with username admin and password admin. SonarQube will prompt us to change the password.

SonarQuber website interface for the firstime
SonarQuber website interface for the firstime
SonarQube Update Password
SonarQube Update Password



SonarQuber version 9.0.1 Website Front End
SonarQuber version 9.0.1 Website Front End

SonarQube has been installed and ready to be used.

Conclusion

On this article, we have learnt how to install SonarQube on Ubuntu 20.04 LTS operating system successfully. I hope this article will be useful for anyhone who need SonarQube installation tutorial.

Share this article via :

Leave a Reply

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