How to install and setup Elasticsearch for Magento 2 (Ubuntu)

Watch

1. Introduction

Elasticsearch is a search engine that runs on your web server and is designed to improve the performance and accuracy of search queries. In this case, searching for products within your website. It’s not difficult to setup, but there are a lot of steps. So, in this video I’ll walk you through installing Elasticsearch in Ubuntu and then setting up Magento 2.

By default, Magento 2 uses MySQL to work as the search engine alongside its base functionality. However, MySQL will no longer be supported as the search engine as of Magento 2.4. Therefore, it’s a good idea to get ahead of these changes.

Important:
Having used my Sandbox Server much more after recording this video, it has become apparent that you’ll want to increase the Memory of your Web Server. You might get away with increasing it by 1GB.

In my case, I’ve been running my Sandbox Server on bare-minimum specs (2GB Memory). Subsequently, I’ve increased my server environment to 4GB now.

Before we begin, I want to highlight some key points about this tutorial:

  • It’s written for Ubuntu 16.04 and 18.04
  • At the time of writing, this will install version 7.6 of Elasticsearch
  • Elasticsearch 7.0+ is only supported on Magento 2.3.5+
  • Typically, there a couple of ways to install Elasticsearch according to the documentation. I have chosen the Download method as I’ve had better success with it
  • We’ll also need to install Java Development Kit (JDK) 1.8

So, right now I’m signed into my server as the “superuser”. If you’ve been following along for a while now, you’ll know I don’t use “root” user for security purposes.

2. Installing Java Development (JDK) Kit 1.8

We’ll start with installing the Java Development Kit. This is a nice and simple step - Which is required in order for Elasticsearch to run.

2.1 Update Repositories

So, the first step is to update our existing repositories by entering: [01:38]

sudo apt-get update

2.2 Install JDK 1.8

Then to install the Java Development Kit using: [01:46]

sudo apt-get install openjdk-8-jdk -y

Note: The -y flag on the end automatically applies “Yes” to any confirmation prompts. You don’t have to add this if you don’t want to skip any prompts during installation.

2.3 Verify Installation of JDK 1.8

To verify your installation of JDK 1.8, enter: [02:39]

java -version

An example output would look like:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~16.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

And that’s it. Nice and simple as promised.

3. Installing Elasticsearch

As I mentioned at the beginning, there are several ways of installing Elasticsearch according to the Official Documentation, which I’ve referenced at the bottom of the tutorial. But we’re going ahead with the Download method.

Note: At the time of creating this, Elasticsearch 7.8 is available. However, Magento only supports Elasticsearch 7.6 - So, we’re installing that version.

3.1 Navigate to your home directory

So, we’re about to download 2 files from the Elasticsearch website. One is the installation file and the other is a duplicate file that will be used to compare the integrity of the file. Useful for both technical and security reasons.

But because we’ll be downloading some files, let’s first navigate to our users home directory. This is better than leaving downloaded files lying around your web server. Thankfully, there is a nice shortcut command we can use to get there. So, let’s do that by entering: [03:51]

cd ~

You can confirm what directory you now sit in by entering: [04:02]

pwd

As you can see, I’m currently logged in as craig and sitting inside /home/craig

3.2 Download the necessary files

Now let’s download the 2 Elasticsearch files I just mentioned using: [04:16]

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb

and then: [04:25]

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb.sha512

3.3 File integrity check

With the 2 files now downloaded to our home directory, it’s time to run the following command to ensure the downloaded files aren’t compromised using: [04:41]

shasum -a 512 -c elasticsearch-7.6.0-amd64.deb.sha512

A successful message will look something like this:

elasticsearch-7.6.0-amd64.deb: OK

3.4 Add the file to our package manager

The final setup is to add the downloaded file to our operating system package manager. This is simply achieved by running: [05:07]

sudo dpkg -i elasticsearch-7.6.0-amd64.deb

And that’s Elasticsearch 7.6 successfully downloaded and installed.

4 Setting Elasticsearch to autorun

Now that we’re installed Elasticsearch, it’s time to tell our web server to autorun whenever you restart your server.

4.1 Reload systemd manager

Start by reloading the systemd manager configuration with: [05:33]

sudo /bin/systemctl daemon-reload

4.2 Add Elasticsearch to system startup

