SonarQube Logo

SonarQube on Ubuntu 14.04

SonarQube (previously just Sonar) is an open source code quality tool. In this short series of articles I’ll be installing and configuring SonarQube on Ubuntu 14.04. Installing SonarQube on Ubuntu is just part of a wider continuous integration (CI) server build that I’ve been performing. Previous articles include installing Artifactory¬†and Java 8.

Installing SonarQube on Ubuntu

SonarQube is made constructed from three parts: a database, a web server and one or more analysers. For a small installation such as the one I’m performing here it’s fine to have all the components on the same server. For a larger installation the components can be spread out over multiple machines. In my case I’m actually going to have the database on a separate machine but that’s just because I have a dedicated database machine where I am.

Installing the Database

Create a database for SonarQube to use. I’m going to perform these operations though the MySQL Workbench but they can also be performed from the command line through a MySQL prompt. The commands you need to run will be something like those shown below. Alter depending on exactly how you want the security configured but this is a reasonable default.

CREATE DATABASE sonarqube CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON 'sonarqube'.* TO 'sonarqube'@'localhost';
FLUSH PRIVILEGES;

Installing the Web Server

Note: I’m running these commands su’ed to root. If you aren’t su’ed to root you’ll need to liberally sprinkle with sudo (most commands will need to be proceeded with sudo).

 

There are now two options for how to install SonarQube on Ubuntu now, either use the native packages or install from the zip file which can be downloaded from the SonarQube site. I’m going to use the native packages as generally that is preferable to a manual install. In order to use the packages you first need to modify your sources.list file to include the SonarQube repository.

cd /etc/apt
nano sources.list

Then add the following line to the bottom of the file:

deb http://downloads.sourceforge.net/project/sonar-pkg/deb binary/

Next update apt and install SonarQube

apt-get update
apt-get install sonar

As far as I can tell the SonarQube repository doesn’t have a code signing key so this installation will give a warning about not being able to authenticate the package. If you accept the warning the package will install into /opt/sonar which should be owned by a new user called sonar.

Now switch to the SonarQube conf directory and configure the install so that it can connect to the database:

cd /opt/sonar/conf
nano sonar.properties

According to the official instructions the out of the box configuration is to use an embedded H2 database but that is only any good for testing or tiny installs (and it doesn’t seem to be the case anyway). I’ll be configuring the system to use the MySQL database created earlier. Start by uncommenting the following two lines (around line 18):

sonar.jdbc.username=sonarqube
sonar.jdbc.password=password

Set the username and password that you created above. Now scroll down until you find the MySQL connection settings, uncomment them and modify to suit your setup:

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

The MySQL settings give plenty of warnings about only the InnoDB storage engine being supported. These look scary but but you have regular MySQL install it’s not a problem as both engines are available. If you are inclined you can modify the context path and port number SonarQube will be available on. I intend to front SonarQube with Apache so I won’t change these from their default values.

Now start SonarQube with:

service sonar start

SonarQube is quite a large application and will take a while to start (especially true if you server runs out of memory as mine did). You should now be able to access SonarQube as:

http://server_address:9000/

You now have SonarQube on Ubuntu up and running. The next step I usually perform is to proxy access through Apache as this allows me to distribute a URL that doesn’t contain a port number (users seem to get confused by ports numbers) this step it optional though and you can quite happily skip it.

Proxying with Apache

If you want to proxy access to SonarQube it’s quite simple with Apache. Just create a new virtual host with settings that look something like this:

<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName sonarqube.example.com
ProxyPass / http://sonarqube.example.com:9000/
ProxyPassReverse / http://sonarqube.example.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log common
</VirtualHost>

Obviously you will need to change the URL and you will probably want to change where the log files end up.

After Install

For some reason this is unusually difficult to find in the documentation but after a fresh install there is a default administrator account:

Username: admin
Password: admin

Read More

Posted in Java, Linux, Methodology and tagged , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *