How to Install OSRM on Ubuntu 20.04 – Open Source Routing Machine

OSRM (Open Source Routing Machine) is a super fast routing engine for OpenStreetMap (OSM) road networks. In previous tutorials, we explained the process of setting up a self-hosted OpenStreetMap tile server and how to add address lookup functionality to your map with Nominatim. This tutorial is going to show you how to add navigation functionality to your OpenStreetMap with OSRM, which provides car routing, bike routing, and walk routing.

This tutorial also works on Ubuntu 18.04.

Install OSRM on Ubuntu 20.04


To follow this tutorial, you should have an OSM tile server up and running. If not, please follow the tutorial below to set up your own OSM tile server.

You should also have access to a geocoding service like Nominatim for address lookup.

Once the requirements are met, follow the instructions below to set up OSRM server.

Step 1: Build OSRM From Source

Install dependency packages.

sudo apt update

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

Create the osrm user. (No need to create a password for this user.)

sudo useradd -d /srv/osrm -s /bin/bash -m osrm

Grant permissions to your own user account.

sudo apt install acl

sudo setfacl -R -m u:username:rwx /srv/osrm/

Change to the /srv/osrm/ directory.

cd /srv/osrm/

Download the OSRM source code from its Github repository.

git clone

Create the build directory.

mkdir build

Change to this directory and configure the build environment.

cd build

cmake /srv/osrm/osrm-backend/

Compile the source code.


build OSRM from source ubuntu 20.04

Install the binaries.

sudo make install

The following binaries will be installed.

  • /usr/local/bin/osrm-extract:
  • /usr/local/bin/osrm-partition:
  • /usr/local/bin/osrm-customize:
  • /usr/local/bin/osrm-contract:
  • /usr/local/bin/osrm-datastore:
  • /usr/local/bin/osrm-routed:

Step 2: Install GNU Screen

In the next step, we will need to extract road networks from OpenStreetMap, which can take a long time. Your computer might be disconnected from the Internet, so it’s recommended to use the GNU Screen utility to keep your session alive. Install screen on the Ubuntu 20.04 server:

sudo apt install screen

Then start screen:


Upon the first launch, you will see an introduction text, simply press Enter to end. Then you will be able to run commands as usual.

Step 3: Generate OSRM Routing Data

Now we need to download the OpenStreetMap data and make it usable for routing. Run the following command to download the map data of the whole planet (56G) in PBF (ProtoBufBinary) format.

wget -c -P /srv/osrm/osrm-backend

If you want a map of an individual country/state/province/city, go to For example, download the map data of Great Britain (1.2G) with the following command.

wget -c -P /srv/osrm/osrm-backend also provides extracts of more than 200 cities and regions worldwide in different formats.

Make sure you are in the /srv/osrm/osrm-backend/ directory.

cd /srv/osrm/osrm-backend/

Extract a graph out of the OpenStreetMap data.

osrm-extract britain-and-ireland-latest.osm.pbf --threads=10

By default, it will use the car.lua profile.

Generate OSRM Routing Data ubuntu 20.04

Now you probably don’t need to do other things on your server. Since you are using Screen, you can press Ctrl+A, release those keys, and then press D key to detach from the current Screen session. You will see a message like below.

[detached from 32113.pts-1.focal]

This tells me that the previous Screen session ID is 32113. You can log out from the SSH session and even shut down your computer. Don’t worry, the osrm-extract process is still running. When you need to come back and check the progress, SSH into your server and run the following command to get the previous Screen Session ID.

screen -ls

Sample output:

There is a screen on:
	32113.pts-1.focal	(05/19/2020 03:45:29 PM)	(Detached)
1 Socket in /run/screen/S-linuxbabe.

Then you can re-attach to the previous Screen session.

screen -r 32113

This process is memory intensive. (It uses 7GB RAM on my server.) Once it’s finised, there will be a file with the same filename but with the .osrm extension. Run the following command to partition this graph recursively into cells

osrm-partition britain-and-ireland-latest.osrm

osrm-partition ubuntu 20.04

Customize the cells by calculating routing weights for all cells.

osrm-customize britain-and-ireland-latest.osrm

osrm-customize ubuntu 20.04

Now you can start the routing engine.

osrm-routed --algorithm=MLD britain-and-ireland-latest.osrm

osrm-routed ubuntu 20.04

As you can see, it listens on TCP port 5000.

Step 4: Creating a systemd service

