How to Set Up OpenStreetMap Tile Server on Ubuntu 20.04

OpenStreetMap (OSM) is a user-contributed, freely-editable world map. You can think of it as an open-source and self-hosted alternative to Google Maps. This tutorial will show you how to build your own OpenStreetMap tile server on Ubuntu 20.04 so you don’t have to use a proprietary map service.

OpenStreetMap Features

  • OpenStreetMap data covers the whole world, making it easy to support users in any country or every country.
  • OpenStreetMap is updated every minute of every hour of every day, and these updates are available to you in real-time.
  • OpenStreetMap data is free and open – there is no subscription fee and no page-view fee.
  • OpenStreetMap data is rich and detailed, containing huge amounts of data that is relevant to people on the ground – the people who collected it.

Prerequisites/Hardware Requirements

The required RAM and disk space depend on which country’s map you are going to use. For example,

  • The UK map requires at least 12G RAM and 100GB disk space.
  • The whole planet map requires at least 32G RAM and 1TB SSD (Solid State Drive). It’s not viable to use a spinning hard disk for the whole planet map.

You will need more disk space if you are going to pre-render tiles to speed up map loading in the web browser, which is highly recommended. Check this tile disk usage page to see how much disk space are required for pre-rendering tiles. For example, if you are going to pre-render tiles from zoom level 0 to zoom level 15 for the planet map, an extra 460 GB disk space is required.

Another thing to note is that importing large map data, like the whole planet, to PostgreSQL database takes a long time. Consider adding more RAM and especially using SSD instead of spinning hard disk to speed up the import process.

If you are going to host the entire world map, I recommend you buy the extra-large VPS from Contabo, which boasts

  • A 10 core CPU
  • 60 GB RAM
  • 1.6 TB Intel Optane SSD

It costs just 26.99 €/month.

Step 1: Upgrade Software

It’s always a good practice to update server software before doing any major work on your server. Log into your server via SSH and run the following command.

sudo apt update; sudo apt upgrade

Step 2: Install PostgreSQL Database Server and the PostGIS Extension

We will use PostgreSQL to store map data. PostGIS is a geospatial extension to PostgreSQL. Run the following commands to install them.

sudo apt install postgresql postgresql-contrib postgis postgresql-12-postgis-3

PostgreSQL database server will automatically start and listens on The postgres user will be created on the OS during the installation process. It’s the super user for PostgreSQL database server. By default, this user has no password and there’s no need to set one because you can use sudo to switch to the postgres user and log into PostgreSQL server.

sudo -u postgres -i

Now you can create a PostgreSQL database user osm.

createuser osm

Then create a database named gis and at the same time make osm as the owner of the database. -E UTF8 specifies the character encoding scheme to be used in the database is UTF8.

createdb -E UTF8 -O osm gis

Next, create the postgis and hstore extension for the gis database.

psql -c "CREATE EXTENSION postgis;" -d gis

psql -c "CREATE EXTENSION hstore;" -d gis

Set osm as the table owner.

psql -c "ALTER TABLE spatial_ref_sys OWNER TO osm;" -d gis

Exit from the postgres user.


Create osm user on your operating system so the tile server can run as osm user. The following command will create a system user without password.

sudo adduser --system --group osm

Step 3: Download Map Stylesheet and Map Data

Change to osm’s home directory.

cd /home/osm/

Download the latest CartoCSS map stylesheets to the osm user’s home directory with git.

sudo apt install git

git clone

If you see “permission denied” error while running the above command, then you can grant permissions with the following command. Replace username with your real username.

sudo apt install acl
sudo setfacl -R -m u:username:rwx /home/osm/

Next, run the following command to download the map data of the whole planet (50G) in PBF (ProtoBufBinary) format.

wget -c

Note that download speeds for are currently restricted to 2048 KB/s. You can download the plant map from another mirror, like

wget -c

If you want a map of individual country/state/province/city, go to Also, provides extracts of more than 200 cities and regions worldwide in different formats. For example, download the map data of Great Britain (1.1G) with the following command.

wget -c

Step 4: Optimize PostgreSQL Server Performance

The import process can take some time. To speed up this process, we can tune some PostgreSQL server settings to improve performance. Edit PostgreSQL main configuration file.

sudo nano /etc/postgresql/12/main/postgresql.conf

First, we should change the value of shared_buffer. The default setting is:

shared_buffers = 128MB

This is too small. The rule of thumb is to set it to 25% of your total RAM (excluding swap space). For example, my VPS has 60G RAM, so I set it to:

shared_buffers = 15GB

Find the following line.

#work_mem = 4MB
#maintenance_work_mem = 64MB

Again, the value is too small. I use the following settings.

work_mem = 1GB
maintenance_work_mem = 8GB

Then find the following line.

#effective_cache_size = 4GB

If you have lots of RAM like I do, you can set a higher value for the effective_cache_size like 20G.

effective_cache_size = 20GB

Save and close the file. Restart PostgreSQL for the changes to take effect.

sudo systemctl restart postgresql

By default, PostgreSQL would try to use huge pages in RAM. However, Linux by default does not allocate huge pages. Check the process ID of PostgreSQL.

sudo head -1 /var/lib/postgresql/12/main/

Sample output:


Then check the VmPeak value of this process ID.

grep ^VmPeak /proc/7031/status

Sample output:

VmPeak: 16282784 kB

This is the peak memory size that will be used by PostgreSQL. Now check the size of huge page in Linux.

cat /proc/meminfo | grep -i huge

Sample output:

AnonHugePages:         0 kB
ShmemHugePages:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB

We can calculate how many huge pages we need. Divide the VmPeak value by the size of huge page: 16282784 kB / 2048 kB = 7950. Edit /etc/sysctl.conf file.

sudo nano /etc/sysctl.conf

Add the following line at the end of this file to allocate 7950 huge pages.

vm.nr_hugepages = 7950

