Skip to main content
  1. Tutorials/

How To Install Elasticsearch on an Ubuntu VPS

Tutorials Elasticsearch Ubuntu
Status: Deprecated>

Status: Deprecated #

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Upgrade to Ubuntu 14.04.
Upgrade from Ubuntu 14.04 to Ubuntu 16.04
Migrate the server data to a supported version

Reason:
Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

Elasticsearch is a platform for distributed, RESTful search and analysis. It can scale as needed, and you can get started using it right away on a single DigitalOcean droplet. In this tutorial, we will download, install, and start using Elasticsearch on Ubuntu. The steps provided have currently been tested on: Ubuntu 12.04.3 x64 and Ubuntu 13.10 x64.

Dependencies>

Dependencies #

First, update the list of available packages by running apt-get update.
Next, we must install the Java runtime. There are two options here.

Install the OpenJDK runtime supplied by Ubuntu.
Install the Elasticsearch recommended Java runtime, Oracle Java.

The first option works perfectly fine if you would just like to play around and get acquainted with Elasticsearch or run a small collection of nodes. The latter option is the one recommended by Elasticsearch for guaranteed compatibility.

OpenJDK>

OpenJDK #

To accomplish the first option, we can simply run apt-get install openjdk-6-jre.

Oracle Java>

Oracle Java #

For the second option, we’ll follow the steps in the Elasticsearch documentation. To begin, we must add a repository that contains the Oracle Java runtime

sudo add-apt-repository ppa:webupd8team/java

We must then run apt-get update to pull in package information from this new repository. After doing so, we can install the Oracle Java runtime

sudo apt-get install oracle-java7-installer

While executing the above command you will be required to accept the Oracle binary license. If you don’t agree to the license, you may instead install the OpenJDK runtime instead.

Test your Java installation>

Test your Java installation #

You can then check that Java is installed by running java -version.
That’s all the dependencies we need for now, so let’s get started with obtaining and installing Elasticsearch.

Download and Install>

Download and Install #

Elasticsearch can be downloaded directly from their site in zip, tar.gz, deb, or rpm packages. You don’t need to do this ahead of time, as we will download the files that we need as we need them in the text below.

Install>

Install #

Given the download options provided by Elasticsearch, we have a few options:

Install from the zip or tar.gz archive.
Install from the deb package.
Install from the rpm package.

That last option is not the Ubuntu way, so we’ll ignore it.
Installing from zip or tar.gz archive is best if you just want to play around with Elasticsearch for a bit. Installing from either of these options simply makes available the binaries needed for running Elasticsearch. Installing from the deb package fully installs Elasticsearch and starts the server running immediately. This includes installing an init script at /etc/init.d/elasticsearch which starts Elasticsearch on boot. If you are only looking to play around with Elasticsearch, I suggest installing from the zip or tar.gz. That way, you can discover Elasticsearch while starting and stopping the server at will.

Installing from zip or tar.gz archive>

Installing from zip or tar.gz archive #

The zip and tar.gz downloads both contain pre-compiled binaries for Elasticsearch.
To begin, download the source somewhere convenient. After extracting the archive, you will be able to run the binaries directly from the resulting directory, so you should place them somewhere accessible by every user you want to have access to Elasticsearch. For this tutorial, we’ll just download to our current user’s directory. If you download them to /tmp, they are likely to disappear when you reboot your VPS. If this is what you want, go ahead and place the download there. You can create a new temporary directory in /tmp quickly by running mktemp -d.
In any case, make sure you’re in the directory you want to extract Elasticsearch into before proceeding.

Download the archive>

Download the archive #

Run either

wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.zip

or

wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.tar.gz

The first command downloads the zip archive, and the second command downloads the tar.gz archive. If you downloaded the zip package, make sure you have previously run apt-get install unzip then run

unzip elasticsearch-0.90.7.zip

Alternatively, if you’ve downloaded the tar.gz package, run

tar -xf elasticsearch-0.90.7.tar.gz

Either option will create the directory elasticsearch-0.90.7. Change into that directory by entering cd elasticsearch-0.90.7, and you’ll find the binaries in the bin folder.

Installing from the Debian software package>

Installing from the Debian software package #

The best package to download for Ubuntu is the deb package. The RPM can work but it needs to be converted first, and we will not cover doing so here. Grab the deb package by running

wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.deb

Installing directly from a Debian package is done by running

dpkg -i elasticsearch-0.90.7.deb

This results in Elasticsearch being properly installed in /usr/share/elasticsearch. Recall that installing from the Debian package also installs an init script in /etc/init.d/elasticsearch that starts the Elasticsearch server running on boot. The server will also be immediately started after installation.

Configuration files>

Configuration files #

If installed from the zip or tar.gz archive, configuration files are found in the config folder of the resulting directory. If installing from the Debain package, configuration files are found in /etc/elasticsearch.
In either case, there will be two main configuration files: elasticsearch.yml and logging.yml. The first configures the Elasticsearch server settings, and the latter, unsurprisingly, the logger settings used by Elasticsearch.
“elasticsearch.yml” will, by default, contain nothing but comments.
“logging.yml” provides configuration for basic logging. You can find the resulting logs in /var/log/elasticsearch.

