Install and Configure Elasticsearch on Ubuntu 20.04 | 22.04
Steps to Install and Configure Elasticsearch on Ubuntu 20.04 and 22.04
What is Elasticsearch?
Elasticsearch is a distributed, open-source search and analytics engine designed to handle large amounts of data. It is based on the Apache Lucene search engine library and provides a RESTful interface for interacting with data. Elasticsearch is known for its scalability, speed, and real-time search capabilities, making it an ideal tool for building complex search and analytics applications.
use cases for Elasticsearch
- Text search: Elasticsearch can be used to search for text within large collections of documents, such as web pages, log files, and social media posts.
- Data analysis: Elasticsearch can be used to analyze and visualize data, providing insights into trends, patterns, and anomalies.
- Logging and monitoring: Elasticsearch is often used to store and search through large volumes of logs and metrics from servers, applications, and other systems.
Install Elasticsearch
Update the package list using the following command:
sudo apt-get update
Install Java:
Elasticsearch requires Java to run. You can install it by running the following command:
sudo apt install default-jdk
Check the Java version using the following command:
java -version
Import the Elasticsearch GPG Key
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
Add the Elasticsearch repository to your sources list:
You may need to install the apt-transport-https package before proceeding:
sudo apt-get install apt-transport-https
Add the Elasticsearch repository to your sources list:
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
Install Elasticsearch as a service
Now that all the packages are available, it is time for you to install them.
Update the package list again using the following command:
sudo apt-get update
Install the Elasticsearch package with:
sudo apt-get install elasticsearch
Check Elasticsearch version
You can use the Elasticsearch version command to check the currently installed version.
curl -X GET "http://localhost:9200/"
Start Elasticsearch using the following command:
sudo systemctl start elasticsearch
Enable the Elasticsearch service to start on boot:
To start it and make sure that the service is always available every time the machine is restarted, type the command:
sudo systemctl enable elasticsearch
Verify your Elasticsearch installation
Check the status of the Elasticsearch service to make sure it is running.
To verify it, run the following command:
sudo systemctl status elasticsearch
Elasticsearch should run automatically, but if this is not the case, make sure to start it.
sudo service elasticsearch start
In the future to stop and restart Elasticsearch, the commands are:
Stop:
sudo service elasticsearch stop
Restart:
sudo service elasticsearch restart
Allow Elasticsearch TCP port 9200 in the Firewall
Elasticsearch's default HTTP port is 9200, you’ll need to allow access to this port on the firewall.
If your firewall is UFW type the following commands:
sudo ufw allow 9200/tcp
configure Elasticsearch
Open up the Elasticsearch configuration file in your “nano” editor:
sudo nano /etc/elasticsearch/elasticsearch.yml
Look for the line that contains the “network host” and uncomment it.
Change the network host value to “127.0.0.1” to listen to the external connections:
network.host: 127.0.0.1
Look for the line that contains the “xpack.security.enabled”.
Change the xpack.security.enabled value to “false”.
xpack.security.enabled: false
The last step is to restart the Elasticsearch service:
sudo service elasticsearch restart
Verify that Elasticsearch is running:
curl -X GET "http://localhost:9200/"
You should see a JSON response with information about your Elasticsearch cluster.
Conclusion:
In this post, You have now installed and configured Elasticsearch on Ubuntu 20.04 and 22.04.