postgresql hugepages

Save and close the file. Then apply the changes.

sudo sysctl -p

If you check the meminfo again,

cat /proc/meminfo | grep -i huge

We can see there are 7950 huge pages available.

AnonHugePages:         0 kB
ShmemHugePages:        0 kB
HugePages_Total:    7950
HugePages_Free:     7950
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

Restart PostgreSQL to use huge pages.

sudo systemctl restart postgresql

Use Screen on Remote Servers

Since the import process can take a long time and your computer might be disconnected from Internet, it’s recommended to use the screen utility to keep your session alive. Install screen on the Ubuntu 20.04 server:

sudo apt install screen

Then start screen:


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

Step 5: Import the Map Data to PostgreSQL

To import map data, we need to install osm2pgsql which converts OpenStreetMap data to postGIS-enabled PostgreSQL databases.

sudo apt install osm2pgsql

Grant permissions to the postgres user.

sudo setfacl -R -m u:postgres:rwx /home/osm/

Switch to the postgres user.

sudo -u postgres -i

Run the following command to load map stylesheet and map data into the gis database. Replace great-britain-latest.osm.pbf with your own map data file.

osm2pgsql --slim -d gis --hstore --multi-geometry --number-processes 10 --tag-transform-script /home/osm/openstreetmap-carto/openstreetmap-carto.lua --style /home/osm/openstreetmap-carto/ -C 32000 /home/osm/great-britain-latest.osm.pbf


  • --slim: run in slim mode rather than normal mode. This option is needed if you want to update the map data using OSM change files (OSC) in the future.
  • -d gis: select database.
  • --hstore: add tags without column to an additional hstore (key/value) column to PostgreSQL tables
  • --multi-geometry: generate multi-geometry features in postgresql tables.
  • --style: specify the location of style file
  • --number-processes: number of CPU cores on your server. I have 10.
  • -C flag specifies the cache size in MegaBytes. It should be around 70% of the free RAM on your machine. Bigger cache size results in faster import speed. For example, my server has 60GB free RAM, so I can specify -C 32000. Be aware that PostgreSQL will need RAM for shared_buffers. Use this formula to calculate how big the cache size should be: (Total RAM - PostgreSQL shared_buffers) * 70%
  • Finally, you need to specify the location of map data file.

Command Output:


If you are going to import the full planet map data, then use the --drop option and the --flat-nodes option to increase the import speed. Note that the --flat-nodes option isn’t suitable for small maps.

osm2pgsql --slim -d gis --drop --flat-nodes /home/osm/nodes.cache --hstore --multi-geometry --number-processes 10 --tag-transform-script /home/osm/openstreetmap-carto/openstreetmap-carto.lua --style /home/osm/openstreetmap-carto/ -C 32000 /home/osm/planet-latest.osm.pbf

RAM usage will gradually increase during the importing process.

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 OSM import process is still running. When you need to come back and check the import 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

And you will be able to continue your work. Once the import is complete, grant all privileges of the gis database to the osm user.


Exit from the postgres user.


Note: If the osm2pgsql import isn’t finished yet, please don’t continue with step 6.


If you encounter the following error when importing map data from PBF file,

PBF error : invalid Blobheader size (> max_blob_header_size)

it’s probably because your PBF file is corrupt. Verify the md5sum of the PBF file like below.

md5sum great-britain-latest.osm.pbf

Compare the result with the md5sum value on the PBF file download page.

Step 6: Install mod_tile and Renderd

mod_tile is an Apache module that is required to serve tiles and renderd is the rendering daemon for rendering OpenStreetMap tiles. The default Ubuntu repository does not include mod_tile and renderd, but we can install them from the OSM PPA.

sudo apt install software-properties-common

sudo add-apt-repository ppa:osmadmins/ppa

sudo apt install libapache2-mod-tile renderd

During the installation, it will install Apache web server and ask if you want to enable mod_tile in the Apache config. Select Yes and press Enter. This will create an Apache config file for mod_tile (/etc/apache2/sites-available/tileserver_site.conf).

apache mod_tile

The render daemon will automatically start, as can be seen with:

systemctl status renderd

Step 7: Generate Mapnik Stylesheet

Install the required packages.

sudo apt install curl unzip gdal-bin mapnik-utils libmapnik-dev python3-pip

We also need to install nodejs and npm from the upstream repository with the following commands.

curl -sL | sudo -E bash -
sudo apt-get install -y nodejs

Then install the carto package with npm.

sudo npm install -g carto

Install the psycopg2 Python module.

sudo -H pip3 install psycopg2==2.8.5

Switch to the postgres user.

sudo -u postgres -i

Cd into the carto style directory.

cd /home/osm/openstreetmap-carto/

Get shapefiles.


If you encounter the following error message while running the above command, then you have DNS issues. Simply wait for several minutes and run the Python script again.

Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

Now build the Mapnik XML stylesheet with the carto map stylesheet compiler.

carto project.mml > style.xml

Grant all privileges of the gis database to the osm user.


Exit from the postgres user.


Step 8: Install Fonts

You need to install the ttf-dejavu package.

sudo apt install ttf-dejavu

To display non-Latin characters, install the following packages.

sudo apt install fonts-noto-cjk fonts-noto-cjk-extra fonts-noto-hinted fonts-noto-unhinted ttf-unifont

Step 9: Configure renderd

Edit renderd config file.

sudo nano /etc/renderd.conf

In the [renderd] section, change the number of threads according to the number of CPU cores on your server.


In the [default] section, change the value of XML and HOST to the following. Note that lines beginning with semicolons (;) are comments.


Configure renderd

In [mapnik] section, change the value of plugins_dir to the following.


You can print the default input plugins directory with the following command.

mapnik-config --input-plugins

If you want to display non-Latin characters, it’s better to change the font settings to the following.


Save and close the file. Then edit the init script file

sudo nano /etc/init.d/renderd

Find the following line.


