How to Install Elasticsearch on Ubuntu 18.04

Updated 

5 min read

Elasticsearch is an open source distributed full-text search and analytics engine. It supports RESTful operations and allows you to store, search, and analyze big volumes of data in real time.

Elasticsearch is one of the most popular search engines powering applications that have complex search requirements such as big e-commerce stores and analytic applications.

In this tutorial, we will show you how to install Elasticsearch on Ubuntu 18.04. The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Linux Mint, Kubuntu and Elementary OS.

Prerequisites

You’ll need to be logged in as a user with sudo privileges to be able to install packages on your Ubuntu system.

Installing Elasticsearch

The easiest way to install Elasticsearch on Ubuntu 18.04 is by installing the deb package from the official Elasticsearch repository.

At the time of writing this article, the latest version of Elasticsearch is 7.0.0 and requires Java 8 to be installed on the system.

Start by updating the packages index and installing the apt-transport-https package that necessary to access a repository over HTTPS:

sudo apt update
sudo apt install apt-transport-https

Install OpenJDK 8:

sudo apt install openjdk-8-jdk

Verify the Java installation by running the following command which will print the Java version:

java -version

The output should look something like this:

openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Now that Java is installed, the next step is to add the Elasticsearch repository.

Import the repository’s GPG using the following wget command:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

The command above should output OK which means that the key has been successfully imported and packages from this repository will be considered trusted.

Next, add the Elasticsearch repository to the system by issuing:

sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
If you want to install a previous version of Elasticsearch, change 7.x in the command above with the version you need.

Once the repository is enabled, update the apt package list and install the Elasticsearch engine by typing:

sudo apt update
sudo apt install elasticsearch

Elasticsearch service will not start automatically after the installation process is complete. To start the service and enable the service run:

sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

You can verify that Elasticsearch is running by sending an HTTP request to port 9200 on localhost with the following curl command:

curl -X GET "localhost:9200/"

You should see something similar to this:

{
  "name" : "kwEpA2Q",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "B-5B34LXQFqDeIYwSgD3ww",
  "version" : {
    "number" : "7.0.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "b7e28a7",
    "build_date" : "2019-04-05T22:55:32.697037Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

It will take 5-10 seconds for the service to start. If you see curl: (7) Failed to connect to localhost port 9200: Connection refused, wait for a few seconds and try again.

If you want to see the messages logged by the Elasticsearch service you can use the command below:

sudo journalctl -u elasticsearch

Congratulations, at this point you have Elasticsearch installed on your Ubuntu server.

Configuring Elasticsearch

Elasticsearch data is stored in the /var/lib/elasticsearch directory, configuration files are located in /etc/elasticsearch and Java start-up options can be configured in the /etc/default/elasticsearch file.

By default, Elasticsearch is configured to listen on localhost only. If the client connecting to the database is also running on the same host and you are setting up a single node cluster you don’t need to change the default configuration file.

Remote Access

Out of box Elasticsearch, does not implement authentication so it can be accessed by anyone who can access the HTTP API. If you want to allow remote access to your Elasticsearch server, you will need to configure your firewall and allow access to the Elasticsearch port 9200 only from trusted clients.

Ubuntu comes with a firewall configuration tool called UFW. By default, UFW is installed but not enabled. Before enabling the UFW firewall first add a rule that will allow incoming SSH connections:

sudo ufw allow 22

Allow assess from the remote trusted IP address:

sudo ufw allow from 192.168.100.20 to any port 9200
Do not forget to change 192.168.100.20 with your remote IP Address.

Enable UFW with by typing:

sudo ufw enable

Finally, check the status of firewall:

sudo ufw status

The output should look something like this:

Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
9200                       ALLOW       192.168.100.20
22 (v6)                    ALLOW       Anywhere (v6)

Once your firewall is configured the next step is to edit the Elasticsearch configuration and allow Elasticsearch to listen for external connections.

To do so, open the elasticsearch.yml configuration file:

sudo nano /etc/elasticsearch/elasticsearch.yml

Search for the line that contains network.host, uncomment it, and change the value to 0.0.0.0:

/etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0

If you have multiple network interfaces on your machine you can specify the interface IP address which will cause Elasticsearch to listen only on the specified interface.

Restart the Elasticsearch service for the changes to take effect:

sudo systemctl restart elasticsearch

That’s it. You can now connect to the Elasticsearch server from your remote location.

Conclusion

You have successfully installed Elasticsearch on your Ubuntu 18.04. You can now visit the official Elasticsearch Documentation page and learn how to get started with Elasticsearch.

If you hit a problem or have feedback, leave a comment below.