Table of Contents
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 18.04/16.04 LTS with minimum 2GB RAM and 1 CPU.
- PostgreSQL Version 9.3 or higher
- SSH access with sudo privileges
- Firewall Port: 9000
sudo sysctl -w vm.max_map_count=262144
sudo sysctl -w fs.file-max=65536
sudo ulimit -n 65536
sudo ulimit -u 4096To Increase the vm.max_map_count kernal ,file descriptor 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 upgradeInstall 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-11-jdk -y sudo apt-get install openjdk-11-jre -y
SET Default JDK
To set default JDK or switch to OpenJDK enter below command,sudo update-alternatives --config javaYou 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 modeType 1 to switch OpenJDK 11.
Check JAVA Version:
java -versionOutput:
$ java -version openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-116.04.1) OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-116.04.1, mixed mode, sharing)
Step #2: Install and Setup PostgreSQL 10 Database For SonarQube
Add and download the PostgreSQL Reposudo 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;
\q
Switch back to the sudo user by running the exit command.
exit
Step #3: Download and Install SonarQube on Ubuntu
Download sonaqube installer files archieve To download latest version of visit SonarQube download page.cd /tmp
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zipOutput:
https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.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: 206192630 (197M) [application/zip] Saving to: ‘sonarqube-7.9.1.zip’ sonarqube-7.9.1.zip 100%[=================================================================================================>] 196.64M 30.0MB/s in 7.4sUnzip the archeve setup to /opt directory
sudo unzip sonarqube-7.9.1.zip -d /optMove extracted setup to /opt/sonarqube directory
sudo mv /opt/sonarqube-7.9.1 /opt/sonarqube
Step #4: Configure SonarQube
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 saparate group and user to run sonarqube.1. Create Group and User:
Create a group as sonarsudo groupadd sonarNow 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 -ROpen 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.
sonar.jdbc.username=your_user sonar.jdbc.password=your_passwordEdit the sonar script file and set RUN_AS_USER
sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh
RUN_AS_USER=sonarType CTRL+X to save and close the file.
2. Start SonarQube:
Now to start SonarQube we need to do following: Switch to sonar usersudo su sonarMove to the script directory
cd /opt/sonarqube/bin/linux-x86-64/Run the script to start SonarQube
./sonar.sh startWe 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 statusOutput:
sonar@fosstechnix:~/bin/linux-x86-64$ ./sonar.sh status SonarQube is running (75745).
4. SonarQube Logs:
To check sonarqube logs, navigate to /opt/sonarqube/logs/sonar.log directorytail /opt/sonarqube/logs/sonar.logOutput:
sonar@fosstechnix:~/bin/linux-x86-64$ tail /opt/sonarqube/logs/sonar.log 2019.09.21 05:26:57 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release. /postgresql/postgresql-42.2.5.jar org.sonar.server.app.WebServer /opt/sonarqube/temp/sq-process18124143927354381169properties 2019.09.21 05:28:55 INFO app[][o.s.a.SchedulerImpl] Process[web] is up 2019.09.21 05:28:55 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/postgresql/postgresql-42.2.5.jar org.sonar.ce.app.CeServer /opt/sonarqube/temp/sq-process11287447210083370759properties 2019.09.21 05:29:08 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up 2019.09.21 05:29:08 INFO app[][o.s.a.SchedulerImpl] SonarQube is upusing about output you will see that sonaqube is up and running successfully.
Step #5: Configure Systemd service
Create a systemd service file for SonarQube to run as System Startup.sudo nano /etc/systemd/system/sonar.serviceAdd 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 sonarEnable the SonarQube service to automatically at boot time System Startup.
sudo systemctl enable sonarcheck if the sonarqube service is running,
sudo systemctl status sonar
Step #6: Access SonarQube
To access the SonarQube using browser type server IP followed by port 9000.http://server_IP:9000 OR http://localhost:9000Login to SonarQube with default administrator username and password is admin.

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 = 262144To set value permanently, update the vm.max_map_count value in /etc/sysctl.conf. To verify after rebooting,
sysctl vm.max_map_count
Best article on sonar-qube, you saved my time
Thanks for your efforts brotherrrrr
Very nice. One small addition to help people after adding SonarQube to systemd: It failed on my machine with “max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]”.
Adding the two lines
LimitNOFILE=65536
LimitNPROC=4096
in /etc/systemd/system/sonar.service fixed my problem. The SonarQube documentation also refers to that on https://docs.sonarqube.org/latest/requirements/requirements/#Requirements-Linux
Hello Bernd,
Thanks. Updated article as per your suggestion.
Modify the step 2: postgres=# grant all privileges on DATABASE sonarqube to sonar;
from grant all privileges on sonarqube to sonar;
Thanks Puja for your suggestion, We have modified it.
Great Article Thanks For Your Efforts.
Thanks for your comment.
I have successfully installed sonarqube and it shows active running status. But it does not open on web browser.
The server is setup in gcp vm instance and the vm has an external ip.
what could be the problem?
Hello Aboli,
Please change sonar.core.serverBaseURL IP in sonar.properties, add sonarqube port in firewall/inbound traffic and restart sonarqube service.