How to Install SonarQube on Ubuntu 18.04 LTS
SonarQube is an open source platform to continuously inspect code quality of applications. It is written in Java language and supports multiple databases. You can inspect code and check the health of an application for more than 20 programming languages including Java, C, C++, C#, PHP, and web languages like JavaScript, HTML and CSS. SonarQube can analyze source code, find security vulnerabilities, detect bugs and show the result on web-based dashboard. You can easily integrate SonarQube with Maven, Ant, Gradle, MSBuild, LDAP, Active Directory and GitHub.
In this tutorial, we will learn how to install SonarQube on an Ubuntu 18.04 LTS (Bionic Beaver) server.
Requirements
- A server running Ubuntu 18.04.
- A non-root user with sudo privileges.
Getting Started
Before starting, you will need to update your system with the latest version. You can do this by running the following command:
sudo apt-get update -y
sudo apt-get upgrade -y
Once your system is updated, restart the system to apply the changes.
Install Java
SonarQube is written in Java language, so you will need to install Java to your system. First, add the Java repository with the following command:
sudo add-apt-repository ppa:webupd8team/java
Next, update the repository and install Java with the following command:
sudo apt-get update -y
sudo apt-get install oracle-java8-installer -y
Once the Java is installed, check the Java version using the following command:
java -version
Output:
openjdk version "10.0.2" 2018-07-17 OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3) OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3, mixed mode)
Install and Configure PostgreSQL
By default, the latest version of PostgreSQL is not available in the Ubuntu 18.04 default repository. So you will need to add the PostgreSQL repository to your system.
You can do this with the following command:
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 -
Next, update the repository and install PostgreSQL with the following command:
sudo apt-get update -y
sudo apt-get install postgresql postgresql-contrib
Once the installation is completed, check the status of PostgreSQL with the following command:
sudo systemctl status postgresql
Output:
? postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Sun 2018-12-02 08:49:29 UTC; 4h 30min ago Process: 1295 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 1295 (code=exited, status=0/SUCCESS) Dec 02 08:49:29 ubuntu1804 systemd[1]: Starting PostgreSQL RDBMS... Dec 02 08:49:29 ubuntu1804 systemd[1]: Started PostgreSQL RDBMS.
Next, switch to the postgres user with the following command:
su - postgres
Next, create a sonar user with the following command:
createuser sonar
Next, switch to the PostgreSQL shell with the following command:
psql
Next, set password for sonar user and create a sonar database with the following command:
ALTER USER sonar WITH ENCRYPTED password 'password';
CREATE DATABASE sonar OWNER sonar;
Next, exti from the PostgreSQL shell:
\q
Install and Configure SonarQube
First, create a user for SonarQube with the following command:
sudo adduser sonar
Next, download the latest version of SonarQube with the following command:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip
Once the download is completed, unzip the downloaded file with the following command:
unzip sonarqube-6.7.6.zip
Next, copy the extracted directory to the /opt with the following command:
sudo cp -r sonarqube-6.7.6 /opt/sonarqube
Next, give ownership to the sonar user with the following command:
sudo chown -R sonar:sonar /opt/sonarqube
Next, you will need to configure SonarQube to run as a sonar user. You can do this with the following command:
sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh
Make the following changes:
RUN_AS_USER=sonar
Save and close the file. Then, open SonarQube default configuration file and modify the database credentials with the one we created earlier:
sudo nano /opt/sonarqube/conf/sonar.properties
Make the following changes:
sonar.jdbc.username=sonar sonar.jdbc.password=password sonar.jdbc.url=jdbc:postgresql://localhost/sonar sonar.web.host=127.0.0.1 sonar.search.javaOpts=-Xms512m -Xmx512m
Save and close the file, when you are finished.
Create Systemd Service file for SonarQube
Next, you will need to create a systemd service file to manage SonarQube service. You can do this with the following command:
sudo nano /etc/systemd/system/sonar.service
Add the following lines:
[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, when you are finished. Then, start SonarQube service and enable it to start on boot time with the following command:
sudo systemctl start sonar
sudo systemctl enable sonar
You can check the status of SonarQube service with the following command:
sudo systemctl status sonar
Output:
? sonar.service - SonarQube service Loaded: loaded (/etc/systemd/system/sonar.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2018-12-02 13:55:34 UTC; 2min 52s ago Process: 2339 ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start (code=exited, status=0/SUCCESS) Main PID: 2396 (wrapper) Tasks: 133 (limit: 2323) CGroup: /system.slice/sonar.service ??2396 /opt/sonarqube/bin/linux-x86-64/./wrapper /opt/sonarqube/bin/linux-x86-64/../../conf/wrapper.conf wrapper.syslog.ident=SonarQ ??2399 java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.path=./lib -classpath ../../lib/jsw/wrapper- ??2445 /usr/lib/jvm/java-8-oracle/jre/bin/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOc ??2545 /usr/lib/jvm/java-8-oracle/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp - ??2622 /usr/lib/jvm/java-8-oracle/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp - Dec 02 13:55:33 ubuntu1804 systemd[1]: Starting SonarQube service... Dec 02 13:55:33 ubuntu1804 sonar.sh[2339]: Starting SonarQube... Dec 02 13:55:34 ubuntu1804 sonar.sh[2339]: Started SonarQube. Dec 02 13:55:34 ubuntu1804 systemd[1]: Started SonarQube service.
Configure Apache for SonarQube
By default, SonarQube listens on port 9000. So, you will need to install and configure Apache as the reverse proxy to access the SonarQube using port 80.
To do so, install Apache with the following command:
sudo apt-get install apache2 -y
Next, enable mod_proxy module with the following command:
sudo a2enmod proxy
sudo a2enmod proxy_http
Next, create an Apache virtual host file for SonarQube with the following command:
sudo nano /etc/apache2/sites-available/sonar.conf
Add the following lines:
<VirtualHost *:80> ServerName example.com ServerAdmin [email protected] ProxyPreserveHost On ProxyPass / http://127.0.0.1:9000/ ProxyPassReverse / http://127.0.0.1:9000/ TransferLog /var/log/apache2/sonarm_access.log ErrorLog /var/log/apache2/sonar_error.log </VirtualHost>
Replace example.com with your own domain name. Save and close the file. Then, enable SonarQube virtual host file with the following command:
sudo a2ensite sonar
Finally, restart Apache and SonarQube service to apply all the changes with the following command:
sudo systemctl restart apache2
sudo systemctl restart sonar
By default, SonarQube stores their logs on /opt/sonarqube/logs directory. You can check SonarQube log with the following command:
sudo tail -f /opt/sonarqube/logs/sonar.log
Output:
Launching a JVM... Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. 2018.12.02 13:55:43 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp 2018.12.02 13:55:44 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001 2018.12.02 13:55:45 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/sonarqube/elasticsearch]: /opt/sonarqube/elasticsearch/bin/elasticsearch -Epath.conf=/opt/sonarqube/temp/conf/es 2018.12.02 13:55:45 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running 2018.12.02 13:55:48 INFO app[][o.e.p.PluginsService] no modules loaded 2018.12.02 13:55:48 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin] 2018.12.02 13:56:34 INFO app[][o.s.a.SchedulerImpl] Process[es] is up 2018.12.02 13:56:34 INFO app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/opt/sonarqube]: /usr/lib/jvm/java-8-oracle/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/sonarqube/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:./lib/server/*:/opt/sonarqube/lib/jdbc/postgresql/postgresql-42.2.1.jar org.sonar.server.app.WebServer /opt/sonarqube/temp/sq-process420500314195865484properties
You can also check SonarQube web log using the following command:
sudo tail -f /opt/sonarqube/logs/web.log
Output:
2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarJava / 4.15.0.12310 / 572454b93016ec73a53fe0e07b2ffdc356d21ba9 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarPHP / 2.11.0.2485 / 741861a29e5f9a26c6c99c06268facb6c4f4a882 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarPython / 1.8.0.1496 / 3fe3bc4d0273a5721ea2fb368dc45b1bb82fede3 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarQube :: Plugins :: SCM :: Git / 1.3.0.869 / 4da53e3f9e55f4f2e5796625cb0c5768ed152079 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarQube :: Plugins :: SCM :: SVN / 1.6.0.860 / 2111fdbd1dddda4ad6d4ed6486fd0b18c1010d3b 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarTS / 1.1.0.1079 / 042c9e65239a47d92d305f9767f730b3cc1e5ed3 2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarXML / 1.4.3.1027 / 39588245cecf538bb27be4e496ff303b0143d20b 2018.12.02 13:57:07 INFO web[][o.s.s.p.d.m.c.PostgresCharsetHandler] Verify that database charset supports UTF8 2018.12.02 13:57:09 INFO web[][o.s.s.p.w.MasterServletFilter] Initializing servlet filter [email protected] [pattern=UrlPattern{inclusions=[/api/system/migrate_db/*, ...], exclusions=[/api/properties*, ...]}] 2018.12.02 13:57:09 INFO web[][o.s.s.a.EmbeddedTomcat] HTTP connector enabled on port 9000 2018.12.02 13:57:16 INFO web[][o.s.s.p.UpdateCenterClient] Update center: https://update.sonarsource.org/update-center.properties (no proxy)
Access SonarQube
SonarQube is now installed and configured. It’s time to access it through web browser.
Open your web browser and type the URL http://example.com. You will be redirected to the following page:
Here, click on the Log In button. You should see the following page:
Provide the default administrator account username and password as admin / admin and click on the Log In button. You should see the SonarQube default dashboard in the following page:
Congratulations! you have successfully installed SonarQube on Ubuntu 18.04 server. You can now easily perform automatic reviews and check the health of an application using SonarQube.