Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete installation script for Ubuntu 20.04 #242

Open
danijar opened this issue Sep 18, 2022 · 9 comments
Open

Complete installation script for Ubuntu 20.04 #242

danijar opened this issue Sep 18, 2022 · 9 comments

Comments

@danijar
Copy link

danijar commented Sep 18, 2022

Not an issue, just thought I'd share a full installation script for the DMLab Python package because it took a while to put all the pieces together and might be helpful for others. Tested on Ubuntu 20.04, might also work on newer versions:

#!/bin/sh
set -eu

# Dependencies
apt-get update && apt-get install -y \
    build-essential curl freeglut3 gettext git libffi-dev libglu1-mesa \
    libglu1-mesa-dev libjpeg-dev liblua5.1-0-dev libosmesa6-dev \
    libsdl2-dev lua5.1 pkg-config python-setuptools python3-dev \
    software-properties-common unzip zip zlib1g-dev g++
pip3 install numpy

# Bazel
apt-get install -y apt-transport-https curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list
apt-get update && apt-get install -y bazel

# Build
git clone https://github.com/deepmind/lab.git
cd lab
echo 'build --cxxopt=-std=c++17' > .bazelrc
bazel build -c opt //python/pip_package:build_pip_package
./bazel-bin/python/pip_package/build_pip_package /tmp/dmlab_pkg
pip3 install --force-reinstall /tmp/dmlab_pkg/deepmind_lab-*.whl
cd ..
rm -rf lab

# Stimuli
mkdir dmlab_data
cd dmlab_data
pip3 install Pillow
curl https://bradylab.ucsd.edu/stimuli/ObjectsAll.zip -o ObjectsAll.zip
unzip ObjectsAll.zip
cd OBJECTSALL
python3 << EOM
import os
from PIL import Image
files = [f for f in os.listdir('.') if f.lower().endswith('jpg')]
for i, file in enumerate(sorted(files)):
  print(file)
  im = Image.open(file)
  im.save('../%04d.png' % (i+1))
EOM
cd ..
rm -rf __MACOSX OBJECTSALL ObjectsAll.zip

apt-get clean
@tkoeppe
Copy link
Collaborator

tkoeppe commented Sep 19, 2022

You can reuse the .precommit.bazelrc file instead of making your own (--bazelrc=...).

@danijar
Copy link
Author

danijar commented Sep 19, 2022

Good to know about --bazelrc=.... The .precommit.bazelrc specifies --cxxopt=-std=c++11 though, wouldn't that be an issue? Without any .bazelrc I got an error that the build requires at least C++ 14.

@tkoeppe
Copy link
Collaborator

tkoeppe commented Sep 19, 2022

Oh, I think that's a pending issue: Abseil has dropped support for C++11, so I think we need to do the same here.

@cirosantilli
Copy link

We need to add a Dockerfile to this project.

@elliottower
Copy link

We need to add a Dockerfile to this project.

Would love an official dockerfile but you can pretty easily adapt this installation script to work with docker:

# Install DM lab requirements
RUN apt-get -y update \
    && apt-get install --no-install-recommends -y \
    build-essential curl freeglut3 gettext git libffi-dev libglu1-mesa \
    libglu1-mesa-dev libjpeg-dev liblua5.1-0-dev libosmesa6-dev \
    libsdl2-dev lua5.1 pkg-config python-setuptools python3-dev \
    software-properties-common unzip zip zlib1g-dev g++

# Install Bazel
RUN apt-get install -y apt-transport-https curl gnupg  \
    && curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg  \
    && mv bazel.gpg /etc/apt/trusted.gpg.d/  \
    && echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list \
    && apt-get update && apt-get install -y bazel

# Build DM lab
RUN git clone https://github.com/deepmind/lab.git \
    && cd lab \
    && echo 'build --cxxopt=-std=c++17' > .bazelrc \
    && bazel build -c opt //python/pip_package:build_pip_package  \
    && ./bazel-bin/python/pip_package/build_pip_package /tmp/dmlab_pkg \
    && pip3 install --force-reinstall /tmp/dmlab_pkg/deepmind_lab-*.whl \
    && cd .. \
    && rm -rf lab

