# Install the server

## Overview <a href="#overview" id="overview"></a>

This section describes a single-node SonarQube instance. For details on clustered setup, see [install-the-server-as-a-cluster](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/install-the-server-as-a-cluster "mention").

### Instance components <a href="#instance-components" id="instance-components"></a>

A SonarQube instance comprises three components:

![SQ instance components](https://4108688904-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG0n8jfxwZN8LmyH08dMa%2Fuploads%2Fgit-blob-be7aaeb59f6c55b97c8e479f4e91255395447bb1%2F32797b61e4af27a9c9fd0fbb71c1773d37a85aad.png?alt=media)

SQ instance components

1. The SonarQube server running the following processes:
   * A web server that serves the SonarQube user interface.
   * A search server based on Elasticsearch.
   * The compute engine in charge of processing code analysis reports and saving them in the SonarQube database.
2. The database to store the following:
   * Metrics and issues for code quality and security generated during code scans.
   * The SonarQube instance configuration.
3. One or more scanners running on your build or continuous integration servers to analyze projects.

### Hosts and locations <a href="#hosts-and-locations" id="hosts-and-locations"></a>

For optimal performance, the SonarQube server and database should be installed on separate hosts, and the server host should be dedicated. The server and database hosts should be located on the same network.

All hosts must be time-synchronized.

## Installing the database <a href="#installing-the-database" id="installing-the-database"></a>

Several [prerequisites-and-overview](https://docs.sonarsource.com/sonarqube-server/8.9/requirements/prerequisites-and-overview "mention") are supported. Be sure to follow the requirements listed for your database. They are real requirements not recommendations.

Create an empty schema and a `sonarqube` user. Grant this `sonarqube` user permissions to `create`, `update`, and `delete` objects for this schema.

<details>

<summary>Microsoft SQL Server</summary>

{% hint style="warning" %}
Collation **MUST** be case-sensitive (CS) and accent-sensitive (AS).

`READ_COMMITED_SNAPSHOT` **MUST** be set on the SonarQube database.
{% endhint %}

MS SQL database’s shared lock strategy may impact SonarQube runtime. Making sure that `is_read_committed_snapshot_on` is set to `true` to prevent SonarQube from facing potential deadlocks under heavy loads.

Example of query to check `is_read_committed_snapshot_on`:

```css-79elbk
SELECT is_read_committed_snapshot_on FROM sys.databases WHERE name='YourSonarQubeDatabase';
```

Example of query to update `is_read_committed_snapshot_on`:

```css-79elbk
ALTER DATABASE YourSonarQubeDatabase SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
```

#### Integrated security <a href="#integrated-security" id="integrated-security"></a>

To use integrated security:

1. Download the [Microsoft SQL JDBC Auth 9.2.0 package](https://github.com/microsoft/mssql-jdbc/releases/download/v9.2.0/mssql-jdbc_auth.zip) and copy `mssql-jdbc_auth-11.2.0.x64.dll` to any folder in your path.
2. *If you’re running SonarQube as a Windows service*, make sure the Windows account under which the service is running has permission to connect your SQL server. The account should have `db_owner` database role membership.

   *If you’re running the SonarQube server from a command prompt*, the user under which the command prompt is running should have `db_owner` database role membership.
3. Ensure that `sonar.jdbc.username` or `sonar.jdbc.password` properties are commented out or SonarQube will use SQL authentication.

```css-79elbk
sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true
```

#### SQL authentication <a href="#sql-authentication" id="sql-authentication"></a>

To use SQL authentication, use the following connection string. Also, ensure that `sonar.jdbc.username` and `sonar.jdbc.password` are set appropriately:

```css-79elbk
sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar
sonar.jdbc.username=sonarqube
sonar.jdbc.password=mypassword
```

</details>

<details>

<summary>Oracle</summary>

If there are two SonarQube schemas on the same Oracle instance, especially if they are for two different versions, SonarQube gets confused and picks the first it finds. To avoid this issue:

* Either privileges associated to the SonarQube Oracle user should be decreased.
* Or a trigger should be defined on the Oracle side to automatically alter the SonarQube Oracle user session when establishing a new connection: `ALTER SESSION SET current_schema="MY_SONARQUBE_SCHEMA"`.

{% hint style="warning" %}
Oracle JDBC driver versions 12.1.0.1 and 12.1.0.2 have major bugs, and are not recommended for use with SonarQube ([**see more details**](https://groups.google.com/forum/#!msg/sonarqube/Ahqt1iarqJg/u0BVRJZnBQAJ)).
{% endhint %}

</details>

<details>

<summary>PostgreSQL</summary>

If you want to use a custom schema and not the default "public" one, the PostgreSQL `search_path` property must be set:

```css-79elbk
ALTER USER mySonarUser SET search_path to mySonarQubeSchema
```

</details>

## Installing SonarQube from the ZIP file <a href="#installing-sonarqube-from-the-zip-file" id="installing-sonarqube-from-the-zip-file"></a>

First, check the [prerequisites-and-overview](https://docs.sonarsource.com/sonarqube-server/8.9/requirements/prerequisites-and-overview "mention"). Then download and unzip the [distribution](https://www.sonarsource.com/products/sonarqube/downloads/) (do not unzip into a directory starting with a digit).

SonarQube cannot be run as `root` on Unix-based systems, so create a dedicated user account for SonarQube if necessary.

`<SONARQUBE_HOME>` (below) refers to the path to the directory where the SonarQube distribution has been unzipped.

### Setting access to the database <a href="#setting-access-to-the-database" id="setting-access-to-the-database"></a>

Edit `<SONARQUBE_HOME>/conf/sonar.properties` to configure the database settings. Templates are available for every supported database. Just uncomment and configure the template you need and comment out the lines dedicated to H2:

```css-79elbk
Example for PostgreSQL
sonar.jdbc.username=sonarqube
sonar.jdbc.password=mypassword
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
```

### Adding the JDBC driver <a href="#adding-the-jdbc-driver" id="adding-the-jdbc-driver"></a>

Drivers for the supported databases (except Oracle) are already provided. Do not replace the provided drivers; they are the only ones supported.

For Oracle, copy the JDBC driver into `<SONARQUBE_HOME>/extensions/jdbc-driver/oracle`.

### Configuring the Elasticsearch storage path <a href="#configuring-the-elasticsearch-storage-path" id="configuring-the-elasticsearch-storage-path"></a>

By default, Elasticsearch data is stored in `<SONARQUBE_HOME>/data`, but this is not recommended for production instances. Instead, you should store this data elsewhere, ideally in a dedicated volume with fast I/O. Beyond maintaining acceptable performance, doing so will also ease the upgrade of SonarQube.

Edit `<SONARQUBE_HOME>/conf/sonar.properties` to configure the following settings:

```css-79elbk
sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp
```

The user used to launch SonarQube must have read and write access to those directories.

### Starting the web server <a href="#starting-the-web-server" id="starting-the-web-server"></a>

The default port is `9000` and the context path is `/`. These values can be changed in `<SONARQUBE_HOME>/conf/sonar.properties`:

```css-79elbk
sonar.web.host=192.168.0.1
sonar.web.port=80
sonar.web.context=/sonarqube
```

Execute the following script to start the server:

* On Linux: `<SONARQUBE_HOME>/bin/linux-x86-64/sonar.sh start`
* On macOS: `<SONARQUBE_HOME>/bin/macosx-universal-64/sonar.sh start`
* On Windows: `<SONARQUBE_HOME>/bin/windows-x86-64/StartSonar.bat`

You can now browse SonarQube at [http://localhost:9000](http://localhost:9000/) (the default system administrator credentials are `admin`/`admin`).

### Adjusting the Java installation <a href="#adjusting-the-java-installation" id="adjusting-the-java-installation"></a>

If there are multiple versions of Java installed on your server, you may need to explicitly define which version of Java is used.

To change the Java JVM used by SonarQube, edit *$SONARQUBE-HOME/conf/wrapper.conf* and update the following line:

```css-79elbk
wrapper.java.command=/path/to/my/jdk/bin/java
```

### Advanced installation features <a href="#advanced-installation-features" id="advanced-installation-features"></a>

* Running SonarQube as a service on [operating-the-server](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/configure-and-operate-a-server/operating-the-server "mention") or [operating-the-server](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/configure-and-operate-a-server/operating-the-server "mention")
* Running SonarQube [operating-the-server](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/configure-and-operate-a-server/operating-the-server "mention")
* Monitoring and adjusting [monitoring](https://docs.sonarsource.com/sonarqube-server/8.9/instance-administration/monitoring "mention")

## Installing SonarQube from the Docker image <a href="#instlaling-sonarqube-from-the-docker-image" id="instlaling-sonarqube-from-the-docker-image"></a>

Follow these steps for your first installation:

1. Creating the following volumes helps prevent the loss of information when updating to a new version or upgrading to a higher edition:
   * `sonarqube_data`: contains data files, such as the embedded H2 database and Elasticsearch indexes
   * `sonarqube_logs`: contains SonarQube logs about access, web process, CE process, and Elasticsearch
   * `sonarqube_extensions`: will contain any plugins you install and the Oracle JDBC driver if necessary.

Create the volumes with the following commands:

```css-79elbk
$> docker volume create --name sonarqube_data
$> docker volume create --name sonarqube_logs
$> docker volume create --name sonarqube_extensions
```

{% hint style="warning" %}
Make sure you’re using [**volumes**](https://docs.docker.com/storage/volumes/) as shown with the above commands, and not [**bind mounts**](https://docs.docker.com/storage/bind-mounts/). Using bind mounts prevents plugins from populating correctly.
{% endhint %}

Drivers for supported databases (except Oracle) are already provided. If you’re using an Oracle database, you need to add the JDBC driver to the `sonar_extensions` volume. To do this:

a. Start the SonarQube container with the embedded H2 database:

```css-79elbk
$ docker run --rm \
    -p 9000:9000 \
    -v sonarqube_extensions:/opt/sonarqube/extensions \
    <image_name>
```

b. Exit once SonarQube has started properly.

c. Copy the Oracle JDBC driver into `sonarqube_extensions/jdbc-driver/oracle`.

3\. Run the image with your database properties defined using the `-e` environment variable flag:

```css-79elbk
$> docker run -d --name sonarqube \
    -p 9000:9000 \
    -e SONAR_JDBC_URL=... \
    -e SONAR_JDBC_USERNAME=... \
    -e SONAR_JDBC_PASSWORD=... \
    -v sonarqube_data:/opt/sonarqube/data \
    -v sonarqube_extensions:/opt/sonarqube/extensions \
    -v sonarqube_logs:/opt/sonarqube/logs \
    <image_name>
```

For more configuration environment variables, see the [environment-variables](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/configure-and-operate-a-server/environment-variables "mention").

{% hint style="warning" %}
Use of the environment variables `SONARQUBE_JDBC_USERNAME`, `SONARQUBE_JDBC_PASSWORD`, and `SONARQUBE_JDBC_URL` is deprecated and will stop working in future releases.
{% endhint %}

### Example Docker Compose configuration <a href="#example-docker-compose-configuration" id="example-docker-compose-configuration"></a>

If you’re using [Docker Compose](https://docs.docker.com/compose/), use the following example as a reference when configuring your `.yml` file. Click the heading below to expand the `.yml` file.

{% hint style="info" %}
The example below will use the latest version of the SonarQube Docker image. If want to use the LTS version of SonarQube, you need to update the example with the `sonarqube:lts-community` image tag.
{% endhint %}

<details>

<summary>Docker Compose .yml file example</summary>

```css-79elbk
version: "3"

services:
  sonarqube:
    image: sonarqube:community
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:12
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:
```

</details>

## Next steps <a href="#next-steps" id="next-steps"></a>

Once your server is installed and running, you may also want to [install-a-plugin](https://docs.sonarsource.com/sonarqube-server/8.9/setup-and-upgrade/install-a-plugin "mention"). Then you’re ready to begin [overview](https://docs.sonarsource.com/sonarqube-server/8.9/analyzing-source-code/overview "mention").

## Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

### Failed to connect to the marketplace via proxy <a href="#failed-to-connect-to-the-marketplace-via-proxy" id="failed-to-connect-to-the-marketplace-via-proxy"></a>

Double-check that settings for proxy are correctly set in `<SONARQUBE_HOME>/conf/sonar.properties`. Note that if your proxy username contains a backslash, then it should be escaped; a username `domain\user` in the file should look like this example:

```css-79elbk
http.proxyUser=domain\\user
```

For some proxies, the exception `java.net.ProtocolException: Server redirected too many times` might mean an incorrect username or password has been configured.

### Exception java.lang.RuntimeException: cannot run elasticsearch as root <a href="#exception-javalangruntimeexception-cannot-run-elasticsearch-as-root" id="exception-javalangruntimeexception-cannot-run-elasticsearch-as-root"></a>

SonarQube starts an Elasticsearch process, and the same account that is running SonarQube itself will be used for the Elasticsearch process. Since Elasticsearch cannot be run as root, that means SonarQube can’t be either. You must choose some other, non-root account with which to run SonarQube, preferably an account dedicated to the purpose.

### Sonarqube fails to decorate merge requests when DNS entry to ALM changes <a href="#sonarqube-fails-to-decorate-merge-requests-when-dns-entry-to-alm-changes" id="sonarqube-fails-to-decorate-merge-requests-when-dns-entry-to-alm-changes"></a>

If you run SonarQube in an environment with a lot of DNS friction, you should define a DNS cache time to live policy as, by default, SonarQube will hold the DNS cache until it is restarted. You can set this policy to five seconds by doing the following:

```css-79elbk
echo "networkaddress.cache.ttl=5" >> "${JAVA_HOME}/conf/security/java.security" 
```

Please be aware that this increases the risk of DNS spoofing attacks.
