
Let’s walk through the essential steps to get SonarQube up and running on your Ubuntu 20.04 server. This process involves setting up prerequisites, configuring a database, downloading and installing SonarQube, and finally, getting it running as a service.
First things first, you need to ensure your system is updated. Open your terminal and run:
sudo apt update
sudo apt upgrade -y
SonarQube requires Java. OpenJDK 11 is a common and recommended version. Install it using:
sudo apt install openjdk-11-jdk -y
Verify the installation with:
java -version
A production SonarQube instance needs a dedicated database. PostgreSQL is an excellent choice. Install the server and client:
sudo apt install postgresql postgresql-contrib -y
Start and enable the PostgreSQL service:
sudo systemctl start postgresql
sudo systemctl enable postgresql
Now, create a dedicated user and database for SonarQube within PostgreSQL. Switch to the PostgreSQL user to access the prompt:
sudo -i -u postgres
Access the PostgreSQL prompt:
psql
Create a new user (replace ‘sonaruser’ and ‘yourpassword’ with secure credentials):
CREATE USER sonaruser WITH PASSWORD ‘yourpassword’;
Create the SonarQube database, ensuring it’s owned by the new user and uses UTF8 encoding:
CREATE DATABASE sonarqube OWNER sonaruser ENCODING ‘UTF8’;
Grant all privileges on the database to the user:
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonaruser;
Exit the PostgreSQL prompt:
\q
Exit the postgres user session:
exit
SonarQube should not run as the root user for security reasons. Create a dedicated system user for it:
sudo useradd -M -d /opt/sonarqube sonar
Now, download the latest SonarQube Community Edition. You can find the download link on the official SonarQube website. Use wget
to download it directly to your server (replace the URL with the current version’s link):
cd /opt/
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.LTS.zip (Use the latest stable or LTS version)
Extract the downloaded archive:
sudo unzip sonarqube-9.9.LTS.zip -d /opt/
Rename the extracted directory for convenience:
sudo mv sonarqube-9.9.LTS sonarqube (Adjust if the version number differs)
Grant ownership of the SonarQube directory to the newly created sonar user:
sudo chown -R sonar:sonar /opt/sonarqube
Next, configure SonarQube to connect to the PostgreSQL database. Edit the main configuration file:
sudo nano /opt/sonarqube/conf/sonar.properties
Uncomment and update the database connection details. Find the lines related to PostgreSQL and set them as follows, using the database name, user, and password you created earlier:
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
sonar.jdbc.username=sonaruser
sonar.jdbc.password=your_password
Also, ensure the JVM options are suitable (often defaults are fine, but you can adjust memory if needed). Find the sonar.web.javaOpts
and sonar.ce.javaOpts
lines.
Save and close the file (Ctrl+X
, Y
, Enter).
For easier management, set up SonarQube as a systemd service. Create a new service file:
sudo nano /etc/systemd/system/sonarqube.service
Add the following content to the file:
[Unit]
Description=SonarQube service
After=syslog.target network.target postgresql.service
[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. Reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload
Enable the SonarQube service to start on boot:
sudo systemctl enable sonarqube
Start the SonarQube service:
sudo systemctl start sonarqube
Check the service status to ensure it’s running:
sudo systemctl status sonarqube
By default, SonarQube runs on port 9000. If you have UFW firewall enabled, you need to allow traffic on this port:
sudo ufw allow 9000/tcp
sudo ufw reload
SonarQube may take a few minutes to start up completely for the first time as it initializes the database. Once the service status shows it’s active and running, you can access the SonarQube web interface by navigating to your server’s IP address or hostname followed by port 9000 in your web browser (e.g., http://your_server_ip:9000
).
The default login credentials are username admin and password admin. You will be prompted to change the password upon your first login, which is highly recommended for security.
Congratulations! You have successfully installed SonarQube on your Ubuntu 20.04 server. You can now begin configuring it to analyze your code projects.
Source: https://kifarunix.com/install-sonarqube-on-ubuntu/