@danijar
Copy link
Author

danijar commented Aug 20, 2024

Updated instal script for Ubuntu 22 (it assumes to be inside a Python 3 venv):

https://gist.github.com/danijar/ca6ab917188d2e081a8253b3ca5c36d3

Usage in Dockerfile:

RUN wget -O - https://gist.github.com/danijar/ca6ab917188d2e081a8253b3ca5c36d3/raw/install-dmlab.sh | sh

Unfortunately, I think DMLab is not compatible with Numpy 2, so you'll also need numpy<2 in your requirements.txt.

@tkoeppe
Copy link
Collaborator

tkoeppe commented Aug 21, 2024

(What does it take to make DMLab compatible with Numpy 2?)

@danijar
Copy link
Author

danijar commented Aug 23, 2024

My understanding is that Numpy's C API has changed a bit, so if that's used inside DMLab it would have to be update to the new API. I'm guessing Numpy is only used for the interface to Python so it shouldn't be that much work?

@ptonso
Copy link

ptonso commented Jan 1, 2025

I encountered issues with deprecated Python 2 (PY2) support and .so files being installed in the wrong location.
Here is my solution for Ubuntu 20.04: it consists of two scripts—one for setup and another for building the virtual environment.
These scripts explicitly exclude PY2 support and ensure .so files are moved to the correct location. They should work completely standalone.

If others face similar problems, it may be worth raising this as a standalone issue.

#!/bin/bash
set -e  # Exit on any error

echo "=== DeepMind Lab Initial Setup Script ==="

# Step 1: Install dependencies
echo "Installing dependencies..."
sudo apt update
sudo apt install -y build-essential libosmesa6-dev libgl1-mesa-dev \
    libxi-dev libxcursor-dev libxinerama-dev libxrandr-dev libopenal-dev \
    mesa-utils python3-dev python3-venv git wget unzip zlib1g-dev g++ pip

# Step 2: Install Bazelisk
echo "Installing Bazelisk..."
wget https://github.com/bazelbuild/bazelisk/releases/download/v1.17.0/bazelisk-linux-amd64 -O bazelisk
chmod +x bazelisk
sudo mv bazelisk /usr/local/bin/bazel

# Verify Bazel installation
bazel --version

# Step 3: Clone DeepMind Lab
echo "Cloning DeepMind Lab repository..."
git clone https://github.com/google-deepmind/lab.git
cd lab

# Step 4: Configure bazelrc
echo "Configuring .bazelrc..."
echo "build --enable_workspace" > .bazelrc
echo "build --python_version=PY3" >> .bazelrc
echo "build --action_env=PYTHON_BIN_PATH=$(which python3)" >> .bazelrc

# Step 5: Modify BUILD file
echo "Modifying BUILD file..."
sed -i '/\[py_binary(/,/\]]/c\
py_binary(\
    name = "python_game_py3",\
    srcs = ["examples/game_main.py"],\
    data = ["//:deepmind_lab.so"],\
    main = "examples/game_main.py",\
    python_version = "PY3",\
    srcs_version = "PY3",\
    tags = ["manual"],\
    deps = ["@six_archive//:six"],\
)' BUILD

# Step 6: Replace content of python_system.bzl
echo "Replacing python_system.bzl content..."
cat > python_system.bzl << 'EOL'
# Copyright 2021 DeepMind Technologies Limited.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# ============================================================================

"""Generates a local repository that points at the system's Python installation."""

_BUILD_FILE = '''# Description:
#   Build rule for Python

exports_files(["defs.bzl"])


cc_library(
    name = "python_headers",
    hdrs = glob(["python3/**/*.h", "numpy3/**/*.h"]),
    includes = ["python3", "numpy3"],
    visibility = ["//visibility:public"],
)
'''

_GET_PYTHON_INCLUDE_DIR = """
import sys
from distutils.sysconfig import get_python_inc
from numpy import get_include
sys.stdout.write("{}\\n{}\\n".format(get_python_inc(), get_include()))
""".strip()

