How to install OSRM and Nominatim on Ubuntu 16.02 ( Google Maps Alternative )

Introduction

OSRM – Open Street Routing Machine is a open source C++ project and Nominatim is a tool for synthesising address from OSM – Open Source Map data. Have a look at both of them here. These applications are used widely by many organisations to supplement their Map requirements. I am assuming the readers are familiar with the Google Maps API, you can have a look at their analogous functions with OSRM.

Google Maps – Directions API         –      OSRM-Backend route

Google Maps – Distance Matrix      –      OSRM-Backend table ( contains only duration )

Google Maps – Geo-coding API       –      Nominatim

Google Maps – Road API                  –      OSRM-Backend match

In case you have the requirement for the above APIs or any here , go ahead to install OSRM and Nominatim.

Requirements

Have a look at the size of the osm pbf you would want to use at geofabrik . File size ranges from 100 MB – 33 GB. Your RAM requirements would range from 512 MB – 8 GB and your Swap requirements would range from 1 GB  – 100 GB. So, be generous while allocating swap to avoid multiple trials installing. Tutorial to create swap .

Installation

To keep the OS updated.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install unattended-upgrades

Go to directory where you would like to have osrm

mkdir osrm
cd osrm
mkdir data
cd data
wget -O map.osm.pbf url_of_the_pbf

The url_of_the_pbf  file you got from geofabrik.
Install the dependencies

sudo apt install build-essential git cmake pkg-config \
libbz2-dev libstxxl-dev libstxxl1v5 libxml2-dev \
libzip-dev libboost-all-dev lua5.2 liblua5.2-dev libluabind-dev libtbb-dev

Installing OSRM

cd ..
git clone https://github.com/Project-OSRM/osrm-backend.git
cd osrm-backend
mkdir build
cd build
cmake ..
sudo make install
cd ..
cd ..

osrm-extract data/map.osm.pbf -p osrm-backend/profiles/car.lua

osrm-extract command may not execute successfully. It depends on the map.osm.pbf file. If it does well and good else look for a older versions of file. Also it could be memory issue try using a bigger RAM and swap.

osrm-contract data/map.osrm

osrm-contract command takes long time to execute. It might fail due to small swap. Again I would give the same advice, try increasing your swap and RAM.

osrm-routed data/map.osrm

Voilaa.. you have it running on your http://localhost:5000.

Try the following command to
curl "http://127.0.0.1:5000/route/v1/driving/source_longitude,source_latitude;destination_longitude,destination_latitude?steps=true&alternatives=true&geometries=geojson"
source_longitude, source_latitude, destination_longitude and destination_latitude should be from the country/region you have downloaded.

Installing Nominatim – Dependencies

sudo apt-get update -qq
sudo apt-get upgrade -y
sudo apt-get install -y build-essential cmake g++ libboost-dev libboost-system-dev \
libboost-filesystem-dev libexpat1-dev zlib1g-dev libxml2-dev \
libbz2-dev libpq-dev libgeos-dev libgeos++-dev libproj-dev \
postgresql-server-dev-9.5 postgresql-9.5-postgis-2.2 postgresql-contrib-9.5 \
apache2 php php-pgsql libapache2-mod-php php-pear php-db \
git

chmod a+x .

Installing Nominatim

git clone --recursive git://github.com/twain47/Nominatim.git
cd Nominatim
mkdir build
cd build
cmake ..
make

sudo -su postgres
./utils/setup.php --osm-file ../../data/map.osm.pbf --all 2>&1 | tee setup.log
exit

sudo -u postgres createuser www-data
sudo -u postgres psql nominatim
GRANT usage ON SCHEMA public TO "www-data";
GRANT SELECT ON ALL TABLES IN SCHEMA public TO "www-data";
\q

Point your Server to Nominatim/build/website/ by setting DocumentRoot in /etc/apache2/sites-enabled/000-default.conf.
Also grant permissions to the osrm Directory in /etc/apache2/apache2.conf
Restart your apache server

sudo service apache restart

You should get your server running on your http://localhost/ . By default its port 80. Try the following command to verify.

curl "http://127.0.0.1/reverse.php?format=json&lat=<latitude>&lon=<longitude>&zoom=18

<latitude>, <longitude> should be from the country/region you have downloaded.

So guys, its just getting started with free usage for Maps. Theres a long road ahead, keep it going.

To setup your own tile server and provide free maps for your web applications you can read the following blog.

  • Robert Sellers

    Nice walkthrough. This worked well for me. In case anyone else has this issue, I got Nominatim working with “sudo service apache2 restart”.

    • Carsten Harder

      works with sudo systemctl [reload, enable, status] apache2 here

  • Carsten Harder

    Hi super tutorial. Two points:

    1.) sudo -u postgres createuser www-data needs to be created in front of the command ./utils/setup.php –osm-file ../../data/map.osm.pbf –all 2>&1 | tee setup.log because this command expects the user www-data to be there

    2.) In front of both you need to download wget -O /home/carsten/osrm/Nominatim/data/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz because the command “./utils/setup.php –osm-file ../../data/map.osm.pbf –all 2>&1 | tee setup.log” expects it to be there too.