SonarQube Installation

1. Get SonarQube

1.1 Visit SonarQube download page.

1.2 Select the latest 5.1.x version of SonarQube and download the zip file, in this example we will get sonarqube-5.1.2.zip, to your Downloads folder

2. Extracts to C:\sonarqube

2.1 Extracts the downloaded file to C:\sonarqube\sonar-5.x using your preferred unzip program

3. Update database driver

Notice the database steps depends on your selected database. As we are using PostgreSQL as our preferred database, following steps are for such database.

3.1 Disable existing PostgreSQL driver, renaming it (we suggest to add ".DISABLED" at the end of the name)

> rename C:\sonarqube\sonar-5.x\lib\jdbc\postgresql\postgresql-9.3-1102-jdbc41.jar postgresql-9.3-1102-jdbc41.jar.DISABLED

3.2 Visit PostgreSQL JDBC download page

3.3 Download the proper PostgreSQL JDBC driver for your installation, in this example we will get postgresql-9.4.1212.jar

3.4 Move the download file to C:\sonarqube\sonar-5.x\lib\jdbc\postgresql\

4. Setup properties

4.1 Uncomment the following properties, and set the proper password

C:\sonarqube\sonar-5.x\conf\sonar.properties
# User credentials.
sonar.jdbc.username=sonar
sonar.jdbc.password=<mypassword>

#----- PostgreSQL 8.x/9.x
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

...

By default, it will use the server IP (if more than one is available, the sonar.web.host property needs to be set) on port 9000 and root context. You can change the host, port, context or other properties in that file.

5. Define system variables

5.1 Open system properties dialog

5.2 Select Advanced System Settings

5.3 Click on Environment Variables... button

5.4 On the Envirnoment Variable dialog, click on system variables New... button

5.5 Define the SONARQUBE_HOME variable

5.6 Accept changes and close all dialogs

6. Setup as a service

6.1 Run the SonarQube service installer

> %SONARQUBE_HOME%/bin/windows-x86-64/InstallNTService.bat

6.2 Open the Services dialog and select Properties on SonarQube service

6.3 Ensure to setup a user wich has enough privileges to run the server

6.4 Now find the PostgreSQL service name on the Services dialog

6.5 To ensure SonarQube service starts after PostgreSQL service is running, open the Command Prompt as administrator, and run the following command, using your PostgreSQL service name:

> C:\Windows\system32\sc config SonarQube depend=postgresql-x64-9.6

7. Check the service

7.1 Restart the SonarQube service

7.2 Check on your brower: http://myserver:9000

Remember to configure your firewall to allow port 9000.

1. Get SonarQube

1.1 Visit SonarQube download page

1.2 Download the latest SonarQube 5.1.x version

$ wget -P /tmp https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.1.2.zip

2. Extracts to /opt/sonarqube

2.1 Extracts the downloaded file to /opt/sonarqube

$ sudo mkdir /opt/sonarqube

$ unzip /tmp/sonarqube-5.1.2.zip -d /opt/sonarqube

$ ls -la /opt/sonarqube
total 12
drwxr-xr-x  3 root root 4096 Sep 30 12:28 .
drwxr-xr-x  6 root root 4096 Sep 30 12:28 ..
drwxr-xr-x 10 root root 4096 Jul 27  2015 sonarqube-5.1.2

3. Define a symbolic link to SonarQube

3.1 For easy upgrades on future it is highly recommended to create the symbolic link

$ sudo ln -s /opt/sonarqube/sonarqube-5.1.2 /opt/sonarqube/sonar

4. Update database driver

Notice the database steps depends on your selected database. As we are using PostgreSQL as our preferred database, following steps are for such database.

4.1 Disable existing PostgreSQL driver

$ mv /opt/sonarqube/sonar/lib/jdbc/postgresql/postgresql-9.3-1102-jdbc41.jar /opt/sonarqube/sonar/lib/jdbc/postgresql/postgresql-9.3-1102-jdbc41.jar.DISABLED

4.2 Download the proper PostgreSQL JDBC driver for your installation

$ wget -P /opt/sonarqube/sonar/lib/jdbc/postgresql/ https://jdbc.postgresql.org/download/postgresql-9.4.1212.jar

5. Setup properties

5.1 Uncomment the following properties, and set the proper password

/opt/sonarqube/sonar/conf/sonar.properties
# User credentials.
sonar.jdbc.username=sonar
sonar.jdbc.password=<mypassword>

#----- PostgreSQL 8.x/9.x
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

..

6. Setup wrapper

6.1 Change wrapper.java.command to the absolute java path

/opt/sonarqube/sonar/conf/wrapper.conf
wrapper.java.command=/opt/java/default-jdk/bin/java
#wrapper.java.command=java
. . .

7. Create sonar user

