SonarQube Installation on Ubuntu 20.04 with Community Branch Plugin

Çiğdem Kadakoğlu
4 min readJan 3, 2023

--

SonarQube Community Edition: 9.8-Ubuntu 20.04-Community Branch Plugin

Hi everyone,

I will show you how to install SonarQube on Ubuntu 20.04 with community branch plugin in this article. 🐧

kadakoglucigdem

On a single Ubuntu server, we’ll install PostgreSQL and SonarQube. We must first configure ElasticSearch.

sudo nano /etc/sysctl.conf

These configurations must be added to this file.

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

Reboot the server.

sudo reboot

Following that, we must install and configure PostgreSQL.

sudo apt update -y
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 -
sudo apt install postgresql postgresql-contrib -y
sudo systemctl enable postgresql
sudo systemctl start postgresql

To create and grant databases, we will now use the “postgres” user. You can also enter the password for this user.

sudo passwd postgres
su - postgres
createuser sonar
psql
ALTER USER sonar WITH ENCRYPTED password '<PASSWORD>';
CREATE DATABASE sonarqube OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;
\q
exit
kadakoglucigdem

PostgreSQL is now installed. After that, SonarQube, JDK 11, and ZIP will be installed.

sudo apt-get install openjdk-11-jdk -y
sudo apt-get install zip -y

Here, you can choose the version you want to install.

sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.8.0.63668.zip
sudo unzip sonarqube-9.8.0.63668.zip
sudo mv sonarqube-9.8.0.63668 /opt/sonarqube
sudo groupadd sonar
sudo useradd -d /opt/sonarqube -g sonar sonar
sudo chown sonar:sonar /opt/sonarqube -R

You need to select the appropriate version of this plugin based on the SonarQube version in order to install the community branch plugin here.

cd /opt/sonarqube/extensions/plugins
sudo wget https://github.com/mc1arke/sonarqube-community-branch-plugin/releases/download/1.14.0/sonarqube-community-branch-plugin-1.14.0.jar

Sonarqube-community-branch-plugin-1.14.0.jar is required because I installed version 9.8 for SonarQube. This directory, “/opt/sonarqube/extensions/plugins,” is where we need to install them.

The SonarQube configuration file must then be configured.

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

These configurations must be added to this file.

sonar.jdbc.username=sonar
sonar.jdbc.password=<PASSWORD>
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

sonar.web.javaAdditionalOpts=-javaagent:/opt/sonarqube/extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=web
sonar.ce.javaAdditionalOpts=-javaagent:/opt/sonarqube/extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=ce

This file is now required for SonarQube to run as a service.

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

Add the following to the file.

[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

The next step is to enable and start SonarQube as a service.

sudo systemctl enable sonar
sudo systemctl start sonar
sudo systemctl status sonar

Because of the branch plugin, we should click the “I understand the risk” button when we log in to SonarQube.

kadakoglucigdem

This is finished now, ta-daa!

Username: admin

Password: admin

kadakoglucigdem

Demo

  • Manually create a project.
kadakoglucigdem
  • Generate a token for Azure DevOps.
kadakoglucigdem
  • Create a service connection for SonarQube.
kadakoglucigdem
  • After analyzing different branches, you can see something like this:
kadakoglucigdem
kadakoglucigdem

NOTE: If you want to install an earlier SonarQube version, you need this configuration below. First, you need to edit the sonar script file, uncomment "RUN_AS_USER," and save.

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

How to Remove

sudo systemctl stop sonar
sudo systemctl disable sonar
sudo systemctl stop postgresql
sudo systemctl disable postgresql
sudo rm /etc/systemd/system/sonar.service
sudo rm -rf /opt/sonarqube/
sudo apt remove postgresql postgresql-contrib -y
sudo apt autoremove
kadakoglucigdem

Hope you find my article helpful. I wish everyone an awesome day!

You can check this article: SonarQube Integration with Azure DevOps

--

--

Çiğdem Kadakoğlu

Senior Cloud&DevOps Engineer | Instructor | Docker Captain | Azure Solutions Architect | CK{A|AD}-AZ{104|204|305|400|800|801|900}-DP900-AI{900|102}-PL900🐧🐳🌼☕