Building a Local OSRM Instance

September 11, 2017

[This article was first published on R on datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The Open Source Routing Machine (OSRM) is a library for calculating routes, distances and travel times between spatial locations. It can be accessed via either an HTTP or C++ API. Since it’s open source you can also install locally, download appropriate map data and start making efficient travel calculations.

These are the instructions for getting OSRM installed on a Ubuntu machine and hooking up the osrm R package.


Building OSRM is memory intensive, so unless you are installing on a machine with a good chunk of RAM, you’ll want to ensure that there’s around 4 Gb of swap space available.

Building OSRM

First make sure that you have the necessary infrastructure and libraries required to build OSRM.

sudo apt update
sudo apt install -y git cmake build-essential jq
sudo apt install -y liblua5.2-dev libboost-all-dev libprotobuf-dev libtbb-dev libstxxl-dev libbz2-dev

Now grab the source directly from the repository on GitHub.

git clone

Move into the source folder.

cd osrm-backend/

Although you could probably just build from the current state of the repository, it might make sense to choose the most recent release. So, for example, to checkout version v5.16.4:

git checkout v5.16.4

Create a build folder and then run cmake to generate Makefiles.

mkdir build
cd build/
cmake ..

Next initiate the build.


Time to kick back and wait: this will take some time!

When the build completes, make the install target.

sudo make install

Getting OpenStreetMap Data

OpenStreetMap Export

Go to the export page on OpenStreetMap. Zoom in on you area of interest and then press the Export button. The area I was working with was too large to download directly from OpenStreetMap, so I followed the link to the Overpass API, which worked flawlessly.

I’m installing on a remote instance, so I used wget to do the download.

wget -O map.xml,-30.1160,31.2616,-29.3882

The resulting download will be a (possibly rather large) XML file. Move it to the osrm-backend folder created above.

Data from Geofabrik

You can also download the OpenStreetMap data in a variety of formats from Geofabrik.

Extracting the Map

In the profiles folder you’ll find three files (bicycle.lua, car.lua and foot.lua) which provide speed profiles for various means of transportation. You can create a custom profile if necessary, but the ones provided will suffice for most situations. We’ll go with the car profile.

The next step is to extract the routing data. This can be very memory intensive, so make sure that you have sufficient swap space or that you’re using STXXL.

osrm-extract map.xml -p profiles/car.lua

Creating a Hierarchy

Now to create data structures that facilitate finding the shortest route between two points.

osrm-contract map.xml.osrm

Launching the Service

We can launch a HTTP server which exposes the OSRM API as follows:

osrm-routed map.xml.osrm

Let’s try a few test queries. First we’ll find the nearest road to a location specified by a longitude/latitude pair.

curl "http://localhost:5000/nearest/v1/driving/31.043515,-29.778562" | jq
  "waypoints": [
      "distance": 37.79936,
      "name": "St Andrews Drive",
      "location": [
  "code": "Ok"

Next the distance and time between two locations.

curl ",-29.778562;31.029080,-29.795506" | jq
  "code": "Ok",
  "routes": [
      "geometry": "xcwtDggn|DfHbC|AjHnFjBxAlNdDjAwA|[email protected]`KtAL|@[email protected]@[email protected]",
      "legs": [
          "steps": [],
          "distance": 2926.9,
          "duration": 357.6,
          "summary": "",
          "weight": 357.6
      "distance": 2926.9,
      "duration": 357.6,
      "weight_name": "routability",
      "weight": 357.6
  "waypoints": [
      "name": "St Andrews Drive",
      "location": [
      "name": "Gainsborough Drive",
      "location": [

The duration values are in seconds and the distance is in metres. Looks pretty legit!

More information on the API can be found here.

If you were wanting to expose this service to the outside world then you’d need to integrate it with your web server and maybe set up something to restart the service if the machine reboots. I’m only planning on using OSRM locally, so these are not issues for me.

The osrm R Package

My primary motivation for setting up OSRM is so that I can use it from within R.

First install a couple of packages.

sudo apt install -y libcurl4-openssl-dev libgeos-dev

Now install the osrm package.


Load the package and point it at the local OSRM service.

> library(osrm)
Data (c) OpenStreetMap contributors, ODbL 1.0.
Routes: OSRM.
If you plan to use the OSRM public API, read the OSRM API Usage Policy:
> options(osrm.server = "")

Now create a couple of locations.

> locations = data.frame(comm_id = c("A", "B", "C"),
+                        lon = c(31.043515, 31.029080, 31.002896),
+                        lat = c(-29.778562, -29.795506, -29.836168))

Generate a table of travel times between those locations.

> osrmTable(loc = locations)
     A   B    C
A  0.0 6.0 11.9
B  6.0 0.0  9.6
C 11.5 9.7  0.0

       lon       lat
A 31.04388 -29.77868
B 31.02913 -29.79539
C 31.00286 -29.83625

       lon       lat
A 31.04388 -29.77868
B 31.02913 -29.79539
C 31.00286 -29.83625

Calculate the optimal route between two locations.

> route = osrmRoute(src = locations[1,], dst = locations[2,], sp = TRUE)
> route$duration
[1] 5.96
> route$distance
[1] 2.9269

The units are now minutes for duration and kilometres for distance.

I’ve been using the gmapsdistance package until now. It has worked brilliantly but I’ve had to manage it carefully to avoid overstepping API limits. With a local OSRM I’ll be making the calculations unconstrained!


An AWS instance is a good option for running an OSRM instance.

A few things to consider:

  • potentially use OpenStreetMap on AWS as a data source;
  • make sure that port 5000 is open in the security group applied to your instance.

To leave a comment for the author, please follow the link and comment on their blog: R on datawookie. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)