LinuxTutorialsUbuntu

How To Install SonarQube on Ubuntu 20.04 LTS

Install SonarQube on Ubuntu 20.04

In this tutorial, we will show you how to install SonarQube on Ubuntu 20.04 LTS. For those of you who didn’t know, SonarQube is an open-source web-based tool to manage code quality and code analysis. SonarQube includes features like bug and vulnerability detection and code tracking. SonarQube can integrate into GitHub, Azure DevOps, Bitbucket, GitLab, and Docker. If you happen to have an on-premise Linux server, or a cloud account with the likes of AWS, Google Cloud, or Azure, handy, you can deploy the community edition of SonarQube for free.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the SonarQube on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, 18.04, and any other Debian-based distribution like Linux Mint or elementary OS.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install SonarQube on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Configure Kernel.

Now modify the kernel system limits. For this we must set the following:

  • vm.max_map_count must be greater than or equal to 524288
  • fs.file-max must be greater than or equal to 131072
  • The SonarQube user must be able to open at least 131072 file descriptors
  • The SonarQube user must be able to open at least 8192 threads
sudo nano /etc/sysctl.conf

Add the following lines to the bottom of that file:

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

Next, open the limits.conf file with the command:

sudo nano /etc/security/limits.conf

At the end of this file, add the following:

sonarqube   -   nofile   65536
sonarqube   -   nproc    4096

Then, reboot your system so the changes will take effect.:

reboot

Step 3. Installing Java.

Run the following command to install OpenJDK and JRE 11:

sudo apt install openjdk-11-jdk
sudo apt install openjdk-11-jre

Step 4. Installing PostgreSQL.

SonarQube only works with PostgreSQL. Now we running the command below to install it:

sudo apt install postgresql postgresql-contrib

Once done, start and enable the database service with the commands:

sudo systemctl enable postgresql
sudo systemctl start postgresql

Now we must set a password for the PostgreSQL user with the command:

sudo passwd postgres

Login as a PostgreSQL superuser and Create SonarQube PostgreSQL Database and Database user:

sudo -Hiu postgres
createuser sonaradmin
createdb -O sonaradmin sonarqubedb
psql
ALTER USER sonaradmin WITH ENCRYPTED password 'changeme';
\q
exit

Step 5. Installing SonarQube on Ubuntu 20.04.

Now download the latest version of the SonarQube installer from the official website:

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.1.44547.zip
sudo unzip sonarqube-8.9.1.44547.zip -d /opt

Move extracted setup to /opt/sonarqube directory:

sudo mv /opt/sonarqube-8.9.1.44547 /opt/sonarqube

Step 5. Configure SonarQube.

Now we create a group as sonar:

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

Next, open the SonarQube configuration file using your favorite text editor:

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 the above steps and add the Postgres connection string:

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

Next, edit the sonar script file and set RUN_AS_USER:

RUN_AS_USER=sonar

Now to start SonarQube:

sudo su sonar
cd /opt/sonarqube/bin/linux-x86-64/

Next, run the script to start SonarQube:

./sonar.sh start

To check if SonarQube is running enter the below command:

./sonar.sh status

Step 6. Configure Systemd service.

Let’s now create a systemd file, so the SonarQube service can be controlled. Create the file with the command:

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

Add the following line:

[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, you can now start and enable the SonarQube service with the following two commands:

sudo systemctl start sonarqube
sudo systemctl enable sonarqube

Step 7. Accessing SonarQube Web Interface.

Once successfully installed, access the SonarQube using browser-type server IP followed by port 9000:

http://your-server-ip-address:9000

You should eventually see a login screen, where you’ll use the default credentials of admin/admin. Upon successful authentication, you’ll be required to change the default password. Once you’ve taken care of that, you’ll find yourself at the SonarQube main page:

Install SonarQube on Ubuntu 20.04

Congratulations! You have successfully installed SonarQube. Thanks for using this tutorial for installing the SonarQube on your Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official SonarQube website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button