Change the user to osm. This is needed to load map data from PostgreSQL database.


Save the file. Set osm as the owner of /var/lib/mod_tile/ directory, which will hold the rendered tile files.

sudo chown osm /var/lib/mod_tile/ -R

Then restart renderd service.

sudo systemctl daemon-reload

sudo systemctl restart renderd

You need to check the log of renderd.

sudo journalctl -eu renderd

Make sure renderd does not produce any error in the log after the restart, or the map won’t be displayed.

Step 10: Configure Apache

Edit the OSM virtual host file.

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

Change the ServerName to your own domain name like You also need to create DNS A record for this sub-domain.


Save and close the file. Restart Apache.

sudo systemctl restart apache2

Then in your web browser address bar, type

You should see the tile of the world map. Congrats! You just successfully built your own OSM tile server.


If you have enabled the UFW firewall, be sure to open port 80 and 443 with the following command.

sudo ufw allow 80,443/tcp

If you see the 404 not found error, simply wait a few minutes, refresh the page in your browser and it should be able to load the tile of world map. If it still won’t load, then restart renderd service (sudo systemctl restart renderd).

Step 11: Display Your Tiled Web Map

Tiled web map is also known as slippy map in OpenStreetMap terminology. There are two free and open-source JavaScript map libraries you can use for your tile server: OpenLayer and Leaflet. The advantage of Leaflet is that it is simple to use and your map will be mobile-friendly.


To display your slippy map with OpenLayer, download JavaScript and CSS from and extract it to the webroot folder.

cd /var/www/

sudo wget

sudo unzip

Next, create the index.html file.

