{"id":7421,"date":"2018-12-06T20:06:48","date_gmt":"2018-12-06T17:06:48","guid":{"rendered":"https:\/\/www.howtoforge.com\/how-to-install-sonarqube-on-ubuntu-1804\/"},"modified":"2018-12-06T20:06:48","modified_gmt":"2018-12-06T17:06:48","slug":"how-to-install-sonarqube-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-sonarqube-on-ubuntu-18-04-lts\/","title":{"rendered":"How to Install SonarQube on Ubuntu 18.04 LTS"},"content":{"rendered":"<p>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.<\/p>\n<p>In this tutorial, we will learn how to install SonarQube on an Ubuntu 18.04 LTS (Bionic Beaver) server.<\/p>\n<h2 id=\"requirements\">Requirements<\/h2>\n<ul>\n<li>A server running Ubuntu 18.04.<\/li>\n<li>A non-root user with sudo privileges.<\/li>\n<\/ul>\n<h2 id=\"getting-started\">Getting Started<\/h2>\n<p>Before starting, you will need to update your system with the latest version. You can do this by running the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo apt-get update -y<br\/>sudo apt-get upgrade -y<\/code><\/pre>\n<p>Once your system is updated, restart the system to apply the changes.<\/p>\n<h2 id=\"install-java\">Install Java<\/h2>\n<p>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:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo add-apt-repository ppa:webupd8team\/java<\/code><\/pre>\n<p>Next, update the repository and install Java with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo apt-get update -y<br\/>sudo apt-get install oracle-java8-installer -y<\/code><\/pre>\n<p>Once the Java is installed, check the Java version using the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">java -version<\/code><\/pre>\n<p>Output:<\/p>\n<pre>openjdk version \"10.0.2\" 2018-07-17&#13;\nOpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3)&#13;\nOpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3, mixed mode)&#13;\n<\/pre>\n<h2 id=\"install-and-configure-postgresql\">Install and Configure PostgreSQL<\/h2>\n<p>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.<\/p>\n<p>You can do this with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo sh -c 'echo \"deb http:\/\/apt.postgresql.org\/pub\/repos\/apt\/ `lsb_release -cs`-pgdg main\" &gt;&gt; \/etc\/apt\/sources.list.d\/pgdg.list'<br\/>wget -q https:\/\/www.postgresql.org\/media\/keys\/ACCC4CF8.asc -O - | sudo apt-key add -<\/code><\/pre>\n<p>Next, update the repository and install PostgreSQL with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo apt-get update -y<br\/>sudo apt-get install postgresql postgresql-contrib<\/code><\/pre>\n<p>Once the installation is completed, check the status of PostgreSQL with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo systemctl status postgresql<\/code><\/pre>\n<p>Output:<\/p>\n<pre>? postgresql.service - PostgreSQL RDBMS&#13; Loaded: loaded (\/lib\/systemd\/system\/postgresql.service; enabled; vendor preset: enabled)&#13; Active: active (exited) since Sun 2018-12-02 08:49:29 UTC; 4h 30min ago&#13; Process: 1295 ExecStart=\/bin\/true (code=exited, status=0\/SUCCESS)&#13; Main PID: 1295 (code=exited, status=0\/SUCCESS)&#13;\n&#13;\nDec 02 08:49:29 ubuntu1804 systemd[1]: Starting PostgreSQL RDBMS...&#13;\nDec 02 08:49:29 ubuntu1804 systemd[1]: Started PostgreSQL RDBMS.&#13;\n<\/pre>\n<p>Next, switch to the postgres user with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">su - postgres<\/code><\/pre>\n<p>Next, create a sonar user with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">createuser sonar<\/code><\/pre>\n<p>Next, switch to the PostgreSQL shell with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">psql<\/code><\/pre>\n<p>Next, set password for sonar user and create a sonar database with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">ALTER USER sonar WITH ENCRYPTED password 'password';<br\/>CREATE DATABASE sonar OWNER sonar;<\/code><\/pre>\n<p>Next, exti from the PostgreSQL shell:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">\\q<\/code><\/pre>\n<h2 id=\"install-and-configure-sonarqube\">Install and Configure SonarQube<\/h2>\n<p>First, create a user for SonarQube with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo adduser sonar<\/code><\/pre>\n<p>Next, download the latest version of SonarQube with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">wget https:\/\/binaries.sonarsource.com\/Distribution\/sonarqube\/sonarqube-6.7.6.zip<\/code><\/pre>\n<p>Once the download is completed, unzip the downloaded file with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">unzip sonarqube-6.7.6.zip<\/code><\/pre>\n<p>Next, copy the extracted directory to the \/opt with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo cp -r sonarqube-6.7.6 \/opt\/sonarqube<\/code><\/pre>\n<p>Next, give ownership to the sonar user with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo chown -R sonar:sonar \/opt\/sonarqube<\/code><\/pre>\n<p>Next, you will need to configure SonarQube to run as a sonar user. You can do this with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo nano \/opt\/sonarqube\/bin\/linux-x86-64\/sonar.sh<\/code><\/pre>\n<p>Make the following changes:<\/p>\n<pre>RUN_AS_USER=sonar&#13;\n<\/pre>\n<p>Save and close the file. Then, open SonarQube default configuration file and modify the database credentials with the one we created earlier:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo nano \/opt\/sonarqube\/conf\/sonar.properties<\/code><\/pre>\n<p>Make the following changes:<\/p>\n<pre>sonar.jdbc.username=sonar&#13;\nsonar.jdbc.password=password&#13;\nsonar.jdbc.url=jdbc:postgresql:\/\/localhost\/sonar&#13;\nsonar.web.host=127.0.0.1&#13;\nsonar.search.javaOpts=-Xms512m -Xmx512m&#13;\n<\/pre>\n<p>Save and close the file, when you are finished.<\/p>\n<h2 id=\"create-systemd-service-file-for-sonarqube\">Create Systemd Service file for SonarQube<\/h2>\n<p>Next, you will need to create a systemd service file to manage SonarQube service. You can do this with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo nano \/etc\/systemd\/system\/sonar.service<\/code><\/pre>\n<p>Add the following lines:<\/p>\n<pre>[Unit]&#13;\nDescription=SonarQube service&#13;\nAfter=syslog.target network.target&#13;\n&#13;\n[Service]&#13;\nType=forking&#13;\n&#13;\nExecStart=\/opt\/sonarqube\/bin\/linux-x86-64\/sonar.sh start&#13;\nExecStop=\/opt\/sonarqube\/bin\/linux-x86-64\/sonar.sh stop&#13;\n&#13;\nUser=sonar&#13;\nGroup=sonar&#13;\nRestart=always&#13;\n&#13;\n[Install]&#13;\nWantedBy=multi-user.target&#13;\n<\/pre>\n<p>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:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo systemctl start sonar<br\/>sudo systemctl enable sonar<\/code><\/pre>\n<p>You can check the status of SonarQube service with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo systemctl status sonar<\/code><\/pre>\n<p>Output:<\/p>\n<pre>? sonar.service - SonarQube service&#13; Loaded: loaded (\/etc\/systemd\/system\/sonar.service; disabled; vendor preset: enabled)&#13; Active: active (running) since Sun 2018-12-02 13:55:34 UTC; 2min 52s ago&#13; Process: 2339 ExecStart=\/opt\/sonarqube\/bin\/linux-x86-64\/sonar.sh start (code=exited, status=0\/SUCCESS)&#13; Main PID: 2396 (wrapper)&#13; Tasks: 133 (limit: 2323)&#13; CGroup: \/system.slice\/sonar.service&#13; ??2396 \/opt\/sonarqube\/bin\/linux-x86-64\/.\/wrapper \/opt\/sonarqube\/bin\/linux-x86-64\/..\/..\/conf\/wrapper.conf wrapper.syslog.ident=SonarQ&#13; ??2399 java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.path=.\/lib -classpath ..\/..\/lib\/jsw\/wrapper-&#13; ??2445 \/usr\/lib\/jvm\/java-8-oracle\/jre\/bin\/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOc&#13; ??2545 \/usr\/lib\/jvm\/java-8-oracle\/jre\/bin\/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=\/opt\/sonarqube\/temp -&#13; ??2622 \/usr\/lib\/jvm\/java-8-oracle\/jre\/bin\/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=\/opt\/sonarqube\/temp -&#13;\n&#13;\nDec 02 13:55:33 ubuntu1804 systemd[1]: Starting SonarQube service...&#13;\nDec 02 13:55:33 ubuntu1804 sonar.sh[2339]: Starting SonarQube...&#13;\nDec 02 13:55:34 ubuntu1804 sonar.sh[2339]: Started SonarQube.&#13;\nDec 02 13:55:34 ubuntu1804 systemd[1]: Started SonarQube service.&#13;\n<\/pre>\n<h2 id=\"configure-apache-for-sonarqube\">Configure Apache for SonarQube<\/h2>\n<p>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.<\/p>\n<p>To do so, install Apache with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo apt-get install apache2 -y<\/code><\/pre>\n<p>Next, enable mod_proxy module with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo a2enmod proxy<br\/>sudo a2enmod proxy_http<\/code><\/pre>\n<p>Next, create an Apache virtual host file for SonarQube with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo nano \/etc\/apache2\/sites-available\/sonar.conf<\/code><\/pre>\n<p>Add the following lines:<\/p>\n<pre>&lt;VirtualHost *:80&gt;&#13; ServerName example.com&#13; ServerAdmin <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"f49590999d9ab4918c9599849891da979b99\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>&#13; ProxyPreserveHost On&#13; ProxyPass \/ http:\/\/127.0.0.1:9000\/&#13; ProxyPassReverse \/ http:\/\/127.0.0.1:9000\/&#13; TransferLog \/var\/log\/apache2\/sonarm_access.log&#13; ErrorLog \/var\/log\/apache2\/sonar_error.log&#13;\n&lt;\/VirtualHost&gt;&#13;\n<\/pre>\n<p>Replace example.com with your own domain name. Save and close the file. Then, enable SonarQube virtual host file with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo a2ensite sonar<\/code><\/pre>\n<p>Finally, restart Apache and SonarQube service to apply all the changes with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo systemctl restart apache2<br\/>sudo systemctl restart sonar<\/code><\/pre>\n<p>By default, SonarQube stores their logs on \/opt\/sonarqube\/logs directory. You can check SonarQube log with the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo tail -f \/opt\/sonarqube\/logs\/sonar.log<\/code><\/pre>\n<p>Output:<\/p>\n<pre>Launching a JVM...&#13;\nWrapper (Version 3.2.3) http:\/\/wrapper.tanukisoftware.org&#13; Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.&#13;\n&#13;\n2018.12.02 13:55:43 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory \/opt\/sonarqube\/temp&#13;\n2018.12.02 13:55:44 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on \/127.0.0.1:9001&#13;\n2018.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&#13;\n2018.12.02 13:55:45 INFO app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running&#13;\n2018.12.02 13:55:48 INFO app[][o.e.p.PluginsService] no modules loaded&#13;\n2018.12.02 13:55:48 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]&#13;\n2018.12.02 13:56:34 INFO app[][o.s.a.SchedulerImpl] Process[es] is up&#13;\n2018.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&#13;\n<\/pre>\n<p>You can also check SonarQube web log using the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">sudo tail -f \/opt\/sonarqube\/logs\/web.log<\/code><\/pre>\n<p>Output:<\/p>\n<pre>2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarJava \/ 4.15.0.12310 \/ 572454b93016ec73a53fe0e07b2ffdc356d21ba9&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarPHP \/ 2.11.0.2485 \/ 741861a29e5f9a26c6c99c06268facb6c4f4a882&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarPython \/ 1.8.0.1496 \/ 3fe3bc4d0273a5721ea2fb368dc45b1bb82fede3&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarQube :: Plugins :: SCM :: Git \/ 1.3.0.869 \/ 4da53e3f9e55f4f2e5796625cb0c5768ed152079&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarQube :: Plugins :: SCM :: SVN \/ 1.6.0.860 \/ 2111fdbd1dddda4ad6d4ed6486fd0b18c1010d3b&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarTS \/ 1.1.0.1079 \/ 042c9e65239a47d92d305f9767f730b3cc1e5ed3&#13;\n2018.12.02 13:57:03 INFO web[][o.s.s.p.ServerPluginRepository] Deploy plugin SonarXML \/ 1.4.3.1027 \/ 39588245cecf538bb27be4e496ff303b0143d20b&#13;\n2018.12.02 13:57:07 INFO web[][o.s.s.p.d.m.c.PostgresCharsetHandler] Verify that database charset supports UTF8&#13;\n2018.12.02 13:57:09 INFO web[][o.s.s.p.w.MasterServletFilter] Initializing servlet filter <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"a8c7dacf86dbc7c6c9da86dbcddadecdda86dfdb86ffcdcafbcddadec1cbcdeec1c4dccddae89f919acd9e9f9f99\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a> [pattern=UrlPattern{inclusions=[\/api\/system\/migrate_db\/*, ...], exclusions=[\/api\/properties*, ...]}]&#13;\n2018.12.02 13:57:09 INFO web[][o.s.s.a.EmbeddedTomcat] HTTP connector enabled on port 9000&#13;\n2018.12.02 13:57:16 INFO web[][o.s.s.p.UpdateCenterClient] Update center: https:\/\/update.sonarsource.org\/update-center.properties (no proxy)&#13;\n<\/pre>\n<h2 id=\"access-sonarqube\">Access SonarQube<\/h2>\n<p>SonarQube is now installed and configured. It&#8217;s time to access it through web browser.<\/p>\n<p>Open your web browser and type the URL http:\/\/example.com. You will be redirected to the following page:<\/p>\n<p><a class=\"fancybox\" id=\"img-page1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_sonarqube_on_ubuntu_1804\/big\/page1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/12\/how-to-install-sonarqube-on-ubuntu-18-04-lts.png\" alt=\"SonarQube\" width=\"550\" height=\"289\" title=\"\"><\/a><\/p>\n<p>Here, click on the <strong>Log<\/strong> <strong>In<\/strong> button. You should see the following page:<\/p>\n<p><a class=\"fancybox\" id=\"img-page2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_sonarqube_on_ubuntu_1804\/big\/page2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/12\/how-to-install-sonarqube-on-ubuntu-18-04-lts-1.png\" alt=\"Log In\" width=\"550\" height=\"288\" title=\"\"><\/a><\/p>\n<p>Provide the default administrator account username and password as admin \/ admin and click on the <strong>Log<\/strong> <strong>In<\/strong> button. You should see the SonarQube default dashboard in the following page:<\/p>\n<p><a class=\"fancybox\" id=\"img-page3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_sonarqube_on_ubuntu_1804\/big\/page3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/12\/how-to-install-sonarqube-on-ubuntu-18-04-lts-2.png\" alt=\"SonarCube Dashboard\" width=\"550\" height=\"291\" title=\"\"><\/a><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7421","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7421"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7421\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}