def _python_repo_impl(repository_ctx):
    """Creates external/<reponame>/BUILD, a python3 symlink, and other files."""

    repository_ctx.file("BUILD", _BUILD_FILE)

    result = repository_ctx.execute(["python3", "-c", _GET_PYTHON_INCLUDE_DIR])
    if result.return_code:
        fail("Failed to run local Python3 interpreter: %s" % result.stderr)
    pypath, nppath = result.stdout.splitlines()
    repository_ctx.symlink(pypath, "python3")
    repository_ctx.symlink(nppath, "numpy3")


python_repo = repository_rule(
    implementation = _python_repo_impl,
    configure = True,
    local = True,
    attrs = {"py_version": attr.string(default = "PY3", values = ["PY3"])},
)
EOL

echo "Initial setup complete! Now run build_venv.sh to create virtual environment and build the project."
#!/bin/bash
set -e  # Exit on any error

# Configuration variables
CREATE_TEST=${CREATE_TEST:-false}
VENVPATH=${VENVPATH:-"$HOME/project/venv"}


# Check if lab directory exists
if [ ! -d "lab" ]; then
    echo "Error: 'lab' directory not found. Please run setup_lab.sh first."
    exit 1
fi

cd lab

echo "=== DeepMind Lab Virtual Environment Setup ==="
echo "Using virtual environment path: $VENVPATH"

# Create and activate virtual environment
echo "Setting up Python virtual environment at $VENVPATH..."
python3 -m venv "$VENVPATH"
source "$VENVPATH/bin/activate"

# Install required packages
pip install --upgrade pip setuptools wheel
pip install numpy dm_env opencv-python

# Export Python path
export PYTHON_BIN_PATH=$(which python3)

# Build the project
echo "Building the project..."
bazel clean --expunge
bazel build -c opt //python/pip_package:build_pip_package

# Generate wheel file
echo "Generating wheel file..."
./bazel-bin/python/pip_package/build_pip_package /tmp/dmlab_pkg

# Install package
echo "Installing package..."
pip install --force-reinstall /tmp/dmlab_pkg/deepmind_lab-*.whl

# Move files to correct locations
echo "Moving files to correct locations..."
SITE_PACKAGES="$VENVPATH/lib/python3.10/site-packages/deepmind_lab"
mv "$SITE_PACKAGES/_main/"* "$SITE_PACKAGES/"

# Create test file if requested
if [ "$CREATE_TEST" = true ]; then
    echo "Creating test file..."
    cat > test_dmlab.py << 'EOL'
import deepmind_lab
import numpy as np
import cv2

# Create environment
env = deepmind_lab.Lab(
    level='seekavoid_arena_01',
    observations=['RGB_INTERLEAVED'],
    config={
        'width': '640',
        'height': '480',
        'fps': '60'
    },
    renderer='hardware'
)

# Reset the environment
env.reset()

try:
    while True:
        if not env.is_running():
            print("Episode ended, resetting...")
            env.reset()
            continue
            
        # Generate a random action
        action = np.zeros(7, dtype=np.intc)
        action[0] = np.random.randint(-10, 11)  # Look left/right
        action[1] = np.random.randint(-10, 11)  # Look up/down
        action[2] = np.random.randint(-1, 2)    # Strafe
        action[3] = np.random.randint(-1, 2)    # Forward/backward
        action[4] = np.random.randint(0, 2)     # Fire
        action[5] = np.random.randint(0, 2)     # Jump
        action[6] = np.random.randint(0, 2)     # Crouch
        
        # Step the environment
        reward = env.step(action, num_steps=4)
        
        if env.is_running():
            obs = env.observations()
            frame = obs['RGB_INTERLEAVED']
            cv2.imshow('DeepMind Lab', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
            
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    cv2.destroyAllWindows()
    env.close()
    print("Environment closed successfully")
EOL
fi

echo "Installation complete!"
if [ "$CREATE_TEST" = true ]; then
    echo "To test, activate the environment and run test_dmlab.py:"
    echo "source $VENVPATH/bin/activate"
    echo "python3 test_dmlab.py"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants