Elasticsearch is an open-source search and analytics engine. It is not available in the default package repositories of CentOS, RHEL, or Fedora. To install it, a dedicated repository must be manually configured. This repository is provided by the developers which contain RPM packages and supports yum or dnf package managers.
Elasticsearch requires a Java Runtime Environment (JRE) to run. Installing a compatible JRE before Elasticsearch is essential to avoid dependency issues. The installation process involves adding the Elasticsearch repository, installing the required packages, and configuring Elasticsearch for proper operation.
After installation, Elasticsearch needs to be configured to start automatically. Network access must be enabled for Elasticsearch to communicate with external systems through its default ports (9200 and 9300). Basic firewall rules should also be set up to allow traffic on these ports.
Steps to install Elasticsearch on CentOS, RHEL, or Fedora:
- Launch terminal.
- Install the Java Runtime Environment.
$ sudo dnf install --assumeyes java-11-openjdk [sudo] password for user: Last metadata expiration check: 1 day, 0:28:04 ago on Fri 30 Apr 2021 07:01:52 AM +08. Dependencies resolved. ================================================================================ Package Arch Version Repo Size ================================================================================ Installing: java-11-openjdk x86_64 1:11.0.11.0.1-0.2.ea.el8 appstream 260 k Installing dependencies: copy-jdk-configs noarch 3.7-4.el8 appstream 27 k java-11-openjdk-headless x86_64 1:11.0.11.0.1-0.2.ea.el8 appstream 39 M javapackages-filesystem noarch 5.3.0-1.module_el8.0.0+11+5b8c10bd appstream 30 k lksctp-tools x86_64 1.0.18-3.el8 baseos 100 k ttmkfdir x86_64 3.0.9-54.el8 appstream 62 k tzdata-java noarch 2021a-1.el8 appstream 192 k xorg-x11-fonts-Type1 noarch 7.5-19.el8 appstream 522 k Enabling module streams: javapackages-runtime 201801 Transaction Summary ================================================================================ Install 8 Packages Total download size: 41 M Installed size: 173 M ##### snipped
- Download and install the GPG key for Elasticsearch repository.
$ sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- Add the Elasticsearch repository to yum or dnf.
$ sudo tee /etc/yum.repos.d/elasticsearch.repo <<EOF [elasticsearch-7.x] name=Elasticsearch repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF
- Install the Elasticsearch package using dnf.
$ sudo dnf install --assumeyes elasticsearch Elasticsearch repository for 7.x packages 17 MB/s | 24 MB 00:01 Last metadata expiration check: 0:00:05 ago on Sat 01 May 2021 07:34:39 AM +08. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: elasticsearch x86_64 7.12.1-1 elasticsearch-7.x 311 M Transaction Summary ================================================================================ Install 1 Package Total download size: 311 M Installed size: 518 M ##### snipped
Even though dnf and yum supposedly resolve and install package dependencies, elasticsearch might not list java as a dependency thus you might come to this error if you don't manually install java as in the previous step.
Running transaction which: no java in (/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin) could not find java; set JAVA_HOME or ensure java is in PATH error: %pre(elasticsearch-0:6.4.0-1.noarch) scriptlet failed, exit status 1 Error in PREIN scriptlet in rpm package elasticsearch-6.4.0-1.noarch Verifying : elasticsearch-6.4.0-1.noarch 1/1 Failed: elasticsearch.noarch 0:6.4.0-1
- Open the Elasticsearch configuration file to edit configuration options if necessary.
$ sudo vi /etc/elasticsearch/elasticsearch.yml
Configure network.host to allow connections from remote systems. Use 0.0.0.0 to listen on all network interfaces.
network.host: 0.0.0.0
Related: How to install Kibana on CentOS, Red Hat, or Fedora
- Allow access to Elasticsearch through firewall ports 9200 and 9300.
$ sudo firewall-cmd --permanent --add-port=9200/tcp success # firewall-cmd --permanent --add-port=9300/tcp success
- Reload firewall rules.
$ sudo firewall-cmd --reload success
- Enable Elasticsearch service to start on boot.
$ sudo systemctl enable elasticsearch Synchronizing state of elasticsearch.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable elasticsearch Created symlink /etc/systemd/system/multi-user.target.wants/elasticsearch.service → /usr/lib/systemd/system/elasticsearch.service.
- Start Elasticsearch service.
$ sudo systemctl start elasticsearch
The service will take a while to start
- Verify Elasticsearch service is running by accessing it locally.
$ curl 127.0.0.1:9200 { "name" : "host", "cluster_name" : "elasticsearch", "cluster_uuid" : "L0drd8tURQSPpAvNzboGwQ", "version" : { "number" : "7.12.1", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "3186837139b9c6b6d23c3200870651f10d3343b7", "build_date" : "2021-04-20T20:56:39.040728659Z", "build_snapshot" : false, "lucene_version" : "8.8.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
You will not be able to connect immediately to Elasticsearch because the service takes quite a while to start and will get the following error if you try to connect immediately.
$ curl 127.0.0.1:9200 curl: (7) Failed to connect to 127.0.0.1 port 9200: Connection refused

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.