Remove Elasticsearch Public Access>

Remove Elasticsearch Public Access #

Before continuing, you will want to configure Elasticsearch so it is not accessible to the public Internet–Elasticsearch has no built-in security and can be controlled by anyone who can access the HTTP API. This can be done by editing elasticsearch.yml. Assuming you installed with the package, open the configuration with this command:

sudo vi /etc/elasticsearch/elasticsearch.yml

Then find the line that specifies network.bind_host, then uncomment it and change the value to localhost so it looks like the following:

network.bind_host: localhost

Then insert the following line somewhere in the file, to disable dynamic scripts:

script.disable_dynamic: true

Save and exit. Now restart Elasticsearch to put the changes into effect:

sudo service elasticsearch restart

We’ll cover other basic configuration options later, but first we should test the most basic of Elasticsearch installs.

Test your Elasticsearch install>

Test your Elasticsearch install #

You have now either extracted the zip or tar.gz archives to a directory, or installed Elasticsearch from the Debian package. Either way, you have the Elasticsearch binaries available, and can start the server. If you used the zip or tar.gz archives, make sure you’re in the resulting directory. If you installed using the Debian package, the Elasticsearch server should already be running, so you don’t need to start the server as shown below.
Let’s ensure that everything is working. Run

 ./bin/elasticsearch

Elasticsearch should now be running on port 9200. Do note that Elasticsearch takes some time to fully start, so running the curl command below immediately might fail. It shouldn’t take longer than ten seconds to start responding, so if the below command fails, something else is likely wrong.
Ensure the server is started by running

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

You should see the following response

{
  "ok" : true,
  "status" : 200,
  "name" : "Xavin",
  "version" : {
    "number" : "0.90.7",
    "build_hash" : "36897d07dadcb70886db7f149e645ed3d44eb5f2",
    "build_timestamp" : "2013-11-13T12:06:54Z",
    "build_snapshot" : false,
    "lucene_version" : "4.5.1"
  },
  "tagline" : "You Know, for Search"
}

If you see a response similar to the one above, Elasticsearch is working properly. Alternatively, you can query your install of Elasticsearch from a browser by visiting :9200. You should see the same JSON as you saw when using curl above.
If you installed by the zip or tar.gz archive, the server can be stopped using the RESTful API

curl -X POST 'http://localhost:9200/_cluster/nodes/_local/_shutdown'

The above command also works when Elasticsearch was installed using the Debian package, but you can also stop the server using service elasticsearch stop. You can restart the server with the corresponding service elasticsearch start.

Using Elasticsearch>

Using Elasticsearch #

Elasticsearch is up and running. Now, we’ll go over some basic configuration and usage.

Basic configuration>

Basic configuration #

When installed by zip or tar.gz archives, configuration files are found in the config folder inside the resulting directory. When installed via Debian package, configuration files can be found in /etc/elasticsearch/. The two configuration files you will find are elasticsearch.yml and logging.yml. The first is a general Elasticsearch configuration. The provided file contains nothing but comments, so default settings are used. Reading through the file will provide a good overview of the options, but I will make a few suggestions below. None of the settings are necessary. You can work with Elasticsearch without doing any of the following, but it’ll be a raw development environment.
The setting “cluster.name” is the method by which Elasticsearch provides auto-discovery. What this means is that if a group of Elasticsearch servers on the same network share the same cluster name, they will automatically discover each other. This is how simple it is to scale Elasticsearch, but be aware that if you keep the default cluster name and there are other Elasticsearch servers on your network that are not under your control, you are likely to wind up in a bad state.

Basic usage>

Basic usage #

Let’s add some data to our Elasticsearch install. Elasticsearch uses a RESTful API, which responds to the usual CRUD commands: Create, Read, Update, and Destroy.
To add an entry

curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'

You should see the following response
{“ok”:true,“_index”:“tutorial”,“_type”:“helloworld”,“_id”:“1”,“_version”:1}
What we have done is send a HTTP POST request to the Elasticserach server. The URI of the request was /tutorial/helloworld/1. It’s important to understand the parameters here:

“tutorial” is index of the data in Elasticsearch.
“helloworld” is the type.
“1” is the id of our entry under the above index and type.

If you saw the response above to the curl command, we can now query for the data with

curl -X GET 'http://localhost:9200/tutorial/helloworld/1'

which should respond with

{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"exists":true, "_source" : { "message": "Hello World!" }}

Success! We’ve added to and queried data in Elasticsearch.
One thing to note is that we can get nicer output by appending ?pretty=true to the query. Let’s give this a try

curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty=true'

Which should respond with

{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 1,
  "exists" : true, "_source" : { "message": "Hello World!" }
}

which is much more readable. The output will also be pretty printed without needing to append the query string if you have set format=yaml in the Elasticsearch configuration file.

Conclusion>

Conclusion #

We have now installed, configured and begun using Elasticsearch. Since it responds to a basic RESTful API. It is now easy to begin adding to and querying data using Elasticsearch from your application.

Submitted by: ckendell