How to Install Elasticsearch on Debian 12

Elasticsearch is a document-oriented database server built with Java. It is an open-source and real-time search and analytics engine. We can also say that Elasticsearch is a NoSQL database, as it stores data in an unstructured way, where you cannot use SQL to query it. Elasticsearch is typically used to support complex queries and high-performance applications. This article aims to guide you through the installation and configuration process of Elasticsearch on Debian 12.

Prerequisites

Step 1. Log in via SSH

Let’s log in to your Debian 12 VPS through SSH as the root user or as a regular user with sudo privileges.

ssh root@IP_Address -p Port_number

If you cannot log in as root, remember to substitute “root” with a user that has sudo privileges. Additionally, change “IP_Address” and “Port_Number” make sure it matches your server’s respective IP address and SSH port.

You can check whether you have the correct Debian version installed on your server with the following command:

# lsb_release -a

You should get this output:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

To make sure everything is updated, let’s run the update index files command.

# apt update

Step 2. Install Dependencies

Almost every package we install relies on other packages to run properly or to be possible to install. Before proceeding with the other steps, we need to install the apt-transport-https package on our Debian 12 server. We will also need to install GNU PG.

# apt install gnupg wget apt-transport-https

Step 3. Add Repository

In this step, we are going to add the Elasticsearch repository to our Debian 12 server. First of all, we need to import the GPG key. With this GPG key, it can ensure us that the software we are going to install is secure and obtained from the original and official source. Simply execute the command below to import the GPG key.

# wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | gpg --dearmor -o /usr/share/keyrings/elasticsearch.gpg

The GPG key has been added, now we need to import the repository.

# echo "deb [signed-by=/usr/share/keyrings/elasticsearch.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

The command above will save the repository definition to /etc/apt/sources.list.d/elastic-8.x.list

Please note that we do not add-apt-repository commands for several reasons. One of them is that add-apt-repository will add entries to the system’s /etc/apt/sources.list file rather than a clean per-repository file in /etc/apt/sources.list.d. Also, older versions of add-apt-repository would always add a deb-src entry, which will cause errors because Elasticsearch does not provide a source package there.

Step 4. Install Elasticsearch

We added the Elasticsearch repository in the previous step. Now, it is time to install the package. First, we need to update the package index files on the system.

# apt update

Finally, we can install Elasticsearch now.

# apt install elasticsearch

Once completed, you should see an output like this:

--------------------------- Security autoconfiguration information ------------------------------

Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.

The generated password for the elastic built-in superuser is : ynzAq21IQ3SxxfXvLn=+

If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
after creating an enrollment token on your existing cluster.

You can complete the following actions at any time:

Reset the password of the elastic built-in superuser with 
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.

Generate an enrollment token for Kibana instances with 
 '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.

Generate an enrollment token for Elasticsearch nodes with 
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.

-------------------------------------------------------------------------------------------------
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
 sudo systemctl daemon-reload
 sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
 sudo systemctl start elasticsearch.service

As you can see in the message after the installation, Elasticsearch will not run automatically. We can execute this command below to run it now and enable it to run upon server reboot.

# systemctl enable --now elasticsearch

That’s it! Elasticsearch should be up and running on port 9200 – you can verify it with this command:

# systemctl status elasticsearch

It will return an output like this:

● elasticsearch.service - Elasticsearch
Loaded: loaded (/lib/systemd/system/elasticsearch.service; enabled; preset: enabled)
Active: active (running) since Wed 2023-10-18 06:15:12 EDT; 24min ago
Docs: https://www.elastic.co
Main PID: 457 (java)
Tasks: 64 (limit: 2265)
Memory: 1.4G
CPU: 55.749s
CGroup: /system.slice/elasticsearch.service
├─457 /usr/share/elasticsearch/jdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=/usr/share/elasticsearch/bin/elasticsearch -Dc>
├─815 /usr/share/elasticsearch/jdk/bin/java -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -Djava.security.manager=allow -XX:>
└─834 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Step 5. Configure Elasticsearch

The Elasticsearch service is running now. It is time to configure it, and in this article we will configure Elastic to run as a single node and we will reset the ‘elastic’ user password.

# nano /etc/elasticsearch/elasticsearch.yml

Find this line cluster.initial_master_nodes: ["YOURHOSTNAME"] then comment it and add this line: discovery.type: single-node

It should look like this:

#cluster.initial_master_nodes: ["debian12"]
discovery.type: single-node

Then, restart Elasticsearch to apply the changes.

# systemctl restart elasticsearch

That’s it, Elasticsearch is running as a single node now. Next, let’s reset elastic’s password.

# /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic

The command above will prompt you that it will print the new password in the console, press Y to continue.

It returns this output:

This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y

Password for the [elastic] user successfully reset.
New value: TPTP5lRkKuH6wohvk43C

With the new password, we can run this command to check its connection to the Elasticsearch.

# curl -u "elastic:TPTP5lRkKuH6wohvk43C" https://localhost:9200 -k

The command will return an output like this.

{
  "name" : "debian12",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "otdQc0xFR_6YvLgemcaxOg",
  "version" : {
    "number" : "8.10.4",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "b4a62ac808e886ff032700c391f45f1408b2538c",
    "build_date" : "2023-10-11T22:04:35.506990650Z",
    "build_snapshot" : false,
    "lucene_version" : "9.7.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

An additional crucial configuration is the jvm.options. It is possible to generate a file within the /etc/elasticsearch/jvm.options.d/ directory. By default, Elasticsearch automatically determines the JVM heap size according to a node’s role and the total memory available. It is advisable to utilize the default sizing for the majority of production environments. Let’s create a new configuration file to set the Java heap size.

# nano /etc/elasticsearch/jvm.options.d/memory.conf

Then add these lines to set it to 4GB, you can use a higher value if you have more RAM.

-Xms4g
-Xmx4g

Save the file, then exit. Please note that setting the minimum and maximum JVM heap space size to the same value is highly recommended. Do not forget to restart the service every time changes are made.

# systemctl restart elasticsearch

Congratulations on successfully installing and configuring Elasticsearch on your Debian 12 system!

If you found this post helpful in learning how to install and set up Elasticsearch on Debian 12 Cloud VPS, please consider sharing it with your friends on social media using the share buttons below. Alternatively, you can leave a comment in the section provided. Thank you.

Leave a Comment