Language:

Search

How to Install SonarQube on Ubuntu 22.04 LTS

  • Share this:

Introduction

SonarQube is an opensource web based tool to manage code quality and code analysis. It is most widely used in continuous code inspection which performs reviews of code to detect bugs, code smells and vulnerability issues of programming languages such as PHP, C#, JavaScript, C/C++ and Java , Also tracks statistics and creates charts that enable developers to quickly identify problems in their code.

Prerequisites

  • Ubuntu 22.04 LTS with minimum 2GB RAM and 1 CPU.
  • PostgreSQL Version 9.3 or higher
  • SSH access with sudo privileges
  • Firewall Port: 9000

Here, We are installing SonarQube 8.9 version and have to install Oracle JAVA/Open JDK, Postgres/MS-SQL as database and Latest browser before installing SonarQube. To know Prerequisite visit sonarqube official page

Note: MySQL Support for SonarQube is depricated. Increase the vm.max_map_count kernal ,file discriptor and ulimit for current session at runtime.

sysctl -w vm.max_map_count=524288
sysctl -w fs.file-max=131072
ulimit -n 131072
ulimit -u 8192

To Increase the vm.max_map_count kernal ,file discriptor and ulimit permanently . Open the below config file and Insert the below value as shown below,

 sudo nano /etc/security/limits.conf

/etc/security/limits.conf

sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

OR If you are using systemd to manage the sonarqube services then add below value in sonarqube unit file under [service] section.

[Service]
...
LimitNOFILE=65536
LimitNPROC=4096
...

Before installing, Lets update and upgrade System Packages

 sudo apt-get update
 sudo apt-get upgrade

Install wget and unzip package

 sudo apt-get install wget unzip -y

Step #1: Install OpenJDK

Install OpenJDK and JRE 11 using following command,

 sudo apt-get install openjdk-17-jdk -y
 sudo apt-get install openjdk-17-jre -y

SET Default JDK

To set default JDK or switch to OpenJDK enter below command,

 sudo update-alternatives --config java

You will see below choices for the alternative java (providing /usr/bin/java).

Selection    Path                                            Priority   Status

------------------------------------------------------------

  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

* 3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode

Type  1 to switch OpenJDK 17.

Check JAVA Version:

 java -version

Output:

java -version

openjdk version "17.0.7" 2023-03-14

OpenJDK Runtime Environment (build 17.0.7+10-post-Ubuntu-3ubuntu1)

OpenJDK 64-Bit Server VM (build 17.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Step #2: Install and Setup PostgreSQL 10 Database For SonarQube

Add and download the PostgreSQL Repo

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
 wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

Install the PostgreSQL database Server by using following command,

sudo apt-get -y install postgresql postgresql-contrib

Start PostgreSQL Database server

sudo systemctl start postgresql

Enable it to start automatically at boot time.

 sudo systemctl enable postgresql

Change the password for the default PostgreSQL user.

 sudo passwd postgres

Switch to the postgres user.

su - postgres

Create a new user by typing:

createuser sonar

Switch to the PostgreSQL shell.

psql

Set a password for the newly created user for SonarQube database.

ALTER USER sonar WITH ENCRYPTED password 'sonar';

Create a new database for PostgreSQL database by running:

CREATE DATABASE sonarqube OWNER sonar;

grant all privileges to sonar user on sonarqube Database.

grant all privileges on DATABASE sonarqube to sonar;

Exit from the psql shell:

\q

Switch back to the sudo user by running the exit command.

exit

Step #3: How to Install SonarQube on Ubuntu 22.04 LTS

Download sonaqube installer files archive To download latest version of visit SonarQube download page.

 cd /tmp
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.65466.zip

Output:

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

https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.65466.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: 209531101 (200M) [application/zip]

Saving to: ‘sonarqube-8.9.1.zip’

sonarqube-9.9.0.65466.zip                    100%[==========================================================================>] 199.82M  1.31MB/s    in 34s

 ‘sonarqube-9.9.0.65466.zip’ saved [209531101/209531101]

Unzip the archeve setup to /opt directory

 sudo unzip sonarqube-9.9.0.65466.zip -d /opt

Move extracted setup to /opt/sonarqube directory

sudo mv /opt/sonarqube-9.9.0.65466 /opt/sonarqube

Step #4:Configure SonarQube on Ubuntu 22.04 LTS

We can’t run Sonarqube as a root user , if you run using root user it stops automatically. We have found solution on this to create separate group and user to run sonarqube.

1. Create Group and User:

Create a group as sonar

 sudo groupadd sonar

Now add the user with directory access

 sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonar sonar 
 sudo chown sonar:sonar /opt/sonarqube -R

Open the SonarQube configuration file using your favorite text editor.

 sudo nano /opt/sonarqube/conf/sonar.properties

Find the following lines.

#sonar.jdbc.username=
#sonar.jdbc.password=

Uncomment and Type the PostgreSQL Database username and password which we have created in above steps and add the postgres connection string.

/opt/sonarqube/conf/sonar.properties

#--------------------------------------------------------------------------------------------------

# 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=sonar
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

Edit the sonar script file and set RUN_AS_USER

 sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh

/opt/sonarqube/bin/linux-x86-64/sonar.sh

# If specified, the Wrapper will be run as the specified user.

# IMPORTANT - Make sure that the user has the required privileges to write

#  the PID file and wrapper.log files.  Failure to be able to write the log

#  file will cause the Wrapper to exit without any way to write out an error

#  message.

# 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

Type CTRL+X to save and close the file.

2. Start SonarQube:

Now to start SonarQube we need to do following: Switch to sonar user

 sudo su sonar

Move to the script directory

 cd /opt/sonarqube/bin/linux-x86-64/

Run the script to start SonarQube

./sonar.sh start

Output:

Starting SonarQube...

Started SonarQube

We can also add this in service and can run as a service.

3. Check SonarQube Running Status:

To check if sonaqube is running enter below command,

./sonar.sh status

Output:

sonar@fosstechnix:~/bin/linux-x86-64$ ./sonar.sh status

SonarQube is running (9490).

4. SonarQube Logs:

To check sonarqube logs, navigate to /opt/sonarqube/logs/sonar.log directory

 tail /opt/sonarqube/logs/sonar.log

Output:

INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/opt/sonarqube]: /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/opt/sonarqube/lib/jdbc/h2/h2-1.3.176.jar org.sonar.ce.app.CeServer /opt/sonarqube/temp/sq-process15059956114837198848properties

 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up

 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

