How to Install the Elasticsearch PHP Client in Linux or MacOS
In the past, there were limited options for users who hoped for fast and powerful search functionality in a PHP website. Elasticsearch offers these users a real-time search solution; fortunately, Elastic also provides an easy-to-use, low-level PHP client for Elasticsearch. This client adheres closely to the REST API– all of its methods are similar to those of the REST API, and they also follow the same structure as the methods found in the Ruby and Python clients for Elasticsearch. While the PHP client may seem a bit “no-frills”, it was designed that way on purpose. The client was meant to provide a base that could be built upon by more sophisticated libraries.
The following instructions will help users install the PHP client for Elasticsearch on both Linux and MacOS.
Before attempting to install Elasticsearch PHP client, it’s important to understand the system requirements. A few prerequisites need to be in place before the installation can occur. The Elasticsearch PHP client requires the following:
The server or machine needs to be running a UNIX-based OS, such as MacOS or Linux.
The latest version of Elasticsearch also needs to be installed for Unix-based systems such as Ubuntu or MacOS. On these Unix-based systems, you can just use the Debian package installer and Composer (the package and dependency manager for PHP) to complete this task.
Another requirement is PHP 7 or newer, as well as the PHP package and dependency manager, Composer.
ext-curlLibcurl extension for PHP needs to be installed.
Elasticsearch’s PHP client library version 6.0 requires PHP version 7 or above to work properly. This client library also requires the native JSON extension to be at least version 1.3.7.
For more information about system requirements and compatibility, please see Elastic’s version matrix page.
Important Note Regarding Github
Make sure not to use Elasticsearch-PHP Master branch, as it may contain incomplete features or have issues with backwards compatibility.
Installation via Composer
The recommended method for installing the PHP client for Elasticsearch is by using Composer:
- To install Elasticsearch PHP client via Composer, the first step is to edit the
composer.jsonfile in your project directory by pasting in a
JSONcode snippet. If the project is new, the
composer.jsonfile may not exist yet; in this case, the file needs to be created first. If the file exists, simply append the following snippet to the contents of the file:
- Once this dependency has been added to the
composer.jsonfile, the next step is to install composer and its related dependencies. This is accomplished by running the terminal commands seen below. The first command simply downloads the package, and the second command starts the installation. To skip the installation of the testing suite and other packages that the average user won’t need, simply use the
--no-devoption with the
curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-dev
Requiring the Autoload File for Composer
- Composer generates an autoload file which will autoload all of the classes in the library. Add the following code to the PHP file’s bootstrap process:
$client = ClientBuilder::create()->build();
For more information on setting up Composer and configuring the autoload process, visit Composer’s website
- Once the installation process is complete, the PHP client for Elasticsearch is ready for use. In most cases, the next task will involve configuring the client. Nearly all aspects of the client can be configured, but the majority of users will only need to configure a couple of parameters to meet their needs.
It’s clear that the PHP client for Elasticsearch can bring fast, robust search functionality to your PHP website. With the help of Composer, the installation process on any Unix-based system is a breeze. The key to a successful install is to be aware of the system requirements and to follow the directions carefully. With these strategies in place, users will have no problem getting started with the Elasticsearch PHP client.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started