Elasticsearch Running Elasticsearch Locally: An Advanced Guide

By Opster Team

Updated: Oct 31, 2023

| 2 min read

Quick Links

Overview

Running Elasticsearch locally is a common practice among developers and system administrators. It allows for testing, development, and troubleshooting in a controlled environment. This article will delve into the advanced aspects of running Elasticsearch locally, including setup, configuration, and optimization.

Setting Up Elasticsearch Locally

The first step to running Elasticsearch locally is to download and install it on your local machine. Elasticsearch is a Java application, so you’ll either need to have Java installed or use the JVM that is bundled with the Elasticsearch distribution. The recommended version for Elasticsearch 8 is Java 17, but newer versions are also compatible.

You can download Elasticsearch directly from the official Elastic website. After downloading, extract the files to your desired location. Elasticsearch is now installed and ready to be run locally.

Running Elasticsearch

To run Elasticsearch, navigate to the bin directory within the Elasticsearch installation folder. Here, you can run the `elasticsearch` command (or `elasticsearch.bat` on Windows). This will start Elasticsearch with the default settings.

Since Elasticsearch 8.0.0, security is now enabled by default and a default password is automatically generated for the elastic user and printed out once in the terminal window. Also certificates and TLS keys are also created and set up so that HTTP traffic is secured. Finally, an enrollment token to log into Kibana is generated and printed out in the terminal window as well. This enrollment token will be valid for 30 minutes.

By default, Elasticsearch runs on port 9200. You can verify that Elasticsearch is running by entering the address in your web browser or by sending a GET request to `localhost:9200` using a tool like curl in a new terminal window:

curl --cacert $ES_HOME/config/certs/http_ca.crt -u elastic:<pwd> https://localhost:9200

Note that <pwd> must be replaced with the password that has been automatically generated and printed out in the terminal window when you started Elasticsearch the first time.

Configuring Elasticsearch

Elasticsearch’s behavior can be customized through its configuration file, `elasticsearch.yml`, located in the config directory of the Elasticsearch installation. Here, you can set various parameters such as the cluster name, node name, and network host.

For example, to change the port that Elasticsearch runs on, you can add the following line to `elasticsearch.yml`:

http.port: 9201

After making changes to the configuration file, you’ll need to restart Elasticsearch for the changes to take effect.

If you need to change the password for the elastic built-in user, you can refer to this guide.

Optimizing Elasticsearch

When running Elasticsearch locally, it’s important to optimize it for your specific use case. One common area of optimization is memory allocation. By default, Elasticsearch allocates 1GB of memory to the JVM heap. This can be increased or decreased based on your needs by setting the `ES_JAVA_OPTS` environment variable.

For example, to allocate 2GB of memory to Elasticsearch, you can run the following command before starting Elasticsearch:

export ES_JAVA_OPTS="-Xms2g -Xmx2g"

Another area of optimization is indexing. By default, Elasticsearch refreshes indices every second. If you’re performing a large number of writes, you may want to increase this interval to reduce CPU usage. This can be done by adding the following line to `elasticsearch.yml`:

index.refresh_interval: 10s

Troubleshooting Elasticsearch

If you encounter issues while running Elasticsearch locally, there are several tools at your disposal. The Elasticsearch logs, located in the logs directory of the Elasticsearch installation, can provide valuable information about any errors or warnings.

Additionally, the `_cat` APIs can provide a wealth of information about your Elasticsearch instance. For example, the `_cat/health` endpoint can provide information about the health of your cluster, while the `_cat/indices` endpoint can provide information about your indices.

You can also run Opster’s free Check-Up tool to detect search problems and resolve them.

Conclusion

In conclusion, running Elasticsearch locally involves more than just starting the server. By understanding how to configure and optimize Elasticsearch, you can ensure that your local instance is tailored to your specific needs.