How to install OSRM backend on Ubuntu 20.04.1 LTS


How to install OSRM backend on Ubuntu 20.04.1 LTS

What is OSRM?

The Open Source Routing Machine or OSRM is a C++ implementation of a high-performance routing engine for shortest paths in road networks. Licensed under the permissive 2-clause BSD license, OSRM is a free network service. OSRM supports Linux, FreeBSD, Windows, and Mac OS X platform.

Tutorial used from Digital Ocean using Geofabrik Map.

Before digging into, you can check Initial Server Setup with Ubuntu 14.04. I added 4GB swap memory using this tutorial.

Please note that the structure for your folders should be

  • ROOT
    • osrm
      • map.osm.pbf
    • osrm-backend (this is the repo cloned. info how to build also here)

See below for required dependencies.

mkdir -p build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .
sudo cmake --build . --target install

To extract the maps you should be in the root.

~$ osrm-extract osrm/map-latest.osm.pbf -p ./osrm-backend/profiles/car.lua

How to run osrm locally:

osrm-routed /home/osrm/osrm/map.osrm

Setup Nginx

Point 9 is described here, but to summary let’s dive in:

Install Nginx

sudo apt-get install nginx

Create a new configuration file in sites-available

sudo nano /etc/nginx/sites-available/osrm.conf

Now fill in the content:

upstream osrm {
    server 0.0.0.0:5000;
}

server {
    listen 80;
    server_name your_IP_or_DNS;

    location / {
        proxy_pass http://osrm/;
        proxy_set_header Host $http_host;
    }
}

Then we should activate this configuration, by creating a link to sites-enabled.

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/osrm.conf

Don’t forget to test the configuration. It should pass the syntax check.

nginx -t

Reload the configuration of Nginx

sudo service nginx reload

and start the server

sudo service nginx start

Open ports

Maybe seems stupid, but please be sure that your machine has inbound ports open for port 80 and/or 443.

Execute OSRM backend as Linux Daemon

We need to create first the service file

sudo nano /etc/systemd/system/osrm.service

Then fill in the content

[Unit]
Description=Daemon for osrm backend

[Service]
ExecStart= /usr/local/bin/osrm-routed /home/osrm/osrm/map.osrm
WorkingDirectory=/home/osrm/osrm/
Restart=always
User=osrm

[Install]
WantedBy=multi-user.target

Please take into consideration the folder structure discuss previously. So as you can see we have a Service block where we specify the start command, but using absolute path osrm-routed /home/osrm/osrm/map.osrm. Then the working directory which in our case is ~/osrm.

Press CTRL+X and save.

Enable the service. It will start automatically on boot, after that.

$ sudo systemctl enable osrm.service

Check status/start/stop/restart

$ sudo systemctl {status|start|stop|restart} osrm

A complete list with all available directives that can be used inside the service file can be found here

Test the api

curl "http://your_IP_or_DNS/route/v1/driving/route/v1/driving/source_longitude,source_latitude;destination_longitude,destination_latitude?steps=true&alternatives=true&geometries=geojson"

Official documentation from OSRM API can be found here

Newsletter


Related Posts

TikTok Ads Strategy from a SaaS founder

Here's a short tutorial from my disrupting strategy of doing Tiktok Ads as a SaaS Founder

Curated SEO Resources: Essential Tools and Tips

Discover a handpicked selection of indispensable SEO resources, including tools and tips, to optimize your website's performance.

A Week in the Life of an Invoice Wrangler: Navigating Ridesharing and Food Delivery Chaos

As an app founder in the ridesharing and food delivery industry, I found myself knee-deep in invoice reports from companies like Bolt, Uber, Glovo, and Bolt Food

Free HTML templates list for Startups

Free HTML templates list for startup. A complete list with free resources to build your next startup's website and gain the traction to the sky.

Deal with client requests in SaaS

How to deal with client requests in Saas which are seeing only their interests, not the product interest.

The first client of LoyalXpert is not answering anymore

Trying to implement LoyalXpert app, I lost my first customer, he's not answering anymore

Experiments with Tiktok Ads

Recently tried out TikTok ads for the first time and here are some of my learnings and challenges

People don’t care about you, until they know you care about them.

People don’t care about you, until they know you care about them. The same happens in business, you need to take care of your clients.

The One Word That Can Ruin Your SaaS Business Anyone

As a SaaS founder, you probably know how important it is to have a clear and specific target market for your product.

How I got my digital certificate connected it with ANAF

How I got my digital certificate from certSIGN and connected it with ANAF