{"id":54702,"date":"2022-02-22T23:58:15","date_gmt":"2022-02-22T18:28:15","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54702"},"modified":"2022-02-22T23:58:15","modified_gmt":"2022-02-22T18:28:15","slug":"how-to-setup-sonarqube","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-setup-sonarqube\/","title":{"rendered":"How to setup SonarQube"},"content":{"rendered":"<p><b>What is SonarQube?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">SonarQube is the code inspection tool that analyses and covers the code and generates reports for the areas that need to be improved to keep the code as clean as possible. Some of the advantages of it&#8217;s usage includes:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Catch tricky bugs to prevent undefined behaviour from impacting end-users<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Fix vulnerabilities that compromise your app, and learn AppSec along the way with Security Hotspots<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Make sure your codebase is clean and maintainable, to increase developer velocity<\/span><\/li>\n<li>Covers <a href=\"https:\/\/www.sonarqube.org\/features\/multi-languages\">various<\/a> languages<\/li>\n<\/ol>\n<p><b>Pre-requisites to using SonarQube<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Docker <\/b><span style=\"font-weight: 400;\">(<\/span><a href=\"https:\/\/docs.docker.com\/get-docker\/\"><span style=\"font-weight: 400;\">click<\/span><\/a><span style=\"font-weight: 400;\"> here to install)<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Docker compose <\/b><span style=\"font-weight: 400;\">(<\/span><a href=\"https:\/\/docs.docker.com\/compose\/install\/\"><span style=\"font-weight: 400;\">click<\/span><\/a><span style=\"font-weight: 400;\"> here to install)<\/span><\/li>\n<\/ol>\n<p><b>Setting up SonarQube<\/b><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Install sonarqube using docker (create a new directory with sonarqube name and copy below docker-compose.yml into it)<br \/>\n<\/span><\/p>\n<pre>version: \"3\"\r\n<i>services:<\/i><i>\u00a0\u00a0sonarqube:<\/i><i>\u00a0\u00a0\u00a0\u00a0image: sonarqube<\/i>\r\n<i> \u00a0\u00a0\u00a0expose:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- 9000<\/i>\r\n<i> \u00a0\u00a0\u00a0ports:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- \"9000:9000\"<\/i>\r\n<i> \u00a0\u00a0\u00a0networks:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarnetwork<\/i>\r\n<i> \u00a0\u00a0\u00a0environment:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- SONARQUBE_JDBC_URL=jdbc:postgresql:\/\/db:5432\/sonar<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- SONARQUBE_JDBC_USERNAME=sonar<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- SONARQUBE_JDBC_PASSWORD=sonar<\/i>\r\n<i> \u00a0\u00a0\u00a0volumes:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarqube_conf:\/opt\/sonarqube\/conf<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarqube_data:\/opt\/sonarqube\/data<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarqube_extensions:\/opt\/sonarqube\/extensions<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarqube_bundled-plugins:\/opt\/sonarqube\/lib\/bundled-plugins<\/i>\r\n<i> \u00a0db:<\/i>\r\n<i> \u00a0\u00a0\u00a0image: postgres<\/i>\r\n<i> \u00a0\u00a0\u00a0networks:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- sonarnetwork<\/i>\r\n<i> \u00a0\u00a0\u00a0environment:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- POSTGRES_USER=sonar<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- POSTGRES_PASSWORD=sonar<\/i>\r\n<i> \u00a0\u00a0\u00a0volumes:<\/i>\r\n<i> \u00a0\u00a0\u00a0\u00a0\u00a0- postgresql:\/var\/lib\/postgresql<\/i>\r\n<i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0- postgresql_data:\/var\/lib\/postgresql\/data<\/i>\r\n<i>networks:<\/i>\r\n<i>\u00a0\u00a0sonarnetwork:<\/i>\r\n<i>volumes:<\/i>\r\n<i> \u00a0sonarqube_conf:<\/i>\r\n<i> \u00a0sonarqube_data:<\/i>\r\n<i> \u00a0sonarqube_extensions:<\/i>\r\n<i> \u00a0sonarqube_bundled-plugins:<\/i>\r\n<i> \u00a0postgresql:<\/i>\r\n<i> \u00a0postgresql_data:<\/i><\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Now run <\/span><b><i>sudo<\/i><\/b> <b><i>docker-compose up<\/i><\/b> <span style=\"font-weight: 400;\">command (this command will turn up sonarqube)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Now install sonar-scanner<br \/>\n<b><\/b><\/span><b><i>wget https:\/\/binaries.sonarsource.com\/Distribution\/sonar-scanner-cli\/sonar-scanner-cli-4.2.0.1873-linux.zip<br \/>\n<\/i><\/b><b><i>unzip sonar-scanner-cli-4.2.0.1873-linux.zip<\/i><\/b><b><i>sudo mv sonar-scanner-4.2.0.1873-linux \/opt\/sonar-scanner<\/i><\/b><\/li>\n<li><span style=\"font-weight: 400;\">Edit the sonar-scanner.properties file.<br \/>\n<\/span><b>sudo<\/b> <b><i>vi \/opt\/sonar-scanner\/conf\/sonar-scanner.properties<\/i><\/b><\/li>\n<li><span style=\"font-weight: 400;\">Configure the SonarQube scanner to connect to your SonarQube server.<br \/>\n<\/span><b><i>sonar.host.url=http:\/\/localhost:9000<\/i><\/b><b><i>sonar.sourceEncoding=UTF-8<\/i><\/b><\/li>\n<li><span style=\"font-weight: 400;\">We need to add the sonar-scanner command to the PATH variable. <\/span>Let\u2019s create a file to automate the required environment variables configuration.<br \/>\n<b>sudo nano \/etc\/profile.d\/sonar-scanner.sh<\/b><span style=\"font-weight: 400;\">Here is the sonar-scanner.sh file content.<\/span><b><i>#\/bin\/bash<\/i><\/b><b><i>export PATH=&#8221;$PATH:\/opt\/sonar-scanner\/bin&#8221;<\/i><\/b><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Reboot your computer or use the source command to add the sonar scanner command to the PATH variable.<br \/>\n<\/span><b><i>reboot<\/i><\/b><b><i>source \/etc\/profile.d\/sonar-scanner.sh<\/i><\/b><\/li>\n<li><span style=\"font-weight: 400;\">Use the following command to verify if the PATH variable was changed as expected.<br \/>\n<\/span><b><i>env | grep PATH<\/i><\/b><span style=\"font-weight: 400;\">Here is the command output:<\/span><b><i>PATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games:\/snap\/bin:\/opt\/sonar-scanner\/bin<\/i><\/b><span style=\"font-weight: 400;\">In our example, the \/opt\/sonar-scanner\/bin directory was added to the PATH variable.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Use the following to verify the SonarQube scanner version installed<br \/>\n<\/span><b><i>sonar-scanner -v<\/i><\/b><span style=\"font-weight: 400;\">Here is the command output.<\/span><b><i>INFO: Scanner configuration file: \/opt\/sonar-scanner\/conf\/sonar-scanner.properties<\/i><\/b><b><i>INFO: Project root configuration file: NONE<\/i><\/b><b><i>INFO: SonarQube Scanner 4.2.0.1873<\/i><\/b><b><i>INFO: Java 11.0.3 AdoptOpenJDK (64-bit)<\/i><\/b><b><i>INFO: Linux 5.3.0-18-generic amd64<\/i><\/b><\/li>\n<\/ol>\n<h2><b>SonarQube &#8211; How to Scan project Code<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">First, you need to access the SonarQube web interface and create a new project. <\/span><span style=\"font-weight: 400;\">Open your browser and enter the IP address of your web server plus: 9000. <\/span><span style=\"font-weight: 400;\">In our example, the following URL was entered in the Browser:<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/localhost:9000\/\"><span style=\"font-weight: 400;\">http:\/\/localhost:9000\/<\/span><\/a><span style=\"font-weight: 400;\">(in case if you are facing memory problem then need to increase it &#8211; see &#8211; <\/span><a href=\"https:\/\/stackoverflow.com\/questions\/51445846\/elasticsearch-max-virtual-memory-areas-vm-max-map-co\"><span style=\"font-weight: 400;\">https:\/\/stackoverflow.com\/questions\/51445846\/elasticsearch-max-virtual-memory-areas-vm-max-map-co<\/span><\/a><span style=\"font-weight: 400;\">)<br \/>\n<\/span><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54697 size-medium\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-54-07-300x195.png\" alt=\"\" width=\"300\" height=\"195\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-54-07-300x195.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-54-07.png 381w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Click on the Login button and use the SonarQube default username and password.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"> Default Username: admin<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Default Password: admin<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">After a successful login, you will be sent to the SonarQube Dashboard.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Create a new project.(e.g. GIH)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Project Key &#8211; GIH<br \/>\n<\/span><span style=\"font-weight: 400;\">Display name &#8211; GIH<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54698 size-medium\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-57-37-300x210.png\" alt=\"\" width=\"300\" height=\"210\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-57-37-300x210.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-13-57-37.png 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Enter a string for the project token name and click on the Generate button. <\/span><span style=\"font-weight: 400;\">The token is used to identify you when an analysis is performed. <\/span><span style=\"font-weight: 400;\">Click on the Continue button.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54699 size-medium\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-38-300x173.png\" alt=\"\" width=\"300\" height=\"173\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-38-300x173.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-38-624x359.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-38.png 629w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">On the Next screen, select your project language. <\/span><span style=\"font-weight: 400;\">In our example, we selected the option: Other (JS, TS, Go, Python, PHP, &#8230;)<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54700 size-medium\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-39-300x88.png\" alt=\"\" width=\"300\" height=\"88\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-39-300x88.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-02-39.png 426w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Select the Linux operating system. <\/span><span style=\"font-weight: 400;\">The system will show you the command-line that you should use to scan the GIH project.<\/span><\/p>\n<p><b><i>sonar-scanner \\<\/i><\/b><\/p>\n<p><b><i>\u00a0<\/i><\/b><b><i>\u00a0-Dsonar.projectKey=GIH \\<\/i><\/b><\/p>\n<p><b><i>\u00a0<\/i><\/b><b><i>\u00a0-Dsonar.sources=. \\<\/i><\/b><\/p>\n<p><b><i>\u00a0<\/i><\/b><b><i>\u00a0-Dsonar.host.url=http:\/\/192.168.15.15:9000 \\<\/i><\/b><\/p>\n<p><b><i>\u00a0<\/i><\/b><b><i>\u00a0-Dsonar.login=<\/i><\/b><b>9ecdeb28268d24f0b08da716ae67f72a8a530392<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In adobe command you can add an exclusion param as some directory like vendor or node_modules generate automatically when we run composer or npm so we can exclude these directory for scan. After add exclusion param above command will look like this<\/span><\/p>\n<p><b>sonar-scanner \u00a0 -Dsonar.projectKey=GIH \u00a0 -Dsonar.sources=. \u00a0 -Dsonar.host.url=http:\/\/localhost:9000 \u00a0 -Dsonar.login=9ecdeb28268d24f0b08da716ae67f72a8a530392 -Dsonar.exclusions=**\/vendor\/**,**\/node_modules\/**,**\/core\/**,**\/sites\/**,**\/contrib\/**,**\/drush\/**,**\/files\/**,**\/default* ,**\/libraries\/**<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Note &#8211; every time you have to run the above command inside your project root to see SonarQube dashboard. <\/span><span style=\"font-weight: 400;\">Once you run above command by terminal you will get a url of sonarqube dashboard<\/span><\/p>\n<p><span style=\"font-weight: 400;\">E.g ANALYSIS SUCCESSFUL, you can browse <\/span><a href=\"http:\/\/localhost:9000\/dashboard?id=GIH\"><span style=\"font-weight: 400;\">http:\/\/localhost:9000\/dashboard?id=GIH<\/span><\/a><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54701 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-07-08.png\" alt=\"\" width=\"652\" height=\"778\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-07-08.png 652w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-07-08-251x300.png 251w, \/blog\/wp-ttn-blog\/uploads\/2022\/02\/Screenshot-from-2022-02-14-14-07-08-624x745.png 624w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>What is SonarQube? SonarQube is the code inspection tool that analyses and covers the code and generates reports for the areas that need to be improved to keep the code as clean as possible. Some of the advantages of it&#8217;s usage includes: Catch tricky bugs to prevent undefined behaviour from impacting end-users Fix vulnerabilities that [&hellip;]<\/p>\n","protected":false},"author":1428,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":118},"categories":[2348,3602,1994],"tags":[4057],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54702"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1428"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54702"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54702\/revisions"}],"predecessor-version":[{"id":54720,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54702\/revisions\/54720"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}