First, see here.

Edit /etc/environment and add ES_HEAP_SIZE=1g. Edit /etc/sysctl.conf and add:

fs.file-max=32000 vm.max_map_count=262144

Edit /etc/security/limits.conf and add root - nofile 32000. Edit /opt/elasticsearch/config/elasticsearch.yml and set bootstrap.mlockall: true.

Finally, restart the VM.