Codespaces

Megumi Oshima & Nicholas Ducharme-Barth

January 2025

What is Codespaces?

  • Development environment that is hosted in the cloud on GitHub.com
  • You control the specs of the machine!
  • Connected to your repository

Why use it?

  • Containerized environment
  • Anyone using it is working in the same environment
    • Same OS
    • Same software
    • Same package versions

“It works on my computer!”

Set Up

To set up a Codespace,

  1. Create a .devcontainer folder inside your repository
  2. Add a devcontainer.json file to that folder
  3. Set up devcontainer.json how you like (packages, software, extensions, etc.)
  4. Add a setup.r file if needed
  5. On main page of repository, “Code” -> “Create codespace on main”

Devcontainer File

devcontainer.json

{
    "name": "Quarto Codespaces",
    "image": "ghcr.io/mcanouil/quarto-codespaces:latest", //pre-built image that includes quarto 
    "remoteUser": "vscode",
    "features": { //options that you add in as needed
        "ghcr.io/rocker-org/devcontainer-features/r-apt:latest": {},
        "ghcr.io/rocker-org/devcontainer-features/apt-packages:1": {
            "packages": "libglpk-dev,libxml2-dev" // needed for igraph
        },
        "ghcr.io/rocker-org/devcontainer-features/quarto-cli:1": {
            "version": "1.6.37", //specific version of quarto
            "installTinyTex": "false", // TinyTex is already installed
            "installChromium": "false" // needed for screen capture of htmlwidgets
        },
        "ghcr.io/rocker-org/devcontainer-features/r-rig:1": {
            "version": "4.4.2", // specific version of R
            "vscodeRSupport": "none",
            "installDevTools": "false",
            "installREnv": "true", //install renv for later
            "installRMarkdown": "false"
        },
        "ghcr.io/rocker-org/devcontainer-features/r-packages:1": {
            "packages": "languageserver,jsonlite,rlang", //r packages, can install packages from GitHub as well
            "installSystemRequirements": true
        }
    },
    "customizations": {
        "vscode": { //setting up VSCode how we like it
            "extensions": [
                "quarto.quarto",
                "REditorSupport.r",
                "GitHub.codespaces",
                "mathematic.vscode-pdf",
                "ms-vscode.live-server",
                "mechatroner.rainbow-csv"
            ],
            "settings": {
                "r.rterm.option": [
                    "--no-save",
                    "--no-restore-data",
                    "--quiet"
                ],
                "r.useRenvLibPath": true //use renv files to build container
            }
        }
    },
    "forwardPorts": [3000],   
    // Use 'postCreateCommand' to run commands after the container is created.
    //run an R script to call renv and restore the packages needed into the container
    "postCreateCommand": "Rscript .devcontainer/setup.r"
}

Let’s build our codespaces!