colouring-montreal/docs/setup-dev-environment.md

396 lines
12 KiB
Markdown
Raw Normal View History

# Setting up a local development environment
2022-02-24 10:23:05 -05:00
This document is intended to guide you through setting up a local development environment for the Colouring London application. This guide assumes you already have either already have access to an machine with Ubuntu 18.04 or 20.04 installed, or can use VirtualBox to set up an Ubuntu virtual machine as below.
2022-02-21 08:41:55 -05:00
2022-02-21 09:21:12 -05:00
<details>
<summary>
Configuring an Ubuntu VM in VirtualBox
2022-02-23 10:41:01 -05:00
</summary><p></p>
Here we explain how to use VirtualBox and SSH into your Ubuntu installation for convenience.
2022-02-21 09:21:12 -05:00
2022-02-24 05:38:57 -05:00
When setting up the VirtualBox VM, consider the size of the database you intend to load for use with the application. Consult the [:house: Loading the building data](#house-loading-the-building-data) section of this guide and decide whether you will be using a full city database or will load test data from OSM.
2022-02-21 09:38:09 -05:00
For "Colouring London", we have found that the size of the database means that a VM with access to 50GB of storage is appropriate. If you are using the OSM test data, the default storage settings in VirtualBox should suffice.
##### In either case, you should set the memory to at least `2048` MB.
2022-02-21 09:38:09 -05:00
2022-02-21 08:45:10 -05:00
If you a running Ubuntu in a virtual environment you will need to configure networking to forward ports from the guest to the host. For Virtual Box the following was configured under NAT port forwarding (found under `Settings -> Network -> Advanced -> Port Forwarding`).
2022-02-21 08:41:55 -05:00
Name | Protocol | Host Port | Guest Port
-------- | --------- | ---------- | -----------
app | TCP | 8080 | 3000
app_dev | TCP | 3001 | 3001
ssh | TCP | 4022 | 22
2022-02-23 07:56:08 -05:00
The `app_dev` mapping is used in development by Razzle which rebuilds and serves client side assets on the fly.
2022-02-21 08:55:38 -05:00
To run the commands in the rest of this setup guide, either `ssh` into the VirtualBox environment or open the terminal within the Ubuntu GUI.
2022-02-21 09:21:12 -05:00
If you wish to `ssh`, you will first need to open the terminal in Ubuntu and run the following.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 09:21:12 -05:00
sudo apt-get install -y openssh-server
```
You can then `ssh` into the VirtualBox VM set up with the port forwarding described above like so, where `<linuxusername>` is the name you set up during the installation of Ubuntu (you can type `whoami` in the Ubuntu terminal to remind yourself of this).
2022-02-21 08:55:38 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 09:14:02 -05:00
ssh <linuxusername>@localhost -p 4022
2022-02-21 08:55:38 -05:00
```
2022-02-21 09:21:12 -05:00
</details>
2022-02-23 10:41:01 -05:00
## Contents
2022-02-24 05:28:08 -05:00
- [:tulip: Installing the tools and components](#tulip-installing-the-tools-and-components)
- [:red_circle: Installing PostgreSQL](#red_circle-installing-postgresql)
- [:rainbow: Installing Colouring London](#rainbow-installing-colouring-london)
- [:arrow_down: Installing Node.js](#arrow_down-installing-nodejs)
- [:large_blue_circle: Configuring PostgreSQL](#large_blue_circle-configuring-postgresql)
- [:arrow_forward: Configuring Node.js](#arrow_forward-configuring-nodejs)
- [:house: Loading the building data](#house-loading-the-building-data)
- [:computer: Running the application](#computer-running-the-application)
2022-02-24 05:59:58 -05:00
- [:eyes: Viewing the application](#eyes-viewing-the-application)
2022-02-23 10:41:01 -05:00
2022-02-24 05:22:25 -05:00
## :tulip: Installing the tools and components
2022-02-21 08:55:38 -05:00
First upgrade the installed packages to the latest versions, to remove any security warnings.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 09:22:58 -05:00
sudo apt-get update -y
sudo apt-get upgrade -y
```
2022-02-24 05:59:58 -05:00
Now install some essential tools.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo apt-get install -y build-essential git vim-nox wget curl
```
2022-02-24 05:22:25 -05:00
### :red_circle: Installing PostgreSQL
2022-02-24 10:26:28 -05:00
Set the postgres repo for apt (these instructions were taken from [postgresql.org](https://www.postgresql.org/download/linux/ubuntu/)).
2022-02-03 05:51:04 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
```
2022-02-03 05:51:04 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
2022-02-24 05:59:58 -05:00
```
```bash
2022-02-21 10:11:21 -05:00
sudo apt-get update
```
2022-02-03 05:51:04 -05:00
Next install postgres and postgis to enable support for geographical objects.
2022-02-21 10:15:15 -05:00
```bash
2022-02-22 11:59:01 -05:00
sudo apt-get install -y postgresql-12 postgresql-contrib-12 libpq-dev postgis postgresql-12-postgis-3
2022-02-21 10:11:21 -05:00
```
and additional geo-spatial tools
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo apt-get install -y gdal-bin libspatialindex-dev libgeos-dev libproj-dev
```
2022-02-24 05:22:25 -05:00
### :rainbow: Installing Colouring London
2022-02-24 05:59:58 -05:00
Now clone the `colouring-london` codebase.
2022-02-21 10:15:15 -05:00
```bash
cd ~ && git clone https://github.com/colouring-london/colouring-london.git
2022-02-21 10:11:21 -05:00
```
2022-02-24 05:59:58 -05:00
**Note:** We assume here that you will clone the repo into the home directory of your Ubuntu installation. Watch out for later commands in this guide that assume the repo is located at `~/colouring-london` and modify the path if appropriate.
2022-02-24 05:22:25 -05:00
### :arrow_down: Installing Node.js
Now install Node. It is helpful to define some local variables.
2022-02-21 10:15:15 -05:00
```bash
2022-02-02 11:58:29 -05:00
export NODE_VERSION=v16.13.2
export DISTRO=linux-x64
wget -nc https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-$DISTRO.tar.xz
sudo mkdir /usr/local/lib/node
sudo tar xf node-$NODE_VERSION-$DISTRO.tar.xz -C /usr/local/lib/node
sudo mv /usr/local/lib/node/node-$NODE_VERSION-$DISTRO /usr/local/lib/node/node-$NODE_VERSION
rm node-$NODE_VERSION-$DISTRO.tar.xz
```
Now add the Node installation to the path and export this to your bash profile.
2022-02-21 10:15:15 -05:00
```bash
cat >> ~/.profile <<EOF
export NODEJS_HOME=/usr/local/lib/node/node-$NODE_VERSION/bin
export PATH=\$NODEJS_HOME:\$PATH
EOF
```
2022-02-24 10:23:05 -05:00
Then run source to make sure node and npm are on your path.
2022-02-21 10:15:15 -05:00
```bash
source ~/.profile
```
You can check the updated variables as follows
2022-02-21 10:15:15 -05:00
```bash
echo $PATH
echo $NODEJS_HOME
```
2022-02-24 05:22:25 -05:00
### :large_blue_circle: Configuring PostgreSQL
Now we configure postgres. First ensure postgres is running.
2022-02-21 10:15:15 -05:00
```bash
sudo service postgresql start
2022-02-21 10:11:21 -05:00
```
Ensure the `en_US` locale exists.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo locale-gen en_US.UTF-8
```
Configure the database to listen on network connection.
2022-02-21 10:15:15 -05:00
```bash
2022-02-22 11:59:01 -05:00
sudo sed -i "s/#\?listen_address.*/listen_addresses '*'/" /etc/postgresql/12/main/postgresql.conf
2022-02-21 10:11:21 -05:00
```
Allow authenticated connections from any IP (so includes the host).
2022-02-21 10:15:15 -05:00
```bash
2022-02-22 11:59:01 -05:00
echo "host all all all md5" | sudo tee --append /etc/postgresql/12/main/pg_hba.conf > /dev/null
2022-02-21 10:11:21 -05:00
```
Restart postgres to pick up config changes.
2022-02-21 10:15:15 -05:00
```bash
sudo service postgresql restart
2022-02-21 10:11:21 -05:00
```
Create a superuser role for this user (`<username>`) if it does not already exist. The
password `<pgpassword>` is arbitrary and probably should not be your Ubuntu login password.
2022-02-21 10:15:15 -05:00
```bash
2022-02-03 06:18:21 -05:00
sudo -u postgres psql -c "SELECT 1 FROM pg_user WHERE usename = '<username>';" | grep -q 1 || sudo -u postgres psql -c "CREATE ROLE <username> SUPERUSER LOGIN PASSWORD '<pgpassword>';"
```
2022-02-24 05:36:14 -05:00
<details>
<summary>Note for "Colouring London" devs</summary><p></p>
If you intend to load the full CL database from a dump file into your dev environment, run the above `psql` command with `<username>` as "cldbadmin" and use that username in subsequent steps, but also run the above a second time with `<username>` as "clwebapp" (see section [:house: Loading the building data](#house-loading-the-building-data) for more details).
</details><p></p>
2022-02-21 12:28:42 -05:00
2022-02-23 07:42:14 -05:00
Set environment variables, which will simplify running subsequent `psql` commands.
2022-02-17 06:10:00 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-17 06:10:00 -05:00
export PGPASSWORD=<pgpassword>
export PGUSER=<username>
export PGHOST=localhost
2022-02-23 07:42:14 -05:00
export PGDATABASE=<colouringlondondb>
2022-02-17 06:10:00 -05:00
```
Create a colouring london database if none exists. The name (`<colouringlondondb>`) is arbitrary.
2022-02-21 10:15:15 -05:00
```bash
sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname = '<colouringlondondb>';" | grep -q 1 || sudo -u postgres createdb -E UTF8 -T template0 --locale=en_US.utf8 -O <username> <colouringlondondb>
```
2022-02-21 10:15:15 -05:00
```bash
2022-03-01 05:04:13 -05:00
psql -d <colouringlondondb> -c "create extension postgis;"
psql -d <colouringlondondb> -c "create extension pgcrypto;"
psql -d <colouringlondondb> -c "create extension pg_trgm;"
```
2022-02-24 05:22:25 -05:00
### :arrow_forward: Configuring Node.js
2022-02-14 10:30:25 -05:00
2022-02-24 05:22:25 -05:00
Now upgrade the npm package manager to the most recent release with global privileges. This needs to be performed as root user, so it is necessary to export the node variables to the root user profile. Don't forget to exit from root at the end.
2022-02-14 10:30:25 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-14 10:30:25 -05:00
sudo su root
export NODEJS_HOME=/usr/local/lib/node/node-v16.13.2/bin/
export PATH=$NODEJS_HOME:$PATH
npm install -g npm@latest
exit
```
Now install the required Node packages. This needs to done from the `app` directory of your
local repository, so that it can read from the `package.json` file.
2022-02-21 10:15:15 -05:00
```bash
2022-02-24 05:59:58 -05:00
cd ~/colouring-london/app
2022-02-21 10:23:32 -05:00
npm install
2022-02-21 10:11:21 -05:00
```
2022-02-14 10:30:25 -05:00
2022-02-24 05:22:25 -05:00
## :house: Loading the building data
2022-02-17 06:16:37 -05:00
<details>
2022-02-24 05:23:53 -05:00
<summary> With a database dump </summary><p></p>
2022-02-17 06:28:19 -05:00
If you are a developer on the Colouring London project (or another Colouring Cities project), you may have a production database (or staging etc) that you wish to duplicate in your development environment.
Log into the environment where your production database is kept and create a dump file from the db.
2022-02-21 10:15:15 -05:00
```bash
2022-02-17 06:28:19 -05:00
pg_dump <colouringlondondb> > <dumpfile>
```
You should then download the file to the machine where you are setting up your development environment. If you are using Virtualbox, you could host share the dump file with the VM via a shared folder (e.g. [see these instructions for Mac](https://medium.com/macoclock/share-folder-between-macos-and-ubuntu-4ce84fb5c1ad)).
In your Ubuntu installation where you have been running these setup steps (e.g. Virtualbox VM), you can then recrate the db like so.
2022-02-21 10:15:15 -05:00
```bash
psql -d <colouringlondondb> < <dumpfile>
2022-02-17 06:28:19 -05:00
```
2022-02-24 05:59:58 -05:00
#### Run migrations
2022-02-23 07:42:14 -05:00
Now run all 'up' migrations to create tables, data types, indexes etc. The `.sql` scripts to
do this are located in the `migrations` folder of your local repository.
```bash
ls ~/colouring-london/migrations/*.up.sql 2>/dev/null | while read -r migration; do psql -d <colouringlondondb> < $migration; done;
```
2022-02-17 06:16:37 -05:00
</details>
<details>
2022-02-24 05:23:53 -05:00
<summary> With test data </summary><p></p>
2022-02-14 10:33:42 -05:00
2022-02-24 05:59:58 -05:00
This section shows how to load test buildings into the application from OpenStreetMaps (OSM).
2022-02-14 10:33:42 -05:00
2022-02-24 10:23:05 -05:00
#### Set up Python
Install python and related tools.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
sudo apt-get install -y python3 python3-pip python3-dev python3-venv
```
Now set up a virtual environment for python. In the following example we have named the
virtual environment *colouringlondon* but it can have any name.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
pyvenv colouringlondon
```
Activate the virtual environment so we can install python packages into it.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
source colouringlondon/bin/activate
```
Install python pip package manager and related tools.
2022-02-21 10:15:15 -05:00
```bash
pip install --upgrade pip
pip install --upgrade setuptools wheel
```
2022-02-24 10:23:05 -05:00
#### Load OpenStreetMap test polygons
2022-02-03 10:26:10 -05:00
2022-02-21 10:23:32 -05:00
First install prerequisites.
2022-02-21 10:15:15 -05:00
```bash
sudo apt-get install -y parallel
2022-02-03 10:26:10 -05:00
```
2022-02-21 10:23:32 -05:00
Install the required python packages. This relies on the `requirements.txt` file located
in the `etl` folder of your local repository.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:23:32 -05:00
cd ~/colouring-london/etl/
pip install -r requirements.txt
2022-02-03 10:26:10 -05:00
```
To help test the Colouring London application, `get_test_polygons.py` will attempt to save a small (1.5km²) extract from OpenStreetMap to a format suitable for loading to the database.
2022-02-21 10:41:52 -05:00
Download the test data.
2022-02-03 10:26:10 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-03 10:26:10 -05:00
python get_test_polygons.py
2022-02-21 10:29:04 -05:00
```
Note: the first time you run it, you will get these warnings:
```
rm: cannot remove 'test_buildings.geojson': No such file or directory
rm: cannot remove 'test_buildings.3857.csv': No such file or directory
```
2022-02-24 05:59:58 -05:00
#### Run migrations
2022-02-23 07:42:14 -05:00
Now run all 'up' migrations to create tables, data types, indexes etc. The `.sql` scripts to
do this are located in the `migrations` folder of your local repository.
```bash
ls ~/colouring-london/migrations/*.up.sql 2>/dev/null | while read -r migration; do psql -d <colouringlondondb> < $migration; done;
```
2022-02-24 05:59:58 -05:00
#### Load buildings
2022-02-21 10:41:52 -05:00
Load all building outlines.
2022-02-21 10:29:04 -05:00
```bash
2022-02-03 10:26:10 -05:00
./load_geometries.sh ./
2022-02-21 10:41:52 -05:00
```
Create a building record per outline.
```bash
2022-02-03 10:26:10 -05:00
./create_building_records.sh
2022-02-21 10:41:52 -05:00
```
2022-02-17 06:16:37 -05:00
</details>
2022-02-24 05:22:25 -05:00
## :computer: Running the application
2022-02-23 07:54:36 -05:00
Now we are ready to run the application.
2022-02-21 06:02:39 -05:00
First enter the app directory.
2022-02-03 10:26:10 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:36:46 -05:00
cd ~/colouring-london/app
2022-02-21 10:11:21 -05:00
```
2022-02-21 06:02:39 -05:00
Then create a folder for the tilecache.
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:11:21 -05:00
mkdir tilecache
```
2022-02-21 06:02:39 -05:00
2022-02-23 07:54:36 -05:00
Create some additional variables for running the application (the `APP_COOKIE_SECRET` is arbitrary).
```bash
export PGPORT=5432
export APP_COOKIE_SECRET=123456
export TILECACHE_PATH=~/colouring-london/app/tilecache
```
Finally, simply run the application with npm.
```bash
npm start
```
2022-02-24 05:59:58 -05:00
**Note:** You can also specify the variables for `npm start` like so:
2022-02-23 07:54:36 -05:00
<details>
<summary>
2022-02-24 05:59:58 -05:00
Specify variables
2022-02-23 07:54:36 -05:00
</summary>
2022-02-21 06:02:39 -05:00
2022-02-21 10:15:15 -05:00
```bash
2022-02-21 10:36:46 -05:00
PGPASSWORD=<pgpassword> PGDATABASE=<colouringlondondb> PGUSER=<username> PGHOST=localhost PGPORT=5432 APP_COOKIE_SECRET=123456 TILECACHE_PATH=~/colouring-london/app/tilecache npm start
2022-02-21 10:11:21 -05:00
```
2022-02-23 07:54:36 -05:00
</details><p></p>
2022-02-24 05:59:58 -05:00
### :eyes: Viewing the application
The site can then be viewed on http://localhost:8080 on the host computer.
Finally to quit the application type `Ctrl-C`.