How to Install Elasticsearch on Ubuntu 22.04 | Step-by-Step

Skylar Johnson
6 min readAug 23, 2023

In this tutorial you will learn how to install Elasticsearch on an Ubuntu 22.04 server. You will also learn how to index and manipulate data using the Elasticsearch REST API

What is Elasticsearch?

Elasticsearch is a free service Distributed search engine and analysis tool based on the Apache Lucene library. It’s a fast and scalable analytics engine that exposes an advanced API that lets you process JSON requests and get feedback in milliseconds. This makes it an ideal choice for data analysis and research use cases.

How does elasticsearch work?

Elasticsearch is a key component of the ELK Stack (Elasticsearch, Logstash Kibana), where it is used to index and store data. Instead of tables and schemas, its structure is based on documents where data is stored in key-value pairs.

Prerequisites

Before you install Elasticsearch on Ubuntu and start using it, ensure that you have the following set of requirements:

A running instance of Ubuntu 22.04 server with at least 2GB RAM and 2 vCPUs.
SSH access to the server with a sudo user configured.

Don’t Miss: ChatGPT: How to Use Generative AI in Content Creation

Step 1: Install Elasticsearch

Elasticsearch is not officially hosted in the standard Ubuntu package repositories. The only approach is to add the elastic bundle source list to the source list directory. Once added, you can install it using the APT package manager.

To get started, you need to add the Elasticsearch GPG signing key to authenticate Elasticsearch packages. Authenticated packages ensure that your system can trust the integrity of packages that the package manager installs on your system.

To add the signing key, import the public GPG key of Elasticsearch file with command curl.

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

Once the GPG key is added, add the list of elastic fonts to the directory sources.list.d.

echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

Next, update the local package lists to notify the system of the newly added repository.

sudo apt update

Then install Elasticseach using the following command:

$ sudo apt install elasticsearch

The command installs Elasticsearch and also creates a user and group named elasticsearch.

Step 2: Configure Elasticsearch

The main configuration file for Elasticsearch is the elasticsearch.yml file in the /etc/elasticsearch directory. It is a YAML file that stores cluster, node, storage, path and network settings. This is the main configuration file and largely controls how Elasticsearch works.

Some are needed to customize Elasticsearch to your liking, so access the file with your favorite text editor. In this example, we’re using the nano editor.

sudo nano /etc/elasticsearch/elasticsearch.yml

First of all specify a cluster name Note that a node can only join a cluster if it has the same cluster name as the other nodes in the same cluster.

Scroll down to the Clusters section and uncomment the cluster.name directive. Enter a descriptive name for your cluster. We rename it my-cluster.

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-cluster
#

Next, define the name of the node. This is defined by the node.name directive. By default, it is set to Node-1. You can configure it manually by commenting and specifying your preferred name. Here we call it example node.

# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: sample-node

By default, Elasticsearch listens for traffic originating from the localhost or IP address 127.0.0.1. To query another server, set the network.host directive to the appropriate IP address. Scroll down to the Network section and set your preferred IP address. In our case, we set it to “localhost”.

# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
network.host: localhost
#

Finally, specify the HTTP port Elasticsearch is listening on. By default, this is port 9200. You can leave it as is or specify a different port.

Once you’re done editing the configuration, save your changes and exit the configuration file. Then run the following command to notify the system of the changes.

sudo systemctl daemon-reload

Next, enable the Elasticsearch service to start on startup.

sudo systemctl enable elasticsearch

Next, start the Elasticsearch service as follows. This usually takes about a minute, and the command appears to hang or freeze as soon as you hit Enter. So don’t panic. Just a little patience.

sudo systemctl start elasticsearch

To confirm that Elasticsearch is running, execute the command:

sudo systemctl status elasticsearch

From the output, you can see that Elasticsearch is up and running.

Step 3: Test Elasticsearch

At this point, Elasticsearch is up and running on port 9200, the default port. The easiest way to verify that Elasticsearch is working is to query the Elasticsearch server by sending a GET request with the curl command, like so.

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

If the installation was successful, you should get the following output in JSON format showing server details.

For in-depth information about the Elasticsearch server, run the following command:

curl -X GET 'http://localhost:9200/_nodes?pretty'

The ?pretty directive formats the into a comprehensible format.

Step 4: Configure UFW firewall

The way things are, the Elasticsearch HTTP Programming interface can be gotten to by anybody who has your server’s IP. You might need to confine admittance to simply your IP address and not every other person.

You can do this by designing the UFW firewall by applying the accompanying guideline where [your-ip-address] is your public IP address.

sudo ufw allow from [your-ip-address] to any port 9200

To add another IP address, run a similar order once more, this time utilizing an alternate IP address.

In the event that the firewall isn’t empowered, make certain to empower it.

sudo ufw enable

Reload the firewall for the rule to take effect.

sudo ufw reload

Then verify the firewall status.

sudo ufw status

Step 5: Working with Elasticsearch

Elasticsearch utilizes a Relaxing Programming interface that permits it to perform essential tasks that compare to Muck tasks, for example, make, read, update, and erase. The HTTP strategies comparable to these tasks are POST, GET, PUT, and Erase, individually.

To begin utilizing Elasticsearch, you want to populate a list for certain information first. A record is what could be compared to a data set in a social data set. It is an assortment of records, each with fields coordinated into key-esteem coordinates that contain information.

To make a record, you really want to send a PUT solicitation to the Programming interface utilizing the Twist order utilizing the list name, type, and ID.

Allow us to record something. In the order beneath, we are making a file called motion pictures and of type class with an ID of 1. The file stores data about a film in JSON design.

curl -XPOST -H "Content-Type: application/json" 'http://localhost:9200/movies/genre/1' -d \
'{
"title": "Designated Survivor",
"director": "David Guggenheim",
"year": 2016,
"genre": ["Drama", "Crime"]
}'

You should get the output like what we have.

To retrieve this entry, send an HTTP GET request as follows.

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

Now we will change the entry using the HTTP PUT request.

curl -X PUT -H "Content-Type: application/json" 'http://localhost:9200/movies/genre/1?pretty' -d \
'{
"title": "Shooter",
"director": "Antoine Fuqua",
"year": 2007,
"genre": ["Drama", "Crime"]
}'

Elasticsearch will recognize the progressions made and show the accompanying result. After the alteration of the film records, notice that the form number has consequently expanded to 2. This demonstrates that a change has been made to the record.

To verify the modification, See the records by sending a GET request.

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

The ?pretty part toward the finish of the order organizes the result into a more intelligible organization.

Conclusion

In the wake of finishing each move toward this instructional exercise, you have effectively introduced and designed Elasticsearch and tried its usefulness utilizing the HTTP POST, GET and PUT techniques.

Read More: ChatGPT Unblocked: How to Unblock ChatGPT in 2023

--

--

Skylar Johnson

I'm a Web developer who is always looking to learn more and eat too much chocolate. https://www.thetravelocity.com