And then adding Elasticsearch to our list or bootup services with: [05:46]

sudo /bin/systemctl enable elasticsearch.service

4.3 Running Elasticsearch

With the autorun now set, we still need to manually start Elasticsearch. Do this by entering: [06:02]

sudo systemctl start elasticsearch

Note: In my experience it usually takes 1-2 minutes for the service to initialise, but be patient. Once it’s active, the command prompt will return to normal.

4.4 Testing Elasticsearch

Now, before we go any further, it’s a good time to test that everything we’ve done so far is working as expected. We can use the cURL tool to do this by entering the following command: [06:30]

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

Which should return values similar to what you see here.

{
  "name" : "My First Node",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.1",
    "build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
    "build_timestamp" : "2016-04-04T12:25:05Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

If so, then that’s great. If not then retrace the steps we’ve gone over so far.

5. Securing Elasticsearch

Disclaimer: Next, I was going to talk about securing your installation of Elasticsearch. But from the research I did, it appeared that this was only necessary when installing Elasticsearch on a secondary server due to the use of Ports and External Connections. It appears that as we’re running this on a single-server, those configurations are redundant. However, I do urge you to do your own research in case I’m wrong.

6. Configuring Elasticsearch

By default, Elasticsearch boasts that it’s default settings are good to go straight “out-the-box”. However, this is subject to some optimisation tweaking (that I won’t be covering in this video) and some slight housekeeping - Which we’ll do now.

Let’s start by opening the Elasticsearch configuration file using: [09:14]

sudo nano /etc/elasticsearch/elasticsearch.yml

There are 3 thing we want to change in this file whilst we have it open:

6.1 Updating the Cluster Name

Using your arrow keys, find the line that contains cluster.name. Once you’ve located it, first delete the preceding “#” and then replace “my-application” with something descriptive like “Primary Cluster”.

6.2 Updating the Node Name

Now do the same with node.name, remembering to delete the preceding “#” and then replacing “node-1” with something descriptive like “Magento”.

6.3 Updating the Network Host

Finally do the same with network.host, replacing the IP (“192.168.0.1”) with “localhost”. Don’t forget to delete the preceding “#” like we did with the others.

Now save your changes and exit the nano editor by pressing Ctrl+X to exit, Y to save and Enter to confirm.

6.4 Reload your changes

In order for those 3 changes to take affect, reload the Elasticsearch service by running: [10:30]

sudo systemctl restart elasticsearch

6.5 Final Test of Elasticsearch

Now let’s run a final check to ensure the changes took affect and that we didn’t break anything. Remember, can do this by entering: [10:48]

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

And as you can see, the Name and Cluster name now reflect those changes.

{
  "name" : "Magento",
  "cluster_name" : "Primary Cluster",
  "cluster_uuid" : "7ymV1F7HTw2RuScEDaimvQ",
  "version" : {
    "number" : "7.6.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
    "build_date" : "2020-02-06T00:09:00.449973Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

7 Configuring Magento

Now we’re nearly at the finish line…

7.1 Update Settings

So it’s time to log into Magento and navigate over to Stores > Configuration > Catalog > Catalog > Catalog Search.

  1. Switch the Search Engine option from “MySQL” to “Elasticsearch 7.0+”
  2. After switching you’ll see a button a little further down that says “Test Connection”. Pressing this should change the button text to “Successful! Test again?”
  3. Then go ahead and save your changes by hitting Save Config.

7.2 Reindex Magento

Before we can finish, we just need to reindex Magento so that we can populate the appropriate tables - Otherwise search results may show an error.

Start by hopping back into SSH and switch to your “Magento” user using. As a quick reminder this is the user that we configured in How to install Magento 2.3 and build a web server. [12:00]

su magento

(Or if you logged out before, just log straight in as that user) You’ll need to enter your password for this user.

Then navigate over to the Magento root directory with: [12:24]

cd /var/www/html/

And finally run the Magento Indexer with: [12:36]

bin/magento indexer:reindex

This will take about a minute to complete…

Summary

So, there you have it. You’ve successfully installed Elasticsearch 7.6 onto your web server and set Magento to use this new feature. But this is just the tip of the ice-burg. To unlock the full potential of Elasticsearch, I have included a link to a 3rd party module that I highly recommend - Along with a discount code to save you some cash on the purchase.

References

3 Likes