docker run -dit \
--name myubuntu \
--hostname myubuntu \
-v ~/ubuntu:/root/ws \
ubuntu:24.04
环境说明:
- Mac(ARM64)
- Docker
- Ubuntu(ARM)
- VS Code Remote Containers
📦 1. Docker + VS Code
- 安装 Docker
- 使用 VS Code 连接 Docker 容器(Remote Containers)
🔧 2. 安装 R 语言环境
apt update
apt install -y software-properties-common dirmngr make build-essential cmake libxml2-dev libssl-dev libcurl4-openssl-dev libuv1-dev locales
查看 R 版本
apt list -a r-base
示例输出:
r-base/noble 4.3.3-2build2 all
安装 R
apt install r-base
验证 R 安装
R --version
运行 R 脚本
Rscript your_script.R
🧠 3. VS Code 中使用 R
安装 VS Code 扩展
- R Extension(VSCode 插件市场)
安装 R 依赖包
install.packages(c("jsonlite", "R6"))
安装调试器(vscDebugger)
方法 1(推荐)
install.packages("vscDebugger", repos = "https://manuelhentschel.r-universe.dev")
方法 2(GitHub)
remotes::install_github("ManuelHentschel/vscDebugger")
加载调试器
library(vscDebugger)
✅ 无报错说明成功
安装 Language Server(代码补全/提示)
install.packages('languageserver', repos='https://cloud.r-project.org/')
install.packages(c('tidyverse','data.table'), repos='https://cloud.r-project.org/')
📓 4. JupyterLab + R 内核
安装 JupyterLab
pip install jupyterlab
安装 IRkernel(R 内核)
install.packages("IRkernel")
IRkernel::installspec()
❗ 常见问题
1️⃣ 找不到 R 内核
IRkernel::installspec(user = FALSE)
2️⃣ 权限问题
IRkernel::installspec(user = TRUE)
3️⃣ 安装包报错(locale 问题)
locale-gen en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
🚀 5. 启动 JupyterLab
jupyter lab --allow-root
dockerfile
启动:
docker build -t r-dev-env .
内容:
# 基础镜像(Ubuntu ARM 兼容)
FROM ubuntu:24.04
# 避免交互
ENV DEBIAN_FRONTEND=noninteractive
# =========================
# 🌍 Locale(解决 R / Jupyter 报错)
# =========================
RUN apt update && apt install -y locales && locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
# =========================
# 🔧 基础工具
# =========================
RUN apt update && apt install -y \
software-properties-common \
dirmngr \
make \
cmake \
git \
curl \
vim
# =========================
# ⚙️ 编译依赖(R 需要)
# =========================
RUN apt install -y \
build-essential \
libxml2-dev \
libssl-dev \
libcurl4-openssl-dev \
libuv1-dev
# =========================
# 📊 安装 R
# =========================
RUN apt install -y r-base
# 验证 R
RUN R --version
# =========================
# 🐍 Python + Jupyter
# =========================
RUN apt install -y python3 python3-pip
RUN pip install jupyterlab
# =========================
# 📦 R 包(VSCode + 调试 + Jupyter)
# =========================
RUN R -e "install.packages(c('jsonlite','R6'), repos='https://cloud.r-project.org/')"
# VSCode 调试
RUN R -e "install.packages('vscDebugger', repos='https://manuelhentschel.r-universe.dev')"
# 自动补全
RUN R -e "install.packages('languageserver', repos='https://cloud.r-project.org/')"
RUN R -e "install.packages(c('tidyverse','data.table'), repos='https://cloud.r-project.org/')"
# Jupyter R 内核
RUN -e "install.packages('IRkernel', repos='https://cloud.r-project.org/')"
RUN R -e "IRkernel::installspec(user = FALSE)"
# =========================
# 📂 工作目录
# =========================
WORKDIR /home/ubuntu
# =========================
# 🌐 端口(Jupyter)
# =========================
# EXPOSE 8888
# =========================
# 🚀 关键:容器长期运行(像虚拟机)
# =========================
CMD ["sleep", "infinity"]