How to Install SonarQube on windows
SonarQube can be used as a stand alone code analysis and quality management tool. It can also be integrated with the commonly used DevOps orchestration tools like Jenkins, GitHub etc.
In this post, we will learn to install SonarQube on Windows OS. Then we will run SonarQube from windows command line and also as a windows service.
We will install the community version of SonarQube version 9 on a local system while using the default H2 database provided by SonarQube.
Towards the end of the post we will go through few tips for troubleshooting and some common issues faced while running SonarQube.
Below is the sequence in which we will proceed. Feel free to jump to any sub topic of your interest.
- Pre-requisites for Installing SonarQube
- Installing SonarQube
- Start SonarQube
- Tips for Troubleshooting
Before we start let us go through the basic minimum pre-requisites for installing and running latest version of sonarqube.
- At a minimum you will require JRE11(Oracle JRE11 or OpenJDK11). The latest version of SonarQube will not work on any of the previous Java versions as per the official documentation.
- Also you will need make sure you have sufficient RAM for proper functioning and analysis using SonarQube. The recommended memory requirement is 2GB at the SonarQube docs website as of date to run SonarQube server.
Installing SonarQube on windows
Now let us dive straight away into the main topic of installing, setting up and running SonarQube.
Download SonarQube zip file
The first step here is to download the zip file from their official website and extract it in the desired location. To do this follow the steps as mentioned:
- Go to SonarQube Official documentation at docs.sonarqube.org
- Select Try Out SonarQube from the left menu
- Go to section Installing a local instance of SonarQube.
- Download the zip file
- Extract zip file in a folder where you want to setup SonarQube.
For purpose of demonstration, the folder where I have extracted the zip file is C:/Softwares. You can extract it to a folder of your choice.
Setup Environment Variables for SonarQube
The next step is to configure SonarQube on your system.
Go to system environment variables by clicking on Start Menu and then select Search from the menu and type environment variables in the search box. Then select the option which says “Edit the the system environment variables”. It will take you to the Advanced tab of the system properties dialog window. You can select the button Environment Variables from here.
Alternatively, you can reach this screen via Control Panel –> System and Security –> System –> Advanced system settings. Then select the button Environment Variables from the Advanced tab.
Create an environment variable “SONARQUBE_HOME” pointing to the base directory of SonarQube installation (which contains the bin folder).
In our case the value for SONAR_HOME becomes C:/Softwares/sonarqube as we have extracted the zip file to C:/Softwares.
Next open the PATH environment variable and make a new entry SONAR_HOME.
SONAR_HOME is the same SonarQube environment variable that we have created above. We could also mention the complete path here as well, however, it is recommended to maintain the hard coded folder location at just place i.e. SONAR_HOME environment variable.
We are now all set to move onto the next step where we will start running SonarQube.
Now we have reached the stage where we should be able to start SonarQube.
There are 2 ways to do that –
- Start SonarQube from command line everytime we wish to use it
- Create SonarQube windows service
Let us discuss each of these options below.
Start SonarQube from command line
Open a command prompt by typing cmd in the Search search tool or just going to Command prompt from the command menu. Right click to select open in administrator mode.
Next step is to start SonarQube from StartSonar.bat file on the command line .
Here we will start a new command window and go to the folder where SonarQube is installed.
The command prompt will pick the path to SonarQube home folder from environment variable SONAR_HOME. The complete value in our case becomes
We need to run the batch file StartSonar.bat located on this folder to start SonarQube. To run the file we will simply type StartSonar at the command prompt as follows.
This process will first run elasticsearch and then run start SonarQube.
The default port for SonarQube is 9000. We will open the SonarQube in a web browser at this port, in our case it is http://localhost:9000
We should be able to view the SonarQube home page in the browser.
When you need to stop SonarQube, you will need to press Ctrl+c in the same command window.
In case you are not able to run SonarQube from command line, you can go through the section for guidance on troubleshooting.
When do we need to create SonarQube windows service?
When you want to have the SonarQube running all the time on the system, you should create a windows service. For example, when you are working in a CI (continuous integration) mode or just doing development and testing the code side by side. Windows service removes the botheration for starting the tools before performing any operation.
Also if you plan to use SonarQube as a continuous integration tool on your local system, then the recommended way to use SonarQube is as a service with Automatic service setting, so that you do not need to start the service every time you restart the system. This setting is shown in detail in the next section.
However, do keep in mind that your system should have recommended memory for SonarQube to run efficiently as a windows service (refer Prerequites section above) while performing your regular activities on the system/vm.
How to create SonarQube windows service
To run SonarQube server as a windows service, we will run the following script available at docs.sonarqube.org.
sc create SonarQube binPath= “\”%SONAR_HOME%\bin\windows-x86-64\wrapper.exe\” -s \”%SONAR_HOME%\conf\wrapper.conf\””
Let us first understand the purpose of the above script.
sc create command is a command used to create service on windows platform from file wrapper.exe (available in the bin folder) . The file wrapper.conf (available in the conf folder) is the configuration file which contains the default options that will be set on creation of the service.
Once the service is created, it can be viewed by opening the services view with the command services.msc or by selecting from the Start menu Windows Administrative Tools –> Services.
You can locate the service SonarQube (default name for the service) in the Services console.
When you double click on SonarQube, a new window opens where you can edit the properties. Here we will select the Startup type as Automatic, so that we don’t have to start the service everytime the system/vm gets restarted. Then click on Start button to start the service and apply the changes and close.
Now you are all set to start using SonarQube as a windows service. You should be able to view SonarQube in your browser. For this, open your browser and in the url section, provide the IP add of the system/vm where SonarQube is installed along with port 9000 which is the default port for SonarQube. In our case we are using SonarQube on our local system, hence we will type the url as http://localhost:9000/ in the browser to start using SonarQube.
We should be able to view the SonarQube home page.
In case you are not able to perform the above step, you can go through the next section for guidance on troubleshooting.
Tips for Troubleshooting
As you are going through this section, you might have come across an error while starting installing or running SonarQube. If this is the case then you it might be helpful to go through the tips provided here.
The error in most cases will just be an indication that things are things are not working as expected, but may not always provide the exact details of the issue. You might see a similar error for multiple issues. Hence it is always helpful to go through the logs, to find out the exact issue that you want to fix.
Now you may them want to go through the troubleshooting tips which provides resolution steps for few most common issues.
Java version mismatch
You will get an error if you are using a java version incompatible with SonarQube latest version. You could see an error like the following.
Could not create the Java Virtual Machine.
Windows could not start the SonarQube service on Local Computer. Error 1067: The process terminated unexpectedly.
In the case of java version mismatch, you should check the current Oracle JRE or OpenJDK version on your system. You can do this by typing java -version in the command prompt.
The latest version of SonarQube works with java version 11 and most of the later versions. However, it is always a good idea to check for the java version compatible with your version of SonarQube at the official SonarQube documentation at sonarqube.docs.org. If your installed java version is incompatible, then you need to re-install jdk/jre. If you have more than one version of java installed on your system, you can just change the default java version on your system.
How to work with multiple versions of java on your system
If you want to keep multiple versions of jdk/jre on your system, you should follow one of the steps below.
Change default jdk/jre version
In this section we will see how to change the default jdk/jre version on the system.
There could be scenario where you would need different jdk versions for working with different applications. If you want to retain the earlier jdk version and have installed a new jdk/jre version for SonarQube, then you need to do the following for setting the default jdk/jre version for SonarQube.
Create a new entry for the jdk/jre version being used for SonarQube in the PATH system variable.
Now move the new entry created for SonarQube above the entry of jdk/jre with the earlier version.
2. If the above does not work by, then also check if there is any entry for javapath like the following in the PATH environment variable:
C:\Program Files\Common Files\Oracle\java\javapath
The above javapath contains java.exe for the version that was installed earlier. You may want to delete this entry from the PATH variable to enable the new version of java to be used.
Change java version only for SonarQube
In case you have more than one version of java running on your system, you can use a specific version of java just for SonarQube. This can be achieved by changing wrapper.conf file available in the main SonarQube folder (SONAR_HOME).
All we need to do is to uncomment the following property and provide the complete path to the java executable.
wrapper.java.command = <path to java>
Out of Memory Error
As mentioned in the pre-requisites section, you should have a decent amount of RAM on your system, otherwise SonarQube may fail to start. If the logs indicate memory unavailability or out of memory error, then you can check for the available RAM on your system for usage by SonarQube. You might also want to check by increasing the java heap size.
Java heap size
Following are few of the error messages that can be seen related to heap size.
Error occurred during initialization of VM jvm 1 | Could not reserve enough space for object heap Error occurred during initialization of VM jvm 1 | Too small initial heap java.lang.OutOfMemoryError: Java heap space
If the out of memory error you are getting mentions low heap size, then increasing the heap size will free up memory. This can be done by editing value of javaOpts in the sonar.properties file. The value of Initial heap size ( -Xms ) and the maximum heap size ( -Xmx ) can be increased depending on the error. The value can be edited on the basis of where we are seeing the error i.e. Elasticsearch, compute engine or SonarQube web.
The heap size that you should provide should keep enough memory for performance of other on the system. This value would vary based largely on the processor type(32 or 64 bit), total memory and the activities performed on the system. Generally speaking, you would leave at least 30 to 40 percent of memory for performing other system activities.
Following are the default heap sizes for SonarQube mentioned in the sonar.properties file.
WEB SERVER #sonar.web.javaOpts=-Xmx512m -Xms128m COMPUTE ENGINE #sonar.ce.javaOpts=-Xmx512m -Xms128m ELASTICSEARCH #sonar.search.javaOpts=-Xmx512m -Xms512m
Default Port is not available
In case the default port number for SonarQube i.e. port 9000 or elasticsearch i.e. port 9001 is already in use, check for the other available port numbers.
Then all you need to do is to change the value for “sonar.web.port” in sonar.properties file. Uncomment this and provide the available port number as its value.
sonar.web.port= <available port number>
Similarly for elasticsearch, you can uncomment the following property and provide the new available port number.
sonar.search.port= <available port number>
Localsystem user access
The user with which you are creating the service or with which you are trying to start SonarQube from command line may not have the access to do so.
Access is denied.
Windows could not start the SonarQube service on Local Computer.
Error 1067: The process terminated unexpectedly.
You might want to get the permissions assigned to your local user.
In the case of windows service, if you want to access the service with different credentials, you can open Service Properties dialog box by double clicking on the service name. Then go to login tab and enter the new user credentials.
Failed to obtain node locks
You might find a java error in the logs that says
java.lang.IllegalStateException: failed to obtain node locks, tried [[…\sonarqube\data\es7]] with lock id ; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was )?
As SonarQube internally uses elasticsearch, a possible resolution in this case is to kill the processes from Task Manager in which we have started running SonarQube. This process could be a process directly running SonarQube. This could also be another process which has started SonarQube, for example a java process running a maven project which is calling SonarQube.
This post explains the steps for installation of SonarQube on a local system using the default H2 database.
This is followed by troubleshooting guidance with tips provided to resolve the most common issues faced during SonarQube installation on windows platform.