sudo nano /var/www/index.html

Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Accessible Map</title>
<link rel="stylesheet" href="" type="text/css">
<script src=""></script>
  a.skiplink {
    position: absolute;
    clip: rect(1px, 1px, 1px, 1px);
    padding: 0;
    border: 0;
    height: 1px;
    width: 1px;
    overflow: hidden;
  a.skiplink:focus {
    clip: auto;
    height: auto;
    width: auto;
    background-color: #fff;
    padding: 0.3em;
  #map:focus {
    outline: #4A74A8 solid 0.15em;
  <a class="skiplink" href="#map">Go to map</a>
  <div id="map" class="map" tabindex="0"></div>
  <button id="zoom-out">Zoom out</button>
  <button id="zoom-in">Zoom in</button>
    var map = new ol.Map({
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM({
             url: '{z}/{x}/{y}.png'
     target: 'map',
     controls: ol.control.defaults({
        attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
          collapsible: false
    view: new ol.View({
       center: [244780.24508882355, 7386452.183179816],

  document.getElementById('zoom-out').onclick = function() {
    var view = map.getView();
    var zoom = view.getZoom();
    view.setZoom(zoom - 1);

  document.getElementById('zoom-in').onclick = function() {
     var view = map.getView();
     var zoom = view.getZoom();
     view.setZoom(zoom + 1);

Save and close the file. Now you can view your slippy map by typing your sub-domain in the browser address bar.



To display your slippy map with Leftlet, download JavaScript and CSS from and extract it to the webroot folder.

cd /var/www/

sudo wget

sudo unzip

Next, create the index.html file.

sudo nano /var/www/index.html

Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.

<meta charset="UTF-8">
<title>My first osm</title>
<link rel="stylesheet" type="text/css" href="leaflet.css"/>
<script type="text/javascript" src="leaflet.js"></script>

  <div id="map"></div>
    var map ='map').setView([55,0.8],6);

Save and close the file. Now you can view your slippy map by typing your server IP address in browser.


openstreetmap tile server setup

Step 12: Pre-render Tiles

Rendering tiles on-the-fly will increase the map loading time in web browser. To pre-render tiles instead of rendering on the fly, use the following render_list command. Use -z and -Z flag specify the zoom level and replace the number of threads according to the number of CPU cores on your server. Render_list renders a list of map tiles by sending requests to the rendering daemon. Pre-rendered tiles will be cached in /var/lib/mod_tile directory.

render_list -m default -a -z 0 -Z 19 --num-threads=10

If later you updated the map data, you can pre-render all tiles again by using the --force option.

render_list -m default -a -z 0 -Z 19 --num-threads=10 --force

To render map tiles in the background, add the & symbol at the end.

render_list -m default -a -z 0 -Z 19 --num-threads=10 &

Now you can close the terminal window. To check the rendering progress, open another SSH session, and run the following command.

sudo journalctl -eu renderd

The above command will show the latest log of the renderd service. The following lines show that my OSM server is now rendering map tiles at zoom level 12.

 renderd[20838]: DEBUG: START TILE default 12 1008-1015 4056-4063, new metatile
 renderd[20838]: Rendering projected coordinates 12 1008 4056 -> -10175297.205328|-19724422.274944 -10097025.688364|-19646150.757980 to a 8 x 8 tile
 renderd[20838]: DEBUG: DONE TILE default 12 1008-1015 3984-3991 in 0.799 seconds
 renderd[20838]: DEBUG: Sending render cmd(3 default 12/1008/3984) with protocol version 2 to fd 18
 renderd[20838]: DEBUG: Got incoming request with protocol version 2
 renderd[20838]: DEBUG: Got command RenderBulk fd(18) xml(default), z(12), x(1008), y(4064), mime(image/png), options()
 renderd[20838]: DEBUG: START TILE default 12 1008-1015 4064-4071, new metatile
 renderd[20838]: Rendering projected coordinates 12 1008 4064 -> -10175297.205328|-19802693.791908 -10097025.688364|-19724422.274944 to a 8 x 8 tile

Step 13: Enable HTTPS

To encrypt HTTP traffic, we can obtain and install a free TLS certificate from Let’s Encrypt. The osmadmins/ppa PPA contains a malfunctioning certbot binary, so I recommend installing certbot from the Snap store.

sudo apt install snapd

sudo snap install --classic certbot

Then run the following command to obtain and install TLS certificate.

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

Once the certificate is installed, refresh the web page and you will see a lock in the address bar.


If you see a yellow triangle in Firefox address bar, that means the tile URLs are still using HTTP. You need to edit the index.html file and replace all HTTP protocol with HTTPS with the following command.

sudo sed -i 's/http/https/g' /var/www/index.html

Step 14: Enable HTTP2

To further improve map loading performance, you can enable HTTP2 protocol. First, you need to enable the HTTP2 module.

sudo a2enmod http2

Then open the SSL virtual host file.

sudo nano /etc/apache2/sites-enabled/tileserver_site-le-ssl.conf

Put the following directive after the opening <VirtualHost *:443> tag.

Protocols h2 http/1.1

Save and close the file. Then restart Apache for the changes to take effect.

sudo systemctl restart apache2

Restrict Access to Your OSM Tile Server with HTTP Referrer Header

By default, anyone can use OpenLayer or Leaflet to create a slippy map with the URL of your tile server. To restrict access to your tile server, edit the Apache virtual host file.

sudo nano /etc/apache2/sites-enabled/tileserver_site-le-ssl.conf

Add the following lines in the <VirtualHost> tags.

    <Location /osm>
        SetEnvIf Referer example\.com trusted_referer
        Order deny,allow
        Deny from all
        Allow from env=trusted_referer

The above code checks if the HTTP referer header includes your own domain. If not, access to the /osm directory will be denied. The backslash is used to escape the dot character. To add multiple hostnames as trusted referrers, use the following syntax.

SetEnvIf Referer (example\.com|www\.example\.com|map\.example\.com) trusted_referer

Save and close the file. Then test the syntax.

sudo apache2ctl -t

If the syntax is Ok, reload Apache for the changes to take effect.

sudo systemctl reload apache2

Auto-Renew TLS Certificate

You can create Cron job to automatically renew TLS certificate. Simply open root user’s crontab file.

sudo crontab -e

Add the following line at the bottom of the file.

@daily /snap/bin/certbot renew --quiet && systemctl reload apache2

PostgreSQL Database and Web Server on Different Hosts

If your PostgreSQL and Apache web server reside on different hosts, then you need to edit the project.mml file on the Apache host.

nano /home/osm/openstreetmap-carto-4.20.0/project.mml

Find the following lines:

osm2pgsql: &osm2pgsql
  type: "postgis"
  dbname: "gis"
  key_field: ""
  geometry_field: "way"
  extent: "-20037508,-20037508,20037508,20037508"

Specify the IP address of PostgreSQL database server.

osm2pgsql: &osm2pgsql
  type: "postgis"
  host: ""
  dbname: "gis"
  key_field: ""
  geometry_field: "way"
  extent: "-20037508,-20037508,20037508,20037508"

Save and close the file. Then build the Mapnik XML stylesheet with the carto map stylesheet compiler.

carto project.mml > style.xml

On the PostgreSQL database server, edit the main configuration file.

sudo nano /etc/postgresql/12/main/postgresql.conf

Add the following line to set PostgreSQL to listen on all interfaces.

listen_addresses = '*'

Save and close the file. Then edit the PostgreSQL client authentication configuration file.

sudo nano /etc/postgresql/12/main/pg_hba.conf

Add the following line at the end of the file to allow the osm user to login from the Apache host. Replace with the IP address of Apache host.

host   gis   osm   trust

Save and close the file. Then restart PostgreSQL.

sudo systemctl restart postgresql

Restart the render daemon on the Apache host.

sudo systemctl restart renderd

You need to check the log of renderd. Make sure renderd does not produce any error in the log, or the map won’t be displayed.

sudo journalctl -eu renderd

You should also restrict access to port 5432 of the PostgreSQL database server. For example, you can use the following UFW command to allow the IP address of Apache host only.

sudo ufw allow in from to any port 5432

Next Step

I hope this tutorial helped you set up OpenStreetMap tile server on Ubuntu 20.04. You may also want to set up Nominatim geocoding server to provide address lookup functionality.

You can provide navigation functionality to your map with the Open Source Routing Machine (OSRM).

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: 6 Average: 5]

95 Responses to “How to Set Up OpenStreetMap Tile Server on Ubuntu 20.04

  • Yury Tkachuk
    1 year ago

    At step 7 of your instruction, I get an error (text below):

    [email protected]:~# sudo -u postgres -i
    [email protected]:~$ cd /home/osm/openstreetmap-carto/
    [email protected]:/home/osm/openstreetmap-carto$ scripts/
    Traceback (most recent call last):
      File "scripts/", line 258, in 
      File "scripts/", line 160, in main
        os.makedirs(data_dir, exist_ok=True)
      File "/usr/lib/python3.8/", line 223, in makedirs
        mkdir(name, mode)
    PermissionError: [Errno 13] Permission denied: 'data'
    [email protected]:/home/osm/openstreetmap-carto$ scripts/
    Traceback (most recent call last):
      File "scripts/", line 258, in 
      File "scripts/", line 160, in main
        os.makedirs(data_dir, exist_ok=True)
      File "/usr/lib/python3.8/", line 223, in makedirs
        mkdir(name, mode)
    PermissionError: [Errno 13] Permission denied: 'data'
    [email protected]:/home/osm/openstreetmap-carto$
    • Grant permissions to the postgres user, as described in step 5.

      sudo setfacl -R -m u:postgres:rwx /home/osm/
      • Yury Tkachuk
        1 year ago

        Thank! I figured out the problem. You need to set your password for the user “postgres”, as well as for the user “root”.
        sudo passwd postgres
        I think this should be reflected in the instructions.

    • I didn’t set a password for postgres. And it works.

      • Yury Tkachuk
        1 year ago

        I did a clean installation according to your instructions, in step 5 everything went fine. I did not have to give permissions to the / home / osm / folder
        But in step 7, this problem arose, described above.
        When I began to give permissions to the user “postgres” to read the folder / home / osm / the system began to request the password for the user “postgres”
        After setting the password for the user “postgres”, the script worked correctly.
        Perhaps this is a special case, but it is better that it be reflected in the instructions

    • Because if you run the setfacl command in step 5, you are not logged in as the postgres user. So no password for postgres is needed.

      Some folks might need to use node cache in step 5, which will create files under /home/osm/. That’s why I put the setfacl command in step 5.

  • Yury Tkachuk
    1 year ago

    I also ask you to write an addition to the instructions, or give comments on how to update .osm.pbf correctly to download relevant information
    Or a script to automate this process

    • Kaushik Ray
      1 year ago

      I second this, it will be great if you could please write something for updating the Map server periodically.

  • Geoffrey
    1 year ago

    I want to restrict map access with an api key. I have run into this article

    Could you kindly comment on exactly how we can secure access for openlayers and leaflet map access with an API key.


  • Hi, can the map data be in other format instead of PBF ?

    • According to man osm2pgsql, only .osm and .pbf files are supported. PBF is actually a compressed form of a .osm file, if I remember it correctly.

    • There are some tools you can use to convert other formats to OSM. For example, sosi2osm can convert SOSI file to OSM file.

  • Kaushik
    1 year ago


    This is an awesome tutorial. Thank you for doing this. I wish i found this a few weeks ago.
    I have a question
    How do I use a server to serve these OSM data so its vector tiles. If you can make a tutorial on that.

    Thank you.

  • Marshall
    1 year ago

    Thanks for the great information and tutorial. I really enjoy your website.

    After going through this tutorial thoroughly and creating / tweaking my own tile server, I would like to mention that the performance of the tile rendering can be greatly improved by adding the indexes provided within the openstreetmap-carto project. (From the file)

    sudo -u postgres psql -d gis -f /home/osm/openstreetmap-carto/indexes.sql

    I did that, and I added 10 of my own additional indexes by monitoring the slower queries.

    • Kaushik
      1 year ago

      Hello Marshall, I noticed the rendering is very slow. I am running it for almost 2 days now and only till zoom level 14. Where did you get the other index files if you could please share. Also wanted to ask what zoom level are you getting ? for me it seems like after I get to zoom level 18 its not working any more.


      • Marshall
        1 year ago

        Hello Kaushik,

        I only pre-rendered to zoom level 12. That took my server a little less than 7 hours. Then for zoom levels 13 to 18 I calculated which tiles had an entry in the planet_osm_point database table, and only cached those. The world is 70% ocean, so a full render seemed a little excessive for those lower level zooms. I ended up using a fork of openstreetmap-carto called openstreetmap-carto-de because they had English names for foreign cities and countries. In openstreetmap-carto-de they use views of the tables, so the additional indexes I created don’t exactly translate to openstreetmap-carto.

        • Kaushik
          1 year ago

          OK I see. I thought of rendering till 16 but it seems like its going to take up a lot of space and time so I will stop once the zoom 14 is rendered. Also next time I will openstreetmap-carto-de its a good thing to have all names in English.

          ALso as i asked are you getting any zoom level above 18 like 19-22. it seems like the zoom level stops at 18 but as i understand it should go till 22. I am very new in this area. I would like to be able to zoom atleast till zoom 20.

        • Marshall
          1 year ago

          You just need to add MAXZOOM=20 to the [default] section of /etc/renderd.conf

          However, I set my max zoom to 22 and received this message :
          Renderd currently only supports up to zoom level 20

          I’m not sure if there is much benefit in going past zoom 18. There’s a reason that is the unlisted default option within renderd.

        • Kaushik
          1 year ago

          thank you. MAXZOOM=20 did the trick

  • I got error when running
    sudo add-apt-repository ppa:osmadmins/ppa

    Cannot add PPA: ‘ppa:~osmadmins…’
    ERROR: ‘~osmadmins’ user or team does not exist

    after checking the we there might be 2 reasons:
    1. clock not sync, run the following to fix this
    sudo apt-get install ntp
    sudo apt-get install ntpdate
    sudo ntpdate

    2. Firewall blocking access
    solution “sudo ufw disable”

  • davidknibb
    1 year ago

    All my stuff is setup with Debian10 – Buster. Do you think that there is anything from your Ubuntu setup that would fail/be a problem on buster ??

  • Kaushik
    1 year ago

    Do any one think its worth sharing all the rendered maps till zoom 16 for North America with a torrent?

  • Prince
    1 year ago

    Thank you for the nice tutorial. I have successfully setup the server. However want to be able to update the map. I have a number of shapefiles containing street names. Please can anyone point me in the right direction on how I can update my server with the data. Also if there is no way out on that part, can I manually update the street names with iD or Potlatch. How can I setup iD or Potlatch editor for my OSM server or any other editor that will work for me. Thank you

  • Hello,
    if I try to import another * .pbf into the database it works, but nothing is shown on the map !?

    How can I add additional countries later?

  • Stan92
    1 year ago

    Hi Marshall and everybody,

    I plan to install by following this tutorial an osm server with the full planet..
    Could you please tell me if it will be ok with this following VPS config
    64 GB DDR4 RAM ECC) – 10 dedicated cores -2 TB SSD
    With this config, which lower level I will be able to use?

    I have two additional questions…

    – Can I set up a standalone Postgres server and 2 OSM Tiles servers that share the same PG server or it doesn’t make sense?
    – What would be the process for updating the tiles as well as the database?

    And @Xiao, thanks for this great tutorial..

  • Stan92
    1 year ago


    I m following the tutorial, for this part :
    Configure Apache ->

    I have to edit my /etc/hostname and /etc/hosts files and set my

  • Anawaz
    1 year ago

    I added one country. I want to append nearby country. How to do?

  • centos
    1 year ago

    so how compatible would be this to install on Centos 7? what needs to be changed/done differently in Centos env?

    • I’ve tried to install OSM on CentOS 8. There are a lot of packages missing from the software repository. You have to compile them from source, which often doesn’t go well. Finally, I quit.

      Never tried with CentOS 7. I think it’s more or less the same.

  • Miles B. Dyson
    1 year ago

    love the tutorial .. thank you for your hard work!

  • hello
    i just need to ask one thing
    i implemented the exact same procedure on virtual machine first issue was that i was unable to get the maps on i solved it by replacing it with the localhost ip
    now the issue is every thing is working fine but i cant see the layer names
    Layer name : Country, city names etc can you please guide me why am i facing this issue.

    • If you didn’t import the planet map, there won’t be country names for every country.

      • yeah you are right thats what i did. I imported the specific country specific region for example Al ula
        but i am even unable to see any result for that specific region too.
        like i cant see the area names etc so is this a necessity to get the whole planet map because the documentation you provided said that if we want a specific region we can simple import that specific area map.


    • Check the log to see if renderd is working properly.

      sudo journalctl -eu renderd

      You imported a city map. You can only see the map for that city.

      • Hello Xiao,
        Hope you are doing well,
        the output of the following command is
        sudo journalctl -eu renderd is

  • Hello.
    I have problems with dependencies of libraries when installing the libmapnik-dev. It depends of libpq5 (= 12.4-0ubuntu0.20.04.1) but says: “12.4-1.pgdg20.04+1 is going to be installed. Problems couldnt be solved. You have broken packages”.
    Thank you Xiao for this tutorial.

  • touseef
    1 year ago

    hi Xiao,
    i tried it many times but the issue for me is i want a specific region i tried the documentation exactly but nothing worked map is showing but i cant see any sort of labels like area name etc
    Please guide me if i want to implement specific region map what steps should be ignored i really need your help.

  • Leandro
    1 year ago

    Hi! thanks for the great tutorial. I have a problem when running the command “systemctl status renderd”, the service is running, but also i get the following message:
    An error occurred while loading the map layer ‘default’: Could not load map file: File does not exist of ‘/etc/mapnik-osm-data/osm.xml’
    Any ideas? thanks in advance

  • Hi! Thanks for the great tutorial.
    I went through the whole tutorial and used the same server you recommended.
    When i go to tile.{mydomain}/osm/0/0/0.png i received 404 I checked the logs later and it took
    35min to generate this one tile. Now i can see that 1 Tile but when i want to create or slippy map or anything every tile takes like
    40min until its available…
    When i run “SELECT pg_size_pretty( pg_database_size(‘gis’) );” it returns

    postgres=# SELECT pg_size_pretty( pg_database_size(‘gis’) );
    259 GB
    (1 row)

    259 GB is too small or am i wrong :/

    Can i check somehow that the indexes are correct in postgres ?
    Thank you very much in advance!

    Dominik 🙂

    • If you imported a large map data, you can add the indexes provided by openstreetmap-carto.

      sudo -u postgres psql -d gis -f /home/osm/openstreetmap-carto/indexes.sql
      • Thanks for your quick reply. I run the script and it created the INDEXES.
        But it still takes a looot of time to run the following query for example:
        “select name, way from planet_osm_polygon where osm_id = ‘199929368’;”

        Do you have some kind of comparative values for this ?
        Since its the same server, can you run the slippymap without prerendered tiles ?
        Also the server memory is always Maxed at 50G/58G and Swap 2G/2G

        Sorry to bothering you with my problems 😀

        • Dominik
          12 months ago

          btw i imported the whole world

        • Dominik
          12 months ago

          This is the output of explain:

          gis=# EXPLAIN ANALYZE SELECT name, way FROM planet_osm_polygon where osm_id = 199929368;
          QUERY PLAN
          Gather (cost=1000.00..19086835.10 rows=2451331 width=64) (actual time=90470.091..480000.294 rows=1 loops=1)
          Workers Planned: 2
          Workers Launched: 2
          -> Parallel Seq Scan on planet_osm_polygon (cost=0.00..18840702.00 rows=1021388 width=64) (actual time=350023.244..479859.028 rows=0 loops=3)
          Filter: (osm_id = 199929368)
          Rows Removed by Filter: 163422076
          Planning Time: 2.895 ms
          Functions: 12
          Options: Inlining true, Optimization true, Expressions true, Deforming true
          Timing: Generation 14.291 ms, Inlining 305.530 ms, Optimization 783.851 ms, Emission 492.779 ms, Total 1596.451 ms
          Execution Time: 480007.619 ms
          (12 rows)

          Its not using the index :/

  • Any idea on how to solve this issue? “too many shared memory segments”:
    Thank you for a very good tutorial. But unable to pre-render zoom level 7 where I get this kind of error for many tiles (but not all). Here is what render list show for tiles not renderd:

    "rendering failed with command 4, pausing."

    And here is what the error in the renderd log show initially (full error message at end of message):

    "Error: failed to render TILE default 7 80-87 64-71
    reason: Postgis Plugin: ERROR: too many dynamic shared memory segments to executeQuery Full sql was: ...."

    Using render list on other zoom levels works though.

    Full error message from renderd log here:

    ERROR: too many dynamic shared memory segments to executeQuery Full sql was:  'SELECT ST_AsBinary("way") AS geom,"feature","way_pixels" FROM (SELECT
                                            way, way_pixels,
                                            COALESCE(wetland, landuse, "natural") AS feature
                                          FROM (SELECT
                                              ('landuse_' || (CASE WHEN landuse IN ('forest', 'farmland', 'residential', 'commercial', 'retail', 'industrial',
                                                                                    'meadow', 'grass', 'village_green', 'vineyard', 'orchard') THEN landuse END)) AS landuse,
                                              ('natural_' || (CASE WHEN "natural" IN ('wood', 'sand', 'scree', 'shingle', 'bare_rock', 'heath', 'grassland', 'scrub') THEN "natural" END)) AS "natural",
                                              ('wetland_' || (CASE WHEN "natural" IN ('wetland', 'mud') THEN (CASE WHEN "natural" IN ('mud') THEN "natural" ELSE tags->'wetland' END) END)) AS wetland,
                                              way_area/NULLIF(POW(4.36783e+06*0.001*0.28,2),0) AS way_pixels,
                                            FROM planet_osm_polygon
                                            WHERE (landuse IN ('forest', 'farmland', 'residential', 'commercial', 'retail', 'industrial', 'meadow', 'grass', 'village_green', 'vineyard', 'orchard')
                                              OR "natural" IN ('wood', 'wetland', 'mud', 'sand', 'scree', 'shingle', 'bare_rock', 'heath', 'grassland', 'scrub'))
                                              AND way_area > 0.01*1222.99::real*1222.99::real
                                              AND building IS NULL
                                          ) AS features
                                          ORDER BY way_area DESC, feature
                                        ) AS landcover_low_zoom WHERE "way" && ST_SetSRID('BOX3D(4852834.051771873 -2661231.576778126,7670608.662478126 156543.033928127)'::box3d, 3857)'
    • Found a solution that worked: edited the postgresql conf file and changed max_connections from 100 to 200. Then restarted postgresql and renderd.
      Also forgot to tell that the problem I encountered above – only happened when rendering meta tiles for zoom 7. Now renderd render meta tiles without any errors.
      It would be interesting to know what a good value for max_connections would be, and how this setting affect rendering performance.

  • if you not find

    then exist link :

    for file: external-data.yml

    • yves.33.54
      11 months ago

      Hi, ,Xiao
      Great job.

      After launching the scripts/, I got the error :

      requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url:

      But this file is available with a wget command at the same address.

  • Hello!
    This is an great Thread!

    My Question is, has somebody this Machine as an Shareable VM-Machine? I want use it offline.


  • I built the map of my country. Now I want to add satellite map and night mode map layers to my map server. Can anyone please guide me?

  • Hello!
    In Step 7 i submit
    carto project.mml > style.xml

    i get the following errors (Warnings).

  • I’m also receiving these warnings. If ignored, the rest of the setup gets completed successfully and map is displayed, but still would be nice to know the reason for the warnings during stylesheet build.

  • George Stoychev
    10 months ago

    Hi! Thanks for the great tutorial. I followed the steps and imported the map for Europe only, but in the end, the map looks empty. Do you have any idea what could be the issue?

  • Hi. Thanks for the tutorial!!!

    How can I UPDATE the pbf osm Data ?

  • How many days my pre-rendered tiles will be available if I don’t update my osm data at all (disconnected totally from internet)? Can I render all zoom levels once and get a whole map at any zoom level very fast always?

    • tooneamelt
      3 months ago

      I documented my steps and this is the process that worked for me:

      Updating OSM
      Install pyosmium
      Installing pyosmium will give you the utilities to get the changes from your current database from the latest openstreetmap data and the utility to get the change file from your current database and the latest openstreetmap data.
      sudo pip3 install osmium
      pyosmium-get-changes and pyosmium-up-to–date are located in /usr/local/bin

      Run pyosmium
      Run pyosmium-get-changes
      This will generate a sequence file
      cd /home/osm/
      pyosmium-get-changes -O -f sequence.state -v

      This will create a sequence.state file in the /home/osm directory to tell pyosmium where to start to look for changes
      Next, we’ll create a change file using the sequence file created above.
      pyosmium-get-changes –size=5000 -f sequence.state -o changes.osm.gz
      Run osm2pgsql
      Launch screen session
      sudo -u postgres -i
      Next, you’ll run osm2pgsql. It is important to run the osm2pgsql update with the same arguments you ran the install with.
      i.e. if you ran:
      osm2pgsql –slim -d gis –flat-nodes /home/osm/nodes.cache –hstore –multi-geometry –number-processes 56 –tag-transform-script /home/osm/openstreetmap-carto/openstreetmap-carto.lua –style /home/osm/openstreetmap-carto/ -C 200000 /srv/downloads/planet-latest.osm.pbf

      You would run:
      osm2pgsql –append –slim -d gis –flat-nodes /home/osm/nodes.cache –hstore –multi-geometry –number-processes 56 –tag-transform-script /home/osm/openstreetmap-carto/openstreetmap-carto.lua –style /home/osm/openstreetmap-carto/ -C 200000 changes.osm.gz

      Notice all arguments are exactly the same except you add –append and change the file used to import

      If you’re really out of date, you’ll need to run through this process a few times to get up to date. If you have to run it more than 4 or so times, you’re best to re-import and start with a fresh database

      • tooneamelt
        3 months ago

        Also, I should note, too update, you can’t use the –drop option during the import. You need the skin tables or nodes.cache file

  • Hey everyone!
    First of all thank you for this awesome tutorial.
    I have some trouble with Step 11. I am trying to add and activate Leaflet. That was partly successful. The Leaflet map frame and the zoom-buttons are displayed but most important – the map itself isn’t diplayed.

    Has anyone an idea, whats the reason for the behaviour? I already studied the index.html and read though the Leaflet quick tutorial but couldn’t find a solution.

    Best regards

  • Stéphane
    8 months ago

    Very Nice guide !
    For the Slippy Map, there’s already a slippymap.html in /var/www/osm

  • Stéphane
    8 months ago

    Ho and also, you forgot to mention information on optimizing the setup for rendering (your setup is good for importing, but it lacks a few tweaks for rendering efficiently) :

    – use custom indexes (psql -d gis -f indexes.sql as posgres user)
    – tweak postgresql settings (some old info but stil useful at )

  • Stéphane
    8 months ago

    Sorry I didn’t realize a lot of people mentioned it already 🙂

  • Hi! Really great tutorial! I need to make that server to work offline with permalink in leaflet/openlayers, but I don’t know how to modify that html. Any solutions?

  • when I run carto project.mml> style.xml I get the following error Warning: style / admin.mss: 18: 6 Styles do not match layer selector

  • Hello. At step 7, when executing “scripts/” the following error appears

    INFO:root:Checking table simplified_water_polygons
    Traceback (most recent call last):
      File "scripts/", line 108, in index
        cur.execute('''VACUUM ANALYZE "{temp_schema}"."{name}";'''
    psycopg2.errors.ActiveSqlTransaction: VACUUM cannot run inside a transaction block
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "scripts/", line 294, in 
      File "scripts/", line 284, in main
      File "scripts/", line 111, in index
        self._conn.autocommit = old_autocommit
    psycopg2.ProgrammingError: set_session cannot be used inside a transaction
    • Yeah, same here

    • Exactly the same error. Server hosted @ Contabo, same spec.
      Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-77-generic x86_64)

    • tooneamelt
      3 months ago

      Not sure if this is the correct way to go about this but this is an error with psycopg2. I did:
      pip3 uninstall psycopg2
      Then installed an older version of psycopg2. I chose 2.8.2
      pip3 install psycopg2==2.8.2

      I’m not sure which version actually broke this but 2.8.2 works for me

      • Thank you so much. You’r really helped me. It’s worked.

      • tooneamelt
        3 months ago

        Just ran a check and the latest psycopg2 package that works is psycopg2==2.8.5

  • tooneamelt
    3 months ago

    I have the server working but would like to change the folder name. I renamed the folder /var/www/osm to /var/www/tile and I changed the url in /var/www/index.html but the server fails to load the tiles. When I switch everything back to /osm instead of /tile it works.

    Is there another place I need to change the url?

    • tooneamelt
      3 months ago

      Nevermind, it was the uri variable in /etc/rendered.conf that also needed to be changed.

  • tooneamelt
    3 months ago

    To improve renderd performance if your tiles are slow to render, from the carto documentation, you should run indexs.sql in the /home/osm/openstreetmap-carto directory

    sudo -u postgres -i
    cd /home/osm/openstreetmap-carto
    psql -d gis -f indexs.sql
    2 months ago

    Finally i successfully completed Open Street Map tile server….!!!
    Thank you……!

  • Hi, please help me on this

    i followed steps given on my ubuntu 20.04 server 16GB RAM laptop, but stuck with

    cd /home/osm/openstreetmap-carto/


    Traceback (most recent call last):
    File “/usr/lib/python3/dist-packages/urllib3/”, line 159, in _new_conn
    conn = connection.create_connection(
    File “/usr/lib/python3/dist-packages/urllib3/util/”, line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
    File “/usr/lib/python3.8/”, line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno -2] Name or service not known

    During handling of the above exception, another exception occurred:

    also tried to copy zip file into local and placed them into www/osm folder but still its not able to proceed

    any help is appreciated

  • Christopher Lee
    3 days ago

    Hi Xiao…I’m running Ubuntu 20.04 on a Contabo VPS XL SSD (60GB RAM, 1.6TB SSD), and I modified my swap file to 120GB.
    Everything seems ok up to step 6, except I’m not getting the prompt indicated in your instructions, below, and the “sites-available” directory is not there.

    “During the installation, it will install Apache web server and ask if you want to enable mod_tile in the Apache config. Select Yes and press Enter. This will create an Apache config file for mod_tile (/etc/apache2/sites-available/tileserver_site.conf).”

    Any ideas?

  • I followed this tutorial and it is amazing.
    I am stuck with this error when running renderd
    As you can see there is no database user mentioned.
    Attached are the files and error images

    please advise

    ** (process:87423): ERROR **: 07:59:17.352: An error occurred while loading the map layer 'default': Postgis Plugin: FATAL:  role "root" does not exist
    Connection string: ' dbname=gis connect_timeout=4'
      encountered during parsing of layer 'landcover-low-zoom' in Layer at line 819 of '/home/osm/openstreetmap-carto/style.xml'

    My renderd.conf file is

    • You are trying to access PostgreSQL as root, that’s why it doesn’t work.

      Renderd should run as the osm

      • devteam
        9 hours ago

        I have already done those steps but still getting the same issue

    • The renderd package has made some changes since this post is published. Now you need to do the following:
      Create a new directory for the renderd service.

      sudo mkdir /etc/systemd/system/renderd.service.d/

      Create a custom config file under this directory.

      sudo nano /etc/systemd/system/renderd.service.d/custom.conf

      Add the following lines in this file.


      Save and close the file. Then reload systemd and restart rendered.

      sudo systemctl daemon-reload
      sudo systemctl restart rendered
      • devteam
        9 hours ago

        Thanks Tried and now getting bind socket error.

    • Run the following command to change the ownership of /run/renderd/ directory.

      sudo chown osm /run/renderd/ -R
      • devteam
        7 hours ago

        It is now showing lots of warning.
        but this not working
        While doing step 6 , it never created /etc/apache2/sites-available/tileserver_site.conf
        Hence I am confused , what to edit.
        Can you please share the complete file /etc/apache2/sites-available/tileserver_site.conf


  • devteam
    6 hours ago

    Can you please share the complete file tileserver_site-le-ssl.conf

  • After pre rendering getting below error:
    please advise
    map renderd[95463]: ** (process:95463): ERROR **: 12:52:17.031: Failed to send render cmd

    • jimmy jimmy
      5 hours ago

      Complete errors
      Oct 12 12:52:17 map renderd[xxxx]: ** (process:xxxx): ERROR **: 12:52:17.033: send error: Inappropriate ioctl for device

  • Can you please advise how to pass map param value in query string using leaflet code .
    Something like this
    We can change lat long value in map and get the map.
    How can we achieve the same in leaflet code in step 11

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