|
| 1 | +# WSL 2 Installation Guide for jupyter-matlab-proxy |
| 2 | + |
| 3 | + |
| 4 | +Windows® Subsystem for Linux® (WSL 2), available in Windows 10 and later, allows users to run Linux binaries in Windows. This repository shows how to install and start `jupyter-matlab-proxy` in WSL 2. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +**Table of Contents** |
| 9 | +- [Requirements](#requirements) |
| 10 | +- [Set up WSL 2](#set-up-wsl-2) |
| 11 | + - [Install WSL 2](#install-wsl-2) |
| 12 | + - [Set up Linux Distribution](#set-up-linux-distribution) |
| 13 | + - [Install System Dependencies](#install-system-dependencies) |
| 14 | +- [Set up MATLAB](#set-up-matlab) |
| 15 | + - [Install MATLAB](#install-matlab) |
| 16 | + - [Add MATLAB executable path to system PATH](#add-matlab-executable-path-to-system-path) |
| 17 | +- [Install jupyter-matlab-proxy](#install-jupyter-matlab-proxy) |
| 18 | +- [Run MATLAB Code in Jupyter](#run-matlab-code-in-jupyter) |
| 19 | +- [Configure a Python Virtual Environment](#configure-a-python-virtual-environment) |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +To use WSL 2, you need one of the following: |
| 24 | + |
| 25 | +- Windows 10 |
| 26 | + |
| 27 | + - For x64 systems: Version 1903 or later, with Build 18362.1049 or later. |
| 28 | + - For ARM64 systems: Version 2004 or later, with Build 19041 or later. |
| 29 | + |
| 30 | +- Windows 11. |
| 31 | + |
| 32 | +To check your Windows version, run the following command in `command prompt`: |
| 33 | + |
| 34 | +```bash |
| 35 | +winver |
| 36 | +``` |
| 37 | + |
| 38 | +## Set up WSL 2 |
| 39 | + |
| 40 | +### Install WSL 2 |
| 41 | + |
| 42 | +Set up WSL 2 on your Windows machine: |
| 43 | + |
| 44 | +1. From the Start menu, click **Turn Windows features on or off**. |
| 45 | + |
| 46 | + <p align="left"> |
| 47 | + <img width="800" src="img/turn-windows-feature-on-or-off.png"> |
| 48 | + </p> |
| 49 | + |
| 50 | +2. Enable the following options: |
| 51 | + |
| 52 | + - Virtual Machine Platform |
| 53 | + - Windows Subsystem for Linux |
| 54 | + |
| 55 | + Restart the system if prompted. |
| 56 | + |
| 57 | + This activates WSL and the system's virtualization capability, which is required to run WSL 2. |
| 58 | + |
| 59 | + <p align="left"> |
| 60 | + <img width="400" src="img/enable-required-options.png"> |
| 61 | + </p> |
| 62 | + |
| 63 | +3. Install the WSL 2 Linux Package. Open `command prompt` and run the following command: |
| 64 | + |
| 65 | + ```bash |
| 66 | + wsl.exe --update |
| 67 | + ``` |
| 68 | + |
| 69 | +For details, see [Install WSL](https://learn.microsoft.com/en-us/windows/wsl/install-manual)(Microsoft). |
| 70 | + |
| 71 | +### Set up Linux Distribution |
| 72 | + |
| 73 | + |
| 74 | +1. Install your Linux distribution, such as Debian or Ubuntu, from the Microsoft Store. MathWorks recommends [Ubuntu 22.04](https://www.microsoft.com/store/productId/9PN20MSR04DW?ocid=pdpshare) or run the following command: |
| 75 | + |
| 76 | + ```bash |
| 77 | + wsl.exe --install Ubuntu-22.04 |
| 78 | + ``` |
| 79 | + |
| 80 | +2. To start the Linux distribution, select it from the Start menu. Use the prompt to set a new username and password. |
| 81 | + |
| 82 | + <p align="left"> |
| 83 | + <img width="800" src="img/setting-username-password.png"> |
| 84 | + </p> |
| 85 | + |
| 86 | +3. Check that the Linux version is `2`. Open a new `command prompt` and run the following command: |
| 87 | + |
| 88 | + ```bash |
| 89 | + wsl -l -v |
| 90 | + ``` |
| 91 | + |
| 92 | + If necessary, set the Linux version to `2` using |
| 93 | + |
| 94 | + ```bash |
| 95 | + wsl --set-version <distro name> 2 |
| 96 | + ``` |
| 97 | + |
| 98 | + For example: |
| 99 | + |
| 100 | + ```bash |
| 101 | + wsl --set-version Ubuntu 2 |
| 102 | + ``` |
| 103 | + |
| 104 | + <p align="left"> |
| 105 | + <img width="800" src="img/changing-wsl-version.png"> |
| 106 | + </p> |
| 107 | + |
| 108 | + |
| 109 | +### Install System Dependencies |
| 110 | + |
| 111 | +Install the system packages required to install and run MATLAB®. |
| 112 | + |
| 113 | +1. Start the `Linux Distro (Ubuntu 22.04)` and update the package lists. |
| 114 | + |
| 115 | + ```bash |
| 116 | + sudo apt update |
| 117 | + ``` |
| 118 | + |
| 119 | +2. Install `xvfb`, used to display plots and figures. |
| 120 | + |
| 121 | + ```bash |
| 122 | + sudo apt install xvfb -y |
| 123 | + ``` |
| 124 | + |
| 125 | + For `xvfb` to work correctly, remount `/tmp/.X11-unix` with read-write permission: |
| 126 | + |
| 127 | + ```bash |
| 128 | + sudo mount -o remount,rw /tmp/.X11-unix |
| 129 | + # Adding it to the .bashrc file |
| 130 | + echo `sudo mount -o remount,rw /tmp/.X11-unix` >> ~/.bashrc |
| 131 | + ``` |
| 132 | + |
| 133 | +3. Install Python. |
| 134 | + |
| 135 | + ```bash |
| 136 | + sudo apt install python-is-python3 python3-pip wget unzip ca-certificates -y |
| 137 | + ``` |
| 138 | + |
| 139 | +4. Install [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md). |
| 140 | + |
| 141 | + ```bash |
| 142 | + wget https://www.mathworks.com/mpm/glnxa64/mpm |
| 143 | + chmod +x mpm |
| 144 | + ``` |
| 145 | + |
| 146 | +## Set up MATLAB |
| 147 | + |
| 148 | +### Install MATLAB |
| 149 | + |
| 150 | + |
| 151 | +1. Use MPM to install MATLAB. For example, to install MATLAB R2023a, navigate to the folder containing the `mpm` binary file and run this command: |
| 152 | + |
| 153 | + ```bash |
| 154 | + ./mpm install --release=R2023a --destination=/home/<username>/matlab --products MATLAB |
| 155 | + ``` |
| 156 | + |
| 157 | + <p align="left"> |
| 158 | + <img width="800" src="img/install-MATLAB.png"> |
| 159 | + </p> |
| 160 | + |
| 161 | + For more information on using MPM to install MATLAB, see [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md). |
| 162 | + |
| 163 | +2. Install the system dependencies that MATLAB requires. To see these dependencies, listed by MATLAB release & OS, refer to `base-dependencies.txt` in the corresponding [MATLAB Dependencies](https://github.com/mathworks-ref-arch/container-images/tree/main/matlab-deps) folder. |
| 164 | + |
| 165 | + For example, to install the dependencies for MATLAB R2023a on Ubuntu 22.04: |
| 166 | + |
| 167 | + 1. Navigate to `2023a` -> `Ubuntu22.04` -> `base-dependencies.txt`. |
| 168 | + 2. Download the base-dependencies file. |
| 169 | + |
| 170 | + ```bash |
| 171 | + wget https://raw.githubusercontent.com/mathworks-ref-arch/container-images/main/matlab-deps/r2023a/ubuntu22.04/base-dependencies.txt |
| 172 | + ``` |
| 173 | + |
| 174 | + 3. Install the dependencies. |
| 175 | + |
| 176 | + ```bash |
| 177 | + cat base-dependencies.txt | xargs sudo apt install -y |
| 178 | + ``` |
| 179 | + |
| 180 | +### Add MATLAB executable path to system PATH |
| 181 | + |
| 182 | + |
| 183 | +`jupyter-matlab-proxy` expects the `matlab` executable to be present on the system PATH and will throw an error if it is unable to find the executable. |
| 184 | + |
| 185 | +1. To make the `matlab` executable available on system PATH use the command: |
| 186 | + |
| 187 | + ```bash |
| 188 | + sudo ln -fs <MATLAB_ROOT>/bin/matlab /usr/bin/matlab |
| 189 | + ``` |
| 190 | + |
| 191 | + where `MATLAB_ROOT` points to the folder in which MATLAB was installed. For example, the default value of `MATLAB_ROOT` in WSL 2 is `/home/<username>/matlab`. |
| 192 | + |
| 193 | + Alternatively, you can update the system PATH to include the path to MATLAB binaries by executing the following command in your terminal. |
| 194 | + |
| 195 | + ```bash |
| 196 | + export PATH=$PATH:/home/<username>/matlab/bin |
| 197 | + ``` |
| 198 | + `/home/<username>/matlab/` is the path where the MATLAB is installed. |
| 199 | + |
| 200 | +2. Execute the following command to show the current MATLAB path. |
| 201 | + |
| 202 | + ```bash |
| 203 | + which matlab |
| 204 | + ``` |
| 205 | + |
| 206 | +<p align="left"> |
| 207 | + <img width="800" src="img/set-matlab-path.png"> |
| 208 | +</p> |
| 209 | + |
| 210 | +## Install matlab-proxy |
| 211 | + |
| 212 | +1. Install `jupyter-matlab-proxy`, run: |
| 213 | + |
| 214 | + ```bash |
| 215 | + python -m pip install jupyter-matlab-proxy |
| 216 | + ``` |
| 217 | + |
| 218 | + Note: in newer versions of Linux, such as Debian 12 or higher, you are unable to install python packages in `externally-managed-environment`. To use a virtual environment instead, follow the steps in [Configure a Python Virtual Environment](#configure-a-python-virtual-environment). |
| 219 | + |
| 220 | +2. The packages are installed in `/home/<username>/.local/bin`. Add this location to the system path. |
| 221 | + |
| 222 | + ```bash |
| 223 | + export PATH=$PATH:/home/<username>/.local/bin |
| 224 | + ``` |
| 225 | + |
| 226 | +## Run MATLAB Code in Jupyter |
| 227 | + |
| 228 | +1. Install Jupyter Notebook or JupyterLab: |
| 229 | + |
| 230 | + ```bash |
| 231 | + # For Jupyter Notebook |
| 232 | + python -m pip install notebook |
| 233 | +
|
| 234 | + # For JupyterLab 3 |
| 235 | + python -m pip install 'jupyterlab>=3.0.0,<4.0.0a0' |
| 236 | + ``` |
| 237 | + |
| 238 | + Note: The package lets you execute MATLAB code in both JupyterLab 3 and JupyterLab 4, but syntax highlighting and auto indentation are currently only supported in JupyterLab 3. To upgrade to JupyterLab 4, use: |
| 239 | + |
| 240 | + ```bash |
| 241 | + python -m pip install jupyterlab |
| 242 | + ``` |
| 243 | + |
| 244 | + |
| 245 | +2. Open your Jupyter environment by starting Jupyter Notebook or JupyterLab. |
| 246 | + |
| 247 | + ```bash |
| 248 | + # For Jupyter Notebook |
| 249 | + jupyter notebook |
| 250 | +
|
| 251 | + # For JupyterLab |
| 252 | + jupyter lab |
| 253 | + ``` |
| 254 | + |
| 255 | + <p align="left"> |
| 256 | + <img width="800" src="img/start-jupyter-lab.png"> |
| 257 | + </p> |
| 258 | + |
| 259 | +3. Open one of the links highlighted in your web browser. |
| 260 | + |
| 261 | + <p align="left"> |
| 262 | + <img width="800" src="img/jupyter-window.png"> |
| 263 | + </p> |
| 264 | + |
| 265 | +4. Now you can click `MATLAB Kernel` or `Open MATLAB`. |
| 266 | + |
| 267 | +5. Click `MATLAB Kernel` and run the `ver` command. |
| 268 | + |
| 269 | +6. You see a prompt to enter the license details. |
| 270 | + |
| 271 | + <p align="left"> |
| 272 | + <img width="800" src="img/enter-license-detail.png"> |
| 273 | + </p> |
| 274 | + |
| 275 | +7. MATLAB displays the output. |
| 276 | + |
| 277 | + <p align="left"> |
| 278 | + <img width="800" src="img/jupyter-final-output.png"> |
| 279 | + </p> |
| 280 | + |
| 281 | +Refer to [Installation](https://github.com/mathworks/jupyter-matlab-proxy#install) and [Get Started](https://github.com/mathworks/jupyter-matlab-proxy#get-started) for more information on installing and using `jupyter-matlab-proxy`. |
| 282 | + |
| 283 | +## Configure a Python Virtual Environment |
| 284 | + |
| 285 | +This guide shows how to install the `jupyter-matlab-proxy` package in a Python Virtual Environment. |
| 286 | + |
| 287 | +Two commonly used environments are: |
| 288 | +1. Python venv |
| 289 | +2. conda |
| 290 | + |
| 291 | +This guide uses conda as the environment manager. |
| 292 | + |
| 293 | +1. Install [miniconda](https://docs.conda.io/projects/miniconda/en/latest/). |
| 294 | + |
| 295 | +2. Create a new python environment called `r2023a` by executing the command: |
| 296 | + |
| 297 | + ```bash |
| 298 | + conda create -n r2023a python=3.11 -y |
| 299 | + ``` |
| 300 | + |
| 301 | +3. Activate the conda environment `r2023a` by executing the command: |
| 302 | + |
| 303 | + ```bash |
| 304 | + conda activate r2023a |
| 305 | + ``` |
| 306 | + |
| 307 | +4. To install `jupyter-matlab-proxy` execute: |
| 308 | + |
| 309 | + ```bash |
| 310 | + python -m pip install jupyter-matlab-proxy |
| 311 | + ``` |
| 312 | + |
| 313 | +5. Reactivate the conda environment. |
| 314 | + |
| 315 | + ```bash |
| 316 | + conda activate r2023a |
| 317 | + ``` |
| 318 | + |
| 319 | +6. Verify that `jupyter-matlab-proxy` is running from the conda virtual environment. |
| 320 | + |
| 321 | + ```bash |
| 322 | + which jupyter |
| 323 | + ``` |
| 324 | + |
| 325 | + ```bash |
| 326 | + /home/<username>/miniconda3/envs/r2023a/bin/jupyter |
| 327 | + ``` |
| 328 | + |
| 329 | + If you see the path listed as `/home/<username>/.local/bin/jupyter`, first uninstall the local version, then reinstall `jupyter-matlab-proxy` in the conda virtual environment. |
| 330 | + |
| 331 | +7. Follow the steps in [Run MATLAB code in Jupyter](#run-matlab-code-in-jupyter). |
| 332 | + |
| 333 | + |
| 334 | +--- |
| 335 | + |
| 336 | +Copyright 2024 The MathWorks, Inc. |
| 337 | + |
| 338 | +--- |
0 commit comments