We can manually run the OSRM routing engine with osrm-routed --algorithm=MLD britain-and-ireland-latest.osrm, but it’s more convenient to run osrm-routed as a systemd service in the background. Press Ctrl+C to stop the current osrm-routed process and create a systemd service unit file for osrm-routed with the following command.

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

Put the following lines into the file.

Description=Open Source Routing Machine

ExecStart=/usr/local/bin/osrm-routed --algorithm=MLD /srv/osrm/osrm-backend/britain-and-ireland-latest.osrm


Save and close the file. Change the ownership of the /srv/osrm/osrm-backend/ directory.

sudo chown osrm:osrm /srv/osrm/osrm-backend/ -R

Now we can start and enable the osrm-routed systemd service.

sudo systemctl start osrm-routed

sudo systemctl enable osrm-routed

Check status.

systemctl status osrm-routed

osrm-routed systemd service ubuntu 20.04

If the osrm-routed service isn’t active (running), you can run the following command to see what’s wrong.

sudo journalctl -eu osrm-routed

Step 5: Configure Apache web server

We can configure Apache web server as a reverse proxy for the osrm-routed service, so we can use a domain name to access the routing service and also enable HTTPS encryption.

Install Apache web server.

sudo apt install apache2

To use Apache as a reverse proxy, we need to enable the proxy, proxy_http and rewrite module.

sudo a2enmod proxy proxy_http rewrite

Then create a virtual host file for OSRM.

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

Add the following texts into the file. Replace with your actual domain name and don’t forget to create DNS A record for it.

<VirtualHost *:80>

    ProxyPass /
    ProxyPassReverse /

    # Only allow authorizied domains to use this service.
    SetEnvIf Origin "^http(s)?://(.+\.)?(example\.com|otherdomain\.tld)$" origin_is=$0
    Header unset Access-Control-Allow-Origin
    Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is

Save and close the file. Then enable this virtual host.

sudo a2ensite osrm.conf

Reload Apache for the changes to take effect.

sudo systemctl reload apache2

Now you can remotely access OSRM by entering the domain name ( ) in browser address bar.

Step 6: Enable HTTPS

We can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. In the OSM tile server setup tutorial, we have already install the Let’s Encrypt client (certbot) from the Snap store. So we just need to run the following command to obtain and install TLS certificate.

sudo /snap/bin/certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d


  • --apache: Use the Apache plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.

The certificate should now be obtained and automatically installed. And you will be able to access Webmin web interface over a secure HTTPS connection.

osrm https certbot ubuntu 20.04

Step 7: Integrate OSRM with a Slippy Map

I assume your slippy map is displayed using the Leaflet JavaScript library, and you have added Nominatim geocoding service to your slippy map.

To integrate OSRM with a slippy map, we can use a plugin called Leaflet Routing Machine. First, include the Leaflet routing machine JavaScript and CSS file to your slippy map. Note that they should be placed after the main Leaflet JavaScript and the Leaflet Control Geocoder JavaScript.

     <link rel="stylesheet" href="[email protected]/dist/leaflet-routing-machine.css" />
     <script src="[email protected]/dist/leaflet-routing-machine.js"></script>

Next, add the following lines to the <script>...</script> snippet in the HTML body.

         serviceUrl: '',
         geocoder: L.Control.Geocoder.nominatim({serviceUrl:''}),
         routeWhileDragging: true

Like this:

     <link rel="stylesheet" href="[email protected]/dist/leaflet-routing-machine.css" />
     <script src="[email protected]/dist/leaflet-routing-machine.js"></script>
   <div id="map"></div>

         serviceUrl: '',
         geocoder: L.Control.Geocoder.nominatim({serviceUrl:''}),
         routeWhileDragging: true



Save and close the file. Then reload the map in your web browser, you should see a control panel on the upper-right corner, where you can enter the starting address and destination address.

Install OSRM on Ubuntu 20.04

You can drag the waypoints on the map and OSRM will automatically recalculate the route.

Wrapping Up

I hope this tutorial helped you set up OSRM server on Ubuntu 20.04. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 4 Average: 5]