using about output you will see that sonaqube is up and running successfully.

Step #5:Configure Systemd service

First stop the SonarQube service as we started manually using above steps Navigate to the SonarQube installed path

 cd /opt/sonarqube/bin/linux-x86-64/

Run the script to start SonarQube

./sonar.sh stop

Create a systemd service file for SonarQube to run as System Startup.

 sudo nano /etc/systemd/system/sonar.service

Add the below lines,

/etc/systemd/system/sonar.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

Save and close the file. Now stop the sonarqube script earlier we started to run using as daemon. Start the Sonarqube daemon by running:

 sudo systemctl start sonar

Enable the SonarQube service to automatically  at boot time System Startup.

 sudo systemctl enable sonar

check if the sonarqube service is running,

 sudo systemctl status sonar

Successfully, We have covered How to Install SonarQube on Ubuntu 22.04 LTS .

Step #6: Access SonarQube

To access the SonarQube using browser type server IP followed by port 9000.

http://server_IP:9000 OR http://localhost:9000

Login to SonarQube  with default administrator username and password is admin.

sonarqube-web-interface

Finally, We have successfully performed all steps to for sonarqube setup.. If you want to change IP address , adding multipla IP address and change the default change in SonarQube Properties as shown below

 sudo nano /opt/sonarqube/conf/sonar.properties

/opt/sonarqube/conf/sonar.properties

# Binding IP address. For servers with more than one IP address, this property specifies which

# address will be used for listening on the specified ports.

# By default, ports will be used on all IP addresses associated with the server.

sonar.web.host=0.0.0.0


# Web context. When set, it must start with forward slash (for example /sonarqube).

# The default value is root context (empty value).

#sonar.web.context=

# TCP port for incoming HTTP connections. Default value is 9000.

sonar.web.port=9000

Troubleshooting

loaded plugin [org.elasticsearch.transport.Netty4Plugin] ERROR: [1] bootstrap checks failed. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144].Solution: Elasticsearch uses a MMap FS directory to store its indices. The default operating system limits on mmap counts is possibly to be too low, which may result in out of memory exceptions. Enter the below command to increase virtual memory value using sudo privileges,

 sudo sysctl -w vm.max_map_count=262144

Output:

 sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144

To set value permanently, update the vm.max_map_count value in /etc/sysctl.conf. To verify after rebooting,

 sysctl vm.max_map_count

Conclusion:

In this article, We have performed ,How to Download and How to Install SonarQube on Ubuntu 22.04 LTS with Configure Sonarqube, Creating Systemd Service and Troubleshooting sonarqube.

Sushan Shrestha

Sushan Shrestha

Join me on a captivating journey as I unravel the complexities of maintaining digital infrastructures, troubleshooting labyrinthine networks, and safeguarding the virtual realms from the shadows. Together, we'll explore the challenges and where every click and command shapes the digital destiny.