Elasticsearch is an open-source distributed search and analysis engine. It is based on Apache Lucene and allows searching various types of structured and unstructured data such as textual, numerical, or geospatial data. It is a popular search engine designed for applications with complex search requirements. You can integrate Elasticsearch with your application to add search functionality.

This tutorial will show you how to install Elasticsearch on Ubuntu 22.04.

Requirements

  • A server running Ubuntu 22.04.
  • A root password is set up on the server.

Install Java

Since Elasticsearch is based on Java, you need to install Java on your server. You can install Java JDK using the following command:

apt install default-jdk -y

Once Java is installed, you can check the Java version with the following command:

java --version

You should see the following output:

openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment (build 11.0.15 10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15 10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Add Elasticsearch Repository

By default, the Elasticsearch package is not included in the default repository of Ubuntu 22.04. Therefore, you need to add the official repository to the APT.

First, install the required dependencies using the following command:

apt install curl wget gnupg2 wget -y

Once all dependencies are installed, add the Elasticsearch GPG key using the following command:

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | gpg --dearmor -o /usr/share/keyrings/elastic.gpg

Next, add the Elasticsearch repository to the APT using the following command:

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

Next, update the repository’s cache with the following command:

apt update -y

Once the repository is updated, you can proceed to the next step.

Install Elasticsearch on Ubuntu 22.04

Now install Elasticsearch with the following command:

apt install elasticsearch -y

Once the Elasticsearch package is installed, edit the Elasticsearch configuration file:

nano /etc/elasticsearch/elasticsearch.yml

Change the following line:

network.host: localhost

Save and close the file and start the Elasticsearch service with the following command:

systemctl start elasticsearch

You can check the status of Elasticsearch with the following command:

systemctl status elasticsearch

You should see the following output:

? elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-05-28 10:44:01 UTC; 8s ago
       Docs: https://www.elastic.co
   Main PID: 7259 (java)
      Tasks: 68 (limit: 2292)
     Memory: 1.2G
        CPU: 45.941s
     CGroup: /system.slice/elasticsearch.service
             ??7259 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.tt>
             ??7417 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

May 28 10:43:20 ubuntu2204 systemd[1]: Starting Elasticsearch...
May 28 10:44:01 ubuntu2204 systemd[1]: Started Elasticsearch.

Verify Elasticsearch Installation

After installing Elasticsearch, you can verify the Elasticsearch installation with the following command:

curl -X GET 'http://localhost:9200'

You should see the following output:

{
  "name" : "ubuntu2204",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "lrkrHPovRgiCdF67bVnS9w",
  "version" : {
    "number" : "7.17.4",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
    "build_date" : "2022-05-18T18:04:20.964345128Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

How to use Elasticsearch

With Elasticsearch you can create, read, update and delete data by using the curl command. To add an entry to Elasticsearch, run the following command:

curl -XPOST -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'

You will get the following output:

{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

You can now retrieve the added entry using the following command:

curl -X GET -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1'

You will get the following output:

{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{ "message": "Hello World!" }}

To modify an existing entry, use the put command as shown below:

curl -X PUT -H "Content-Type: application/json" 'localhost:9200/tutorial/helloworld/1?pretty' -d ' { "message": "Hello, People!" }'

You will get the following output:

{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

Now you can retrieve the added entry in a more readable format using the following command:

curl -X GET -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1?pretty'

You will get the following output:

{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "message" : "Hello, People!"
  }
}

Conclusion

Congratulations. You have successfully installed Elasticsearch on Ubuntu 22.04. Now you can integrate Elasticsearch into your application and search the data in real time.