14 Responses to “How to Install OSRM on Ubuntu 20.04 – Open Source Routing Machine

  • joseph
    1 year ago

    Hi, thanks for the other two tutorials, they work great. I had to do a bit extra to get them working in a proxmox container with ZFS, and then working with pfsense and cloudflare.

    I have been trying to get this last tutorial working, but I don’t know where to check for error information. When I try it in an empty LXC it still errors out.

    [info] Generating edge-expanded graph representation
    [info] . 10% . 20% . 30% . 40% . 50% . 60% . 70% . 80% . 90% . 100%
    [info] Node compression ratio: 0.20656
    [info] Edge compression ratio: 0.24589

    The server has 256GB, the vm is allotted 128GB with a 40GB scratch an 200GB free disk space.
    Any insight appreciated, but the know-how to see what killed my poor machine would be nice.

    NOTE: I am for now on using your amazon link for all future purchases=)

    • Xiao Guoan (Admin)
      4 days ago

      Probably your server runs out of RAM.

  • joseph
    1 year ago

    disregard my post- delete….
    I added more memory, restarted the lxc, and stopped it from pre-rendering tiles.
    Then the process completed, but I seem to have trouble rendering tiles.
    I’ll work on this for another day and see if I can see something in journalctl.

    • tooneamelt
      1 year ago

      If you also followed the guide to set up nominatim, I then had to give permissions again to osm on gis. My server wouldn’t render after nominatim but after reapplying the osm privilege to gis, it started working again.
      The error was in renderd journalctl saying the postgres server disconnected unexpectedly

    • tooneamelt
      1 year ago

      Sorry, I missed saying you need to give permissions to osm on the gis scheme in postgres

  • tooneamelt
    1 year ago

    I’d recommend adding swap if you have the space for it, as osrm-extract can be much faster if you add threads (if you have them)

    sudo mkdir /media
    sudo dd if=/dev/zero of=/media/swapfile.img bs=1024 count=100M # 1M=1GiB, so change count to whatever you can afford from your drive
    sudo mkswap /media/swapfile.img
    sudo swapon /media/swapfile.img
  • [info] Parsing in progress..
    [info] input file generated by osmium/1.8.0
    [info] timestamp: 2019-09-14T20:15:02Z
    [info] Using profile api version 4
    [info] Found 3 turn restriction tags:
    [info] motorcar
    [info] motor_vehicle
    [info] vehicle
    [info] Parse relations …
    [info] Parse ways and nodes …
    [info] Using profile api version 4
    [info] Using profile api version 4
    [info] Using profile api version 4
    [info] RAM: peak bytes used: 1191161856
    [error] [exception] std::bad_alloc
    [error] Please provide more memory or consider using a larger swapfile

  • How do I change the route instructions unit to miles? I tried switching line 16270 in leaflet-routing-machine.js to ‘imperial’ but it didn’t seem to change anything.

  • Thiago Guedes
    3 months ago

    Boa tarde , fiz as configurações. Porém quando acesso no navegador esta abrindo uma pagina do apache . Poderia me ajudar?


    • Xiao Guoan (Admin)
      4 days ago

      Use a real domain name instead of an IP address.

  • Hi

    I am running into fallowing issue which throws an fatal error and exits the script. I already try to find a solution via google but cant….

    This is the error …

    command: cmake /srv/osrm/osrm-backend/

    Scanning dependencies of target EXTRACTOR
    [  0%] Building CXX object CMakeFiles/EXTRACTOR.dir/src/extractor/compressed_edge_container.cpp.o
    [  2%] Building CXX object CMakeFiles/EXTRACTOR.dir/src/extractor/edge_based_graph_factory.cpp.o
    /srv/osrm/osrm-backend/src/extractor/edge_based_graph_factory.cpp:35:10: fatal error: tbb/parallel_pipeline.h: No such file or directory
       35 | #include 
    • I have this exact same error, and I would also like to know an answer! If you find out a solution please let me know. I have tried reinstalling tbb libraries, hardcoding it to path, etc. Nothing is working!

      • Hi i fixed my problem by


            apt-get update && \
            apt-get -y --no-install-recommends install ca-certificates cmake make git gcc g++ libbz2-dev libxml2-dev wget \
            libzip-dev libboost1.74-all-dev lua5.4 liblua5.4-dev -o APT::Install-Suggests=0 -o APT::Install-Recommends=0


            NPROC=${BUILD_CONCURRENCY:-$(nproc)} && \
            ldconfig /usr/local/lib && \
            git clone --branch v2021.3.0 --single-branch && \
            cd oneTBB && \
            mkdir build && \
            cd build && \
            cmake -DTBB_TEST=OFF -DCMAKE_BUILD_TYPE=Release ..  && \
            cmake --build . && \
            cmake --install .


        cd /srv/osrm/
        git clone
        mkdir build
        cd build
        cmake /srv/osrm/osrm-backend/

        Found this solution on the github page of the osrm backend

        Hope that this will help you!

    • Xiao Guoan (Admin)
      1 week ago

      Are you using Ubuntu 22.04? It currently doesn’t work.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here