7.1 Create sonar user

$ sudo useradd sonar

$ sudo chown -R sonar:sonar /opt/sonarqube

8. Setup as a service with systemctl

This is the recommended way for registering your SonarQube service. If your Linux distribuition does not support systemctl to manage services, then skip to the next point.

8.1 Create the service descriptor

/lib/systemd/system/sonar.service
[Unit]
Description=SonarQube
Requires=postgresql.service
After=postgresql.service

[Service]
Environment=JAVA_HOME=/opt/java/default-jdk/ PATH=/opt/java/default-jdk/bin:/bin:/usr/bin
ExecStart=/opt/sonarqube/sonar/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/sonar/bin/linux-x86-64/sonar.sh stop
User=sonar
Type=forking
SyslogIdentifier=sonarqube
WorkingDirectory=/opt/sonarqube/sonar

[Install]
WantedBy=multiuser.target postgresql.service

8.2 Enable the service to start at boot

$ sudo systemctl enable sonar.service

9. Setup as a service with init.d

This setup is an alternative to previous one. If you created your service using systemctl, please skip this chapter.

9.1 Copy the service base file

$ sudo cp /opt/sonarqube/sonar/bin/linux-x86-64/sonar.sh /etc/init.d/sonar

9.2 Edit the copied file and add SONAR_HOME and PLATFORM properties. Modify the other properties as shown next:

/etc/init.d/sonar
#! /bin/sh
### BEGIN INIT INFO
# Provides:          SonarQube Service
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: SonarQube Service
# Description:
#
### END INIT INFO

. . .

# Home and Platform
SONAR_HOME="/opt/sonarqube/sonar"
PLATFORM="linux-x86-64"

# Wrapper
WRAPPER_CMD="${SONAR_HOME}/bin/${PLATFORM}/wrapper"
WRAPPER_CONF="${SONAR_HOME}/conf/wrapper.conf"

# Priority at which to run the wrapper.  See "man nice" for valid priorities.
#  nice is only used if a priority is specified.
PRIORITY=

# Location of the pid file.
PIDDIR="${SONAR_HOME}/.."

# If uncommented, causes the Wrapper to be shutdown using an anchor file.
#  When launched with the 'start' command, it will also ignore all INT and
#  TERM signals.
#IGNORE_SIGNALS=true

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

The header of the file is required if you want to avoid the following error when registering the service:

insserv: warning: script 'sonarqube' missing LSB tags and overrides

9.3 Register the service

$ sudo chmod 755 /etc/init.d/sonar
$ sudo update-rc.d -f sonar remove
$ sudo update-rc.d sonar defaults

10. Check the service

10.1 Restart the server to check if the service is loaded automatically on startup

$ service sonar status
● sonarqube.service - LSB: SonarQube Service
   Loaded: loaded (/etc/init.d/sonarqube)
   Active: active (running) since Fri 2016-09-30 12:08:17 CET; 1min 2s ago
  Process: 560 ExecStart=/etc/init.d/sonarqube start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/sonarqube.service
           ├─ 697 /opt/sonarqube/sonar/bin/linux-x86-64/wrapper /opt/sonarqube/sonar/conf/wrapper.conf wrapper.syslog.ident=SonarQube wrapper.pidfile=/opt/sonarqube/sonar/../SonarQ...
           ├─ 771 /opt/java/jdk1.8.0_111/bin/java -Djava.awt.headless=true -Xms3m -Xmx32m -Djava.library.path=./lib -classpath ../../lib/jsw/wrapper-3.2.3.jar:../../lib/sonar-appli...
           ├─1343 /opt/java/jdk1.8.0_111/jre/bin/java -Djava.awt.headless=true -Xmx1G -Xms256m -Xss256k -Djava.net.preferIPv4Stack=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX...
           └─2165 /opt/java/jdk1.8.0_111/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.management.enabled=false -Djruby.compile.invokedynamic=false -server -X...

Sep 30 12:08:16 myserver su[630]: Successful su for sonar by root
Sep 30 12:08:16 myserver su[630]: + ??? root:sonar
Sep 30 12:08:16 myserver su[630]: pam_unix(su:session): session opened for user sonar by (uid=0)
Sep 30 12:08:16 myserver sonarqube[560]: Starting SonarQube...
Sep 30 12:08:16 myserver sonarqube[560]: Removed stale pid file: /opt/sonarqube/sonar/../SonarQube.pid
Sep 30 12:08:17 myserver sonarqube[560]: Started SonarQube.
Sep 30 12:08:17 myserver su[630]: pam_unix(su:session): session closed for user sonar
Sep 30 12:08:17 myserver systemd[1]: Started LSB: SonarQube Service.

10.2 Check on your brower: http://myserver:9000

Remember to configure your firewall to allow port 9000.