How to Install SonarQube on Ubuntu 18.04/16.04 LTS

In this article, we are going to perform, How to Install SonarQube on Ubuntu 18.04/16.04 LTS.

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

Here, We are installing SonarQube 7.9.1 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 and file discriptor

$ sudo sysctl -w vm.max_map_count=262144
$ sudo sysctl -w fs.file-max=65536
$ sudo ulimit -n 65536
$ sudo ulimit -u 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-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 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 11.

Check JAVA Version:

$ java -version

Output:

$ 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

Install 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 and enable it to start automatically at boot time.

$ sudo systemctl start postgresql
$ 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 sonarqube  to sonar;

Exit from the psql shell:

\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.zip

Output:

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.4s

Unzip the archeve setup to /opt directory

$ sudo unzip sonarqube-7.9.1.zip -d /opt

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

sonar.jdbc.username=your_user
sonar.jdbc.password=your_password

Edit the sonar script file and set RUN_AS_USER

$ sudo nano  /opt/sonarqube/bin/linux-x86-64/sonar.sh
$ 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/sonar/bin/linux-x86-64/

Run the script to start SonarQube

./sonar.sh start

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:

[email protected]:~/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 directory

$ tail /opt/sonarqube/logs/sonar.log

Output:

[email protected]:~/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 up

using 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.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 [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

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

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 Install SonarQube on Ubuntu 18.04/16.04 LTS with Configure Sonarqube, Creating Systemd Service and Troubleshooting sonarqube.

Related Articles:

5 Simple Steps to Download & Install Jenkins on Windows

Shivdas Kanade

I am Shivdas Kanade working as DevOps Engineer. Believe in Sharing Knowledge.

2 thoughts on “How to Install SonarQube on Ubuntu 18.04/16.04 LTS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Shares
Share via
Copy link