5

I am trying to install Cartopy on Ubuntu and need to install proj v8.0.0 binaries for Cartopy. However when I try to apt-get install proj-bin I can only get proj v6.3.1. How do I install the latest (or at least v8.0.0) proj for cartopy?

Recessive
  • 1,780
  • 2
  • 14
  • 37

3 Answers3

7

I'm answering my own question here partly to help others with this problem, and partly as an archive for myself so I know how to fix this issue if I come across it again. I spent quite a while trying to figure it out, and wrote detailed instructions, so see below:

Installing cartopy is a huge pain, and I've found using conda to be a very bad idea (it has bricked itself and python along with it multiple times for me)

THIS INSTALLATION IS FOR LINUX.

Step 0. Update apt:

apt update

Step 1. Install GEOS:

Run the following command to install GEOS:

apt-get install libgeos-dev

In case that doesn't do it, install all files with this:

apt-get install libgeos-dev libgeos++-dev libgeos-3.8.0 libgeos-c1v5 libgeos-doc

Step 2. Install proj dependencies:

  • Install cmake:
apt install cmake
  • Install sqlite3:
apt install sqlite3
  • Install curl devlopment package:
apt install curl && apt-get install libcurl4-openssl-dev

Step 3. Install Proj

Trying apt-get just in case it works:

Unfortunately, cartopy requires proj v8.0.0 as a minimum, but if you install proj using apt you can only install proj v6.3.1

Just for reference in case anything changes, this is the command to install proj from apt:

apt-get install proj-bin

I'm fairly sure this is all you need, but in case it's not, this command will install the remaining proj files:

apt-get install proj-bin libproj-dev proj-data

To remove the above installation, run:

apt-get remove proj-bin

or:

apt-get remove proj-bin libproj-dev proj-data

Building Proj from source

So if the above commands don't work (it's not working as of 2022/4/8), then follow the below instructions to install proj from source:

  • Go to your install folder and download proj-9.0.0 (or any version with proj-x.x.x.tar.gz):
wget https://download.osgeo.org/proj/proj-9.0.0.tar.gz 
  • Extract the tar.gz file:
tar -xf proj-9.0.0.tar.gz
  • cd into the folder:
cd proj-9.0.0
  • Make a build folder and cd into it:
mkdir build && cd build
  • Run (this may take a while):
cmake ..
cmake --build .
cmake --build . --target install
  • Run to make sure everything installed correctly:
ctest

The test command failed on one test for me (19 - nkg), but otherwise was fine.

You should find the required files in the ./bin directory

Finally:

  • Move binaries to the /bin directory:
cp ./bin/* /bin
  • As per Justino, you may also need to move the libraries:
cp ./lib/* /lib

Now after all this, you can finally install cartopy with pip:

pip install cartopy

After doing this, my cartopy still wasn't working. I went home to work on this next week, came back, and all of a sudden it was working so maybe try restarting

Recessive
  • 1,780
  • 2
  • 14
  • 37
  • Your solution worked perfectly for me. The issue I had was installing geoviews with pip (which asked for Proj 8.0.0 and GEOS) in the python:3.9 docker image. – KostasVlachos Aug 13 '22 at 13:51
0

The libraries should be copied manually sudo cp ./lib/* /lib This works for me

0

Below is a Multistage Dockerfile I put together for working with atmospheric data and machine learning.

# syntax=docker/dockerfile:1

# Description:
# Multistage Dockerfile for working with the SEVIR dataset and other atmospheric
# data formats for analysis, processing, and Machine Learning.

# Example:
# - docker build -t $USER/sevir . 
# - docker run -it -p 8888:8888 --gpus all --volume $PATH_TO_SEVIR:/home/vscode $USER/sevir

# TODD:
# install ffmpeg for video processing needed for matplotlib animations
# sudo apt install ffmpeg    

FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04 AS base
USER root

WORKDIR /
SHELL ["/bin/bash","-c"]
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y --no-install-recommends \
    python3.10 \
    python3.10-venv \
    libgeos3.10.2 \
    libgdal30 \
    && rm -rf /var/lib/apt/lists/* \
    && python3.10 -m venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH"
USER 1001

# =====================================================================================================================
# Compiler Stage; this will be omitted from the final image
# =====================================================================================================================
FROM base AS compiler
USER root

WORKDIR /
SHELL ["/bin/bash","-c"]
ENV DEBIAN_FRONTEND=noninteractive
# hadolint ignore=DL3008
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends \
    wget \
    gcc   \
    g++    \
    cmake   \
    gfortran \
    python3.10-dev \
    build-essential \
    # see: https://github.com/OSGeo/PROJ/blob/master/Dockerfile
    zlib1g-dev libsqlite3-dev sqlite3 libcurl4-gnutls-dev libtiff5-dev libsqlite3-0 libtiff5 \
    libgdal-dev libatlas-base-dev libhdf5-serial-dev \
    && rm -rf /var/lib/apt/lists/*

USER 1001

# =====================================================================================================================
# EcCodes is a library for decoding and encoding grib files.
# =====================================================================================================================
FROM compiler AS eccodes
USER root
ARG ECCODES="eccodes-2.24.2-Source" 
ARG ECCODES_DIR="/usr/include/eccodes"

WORKDIR /tmp
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN wget -c --progress=dot:giga \
    https://confluence.ecmwf.int/download/attachments/45757960/${ECCODES}.tar.gz  -O - | tar -xz -C . --strip-component=1 

WORKDIR /tmp/build
SHELL ["/bin/bash","-c"]
RUN cmake -DCMAKE_INSTALL_PREFIX="${ECCODES_DIR}" -DENABLE_PNG=ON .. \
    && make -j"$(nproc)" \
    && make install

USER 1001

# =====================================================================================================================
# PROJ is a library for coordinate transformations, and is a requirement for cartopy.
# =====================================================================================================================
FROM compiler AS proj
USER root

WORKDIR /proj
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN wget -c --progress=dot:giga \
    https://github.com/OSGeo/PROJ/archive/refs/tags/9.0.1.tar.gz  -O - | tar -xz -C . --strip-component=1 

WORKDIR /proj/build
SHELL ["/bin/bash","-c"]
RUN cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF \
    && make -j"$(nproc)" \
    && make install

USER 1001

# =====================================================================================================================
# Cartopy is a python library for plotting data on maps.
# =====================================================================================================================
FROM proj AS cartopy
USER root

SHELL ["/bin/bash","-c"]
ENV DEBIAN_FRONTEND=noninteractive
# hadolint ignore=DL3008
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends \
    python3.10-dev \
    && rm -rf /var/lib/apt/lists/*
# hadolint ignore=DL3013
RUN python3.10 -m pip install --upgrade pip --no-cache-dir && python3.10 -m pip install --no-cache-dir \
    Cartopy==0.21.1 \
    matplotlib==3.7.2

USER 1001

# =====================================================================================================================
# Final Image
# =====================================================================================================================
FROM base AS lunch-box
USER root
ARG USERNAME=vscode 
ARG USER_UID=1000
ARG USER_GID=$USER_UID

WORKDIR /tmp/sevir
SHELL ["/bin/bash","-c"]
# hadolint ignore=DL3008
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
    && chown -R $USER_UID:$USER_GID /opt/venv

ENV ECCODES_DIR=/usr/include/eccodes
COPY --from=eccodes --chown=$USER_UID:$USER_GID $ECCODES_DIR $ECCODES_DIR
COPY --from=cartopy --chown=$USER_UID:$USER_GID /opt/venv /opt/venv
# - install requirements that arn't in the requirements.txt and torch for caching
RUN python3.10 -m pip install --no-cache-dir \
    torch==2.0.1 \
    cfgrib==0.9.10.4 \
    notebook==6.5.4 \
    eccodes==1.6.0 
# - install the package and dependencies
COPY src/ src/
COPY setup.py setup.py
COPY pyproject.toml pyproject.toml
RUN python3.10 -m pip install . --no-cache-dir && rm -rf /tmp/*

USER $USERNAME
ARG HOME=/home/$USERNAME
WORKDIR $HOME
COPY --chown=$USER_UID:$USER_GID notebooks/ examples/
VOLUME $HOME/sevir-volume
ENV PATH_TO_SEVIR=$HOME/sevir-volume/sevir

CMD [ "jupyter", "notebook", "--ip=0.0.0.0", "--NotebookApp.token=''", "--NotebookApp.password=''", "--no-browser" ]
Jason Leaver
  • 286
  • 2
  • 11