1*da0073e9SAndroid Build Coastguard Worker# Step by step guide on using PyTorch's DevContainer 2*da0073e9SAndroid Build Coastguard Worker 3*da0073e9SAndroid Build Coastguard WorkerUsing PyTorch's DevContainer environment involves a series of steps that will help you set up a development environment that is isolated and replicable. Below, we'll guide you through each step to make this process as smooth as possible: 4*da0073e9SAndroid Build Coastguard Worker 5*da0073e9SAndroid Build Coastguard Worker## Step 1: Install VSCode 6*da0073e9SAndroid Build Coastguard Worker 7*da0073e9SAndroid Build Coastguard Worker1. Navigate to the [Visual Studio Code website](https://code.visualstudio.com/). 8*da0073e9SAndroid Build Coastguard Worker2. Download the appropriate installer for your operating system (Windows, Linux, or macOS). 9*da0073e9SAndroid Build Coastguard Worker3. Run the installer and follow the on-screen instructions to install VSCode on your system. 10*da0073e9SAndroid Build Coastguard Worker4. After installation, launch VSCode. 11*da0073e9SAndroid Build Coastguard Worker 12*da0073e9SAndroid Build Coastguard Worker## Step 2: Install DevContainer Extension 13*da0073e9SAndroid Build Coastguard Worker 14*da0073e9SAndroid Build Coastguard Worker1. In VSCode, go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window. 15*da0073e9SAndroid Build Coastguard Worker2. Search for "Dev Containers" in the Extensions view search bar. 16*da0073e9SAndroid Build Coastguard Worker3. Find the "Dev Containers" extension in the search results and click on the install button to install it. 17*da0073e9SAndroid Build Coastguard Worker 18*da0073e9SAndroid Build Coastguard WorkerYou can also go to the extension's [homepage](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) and [documentation page](https://code.visualstudio.com/docs/devcontainers/containers) to find more details. 19*da0073e9SAndroid Build Coastguard Worker 20*da0073e9SAndroid Build Coastguard Worker## Step 3: Install Docker and Add Current Login User to Docker Group 21*da0073e9SAndroid Build Coastguard Worker 22*da0073e9SAndroid Build Coastguard Worker1. Follow the [official guide](https://docs.docker.com/get-docker/) to install Docker. Don't forget the [post installation steps](https://docs.docker.com/engine/install/linux-postinstall/). 23*da0073e9SAndroid Build Coastguard Worker 24*da0073e9SAndroid Build Coastguard WorkerIf you are using [Visual Studio Code Remote - SSH](https://code.visualstudio.com/docs/remote/ssh), then you only need to install Docker in the remote host, not your local computer. And the following steps should be run in the remote host. 25*da0073e9SAndroid Build Coastguard Worker 26*da0073e9SAndroid Build Coastguard Worker## Step 4 (Optional): Install NVIDIA Container Toolkit for GPU Usage 27*da0073e9SAndroid Build Coastguard Worker 28*da0073e9SAndroid Build Coastguard Worker1. If you intend to use GPU resources, first ensure you have NVIDIA drivers installed on your system. Check if `nvidia-smi` works to verify your GPU setup. 29*da0073e9SAndroid Build Coastguard Worker2. Follow the [official guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#docker) to install the NVIDIA Container Toolkit. 30*da0073e9SAndroid Build Coastguard Worker3. After installation, verify that the toolkit is installed correctly by running: 31*da0073e9SAndroid Build Coastguard Worker ``` 32*da0073e9SAndroid Build Coastguard Worker docker run --rm --runtime=nvidia --gpus all nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi 33*da0073e9SAndroid Build Coastguard Worker ``` 34*da0073e9SAndroid Build Coastguard Worker 35*da0073e9SAndroid Build Coastguard Worker## Step 5: Clone PyTorch 36*da0073e9SAndroid Build Coastguard Worker 37*da0073e9SAndroid Build Coastguard Worker1. Open a terminal or command prompt. 38*da0073e9SAndroid Build Coastguard Worker2. Use the following command to clone the PyTorch repository: 39*da0073e9SAndroid Build Coastguard Worker ``` 40*da0073e9SAndroid Build Coastguard Worker git clone https://github.com/pytorch/pytorch 41*da0073e9SAndroid Build Coastguard Worker ``` 42*da0073e9SAndroid Build Coastguard Worker3. Navigate to the cloned directory: 43*da0073e9SAndroid Build Coastguard Worker ``` 44*da0073e9SAndroid Build Coastguard Worker cd pytorch 45*da0073e9SAndroid Build Coastguard Worker ``` 46*da0073e9SAndroid Build Coastguard Worker 47*da0073e9SAndroid Build Coastguard Worker## Step 6: Open in DevContainer 48*da0073e9SAndroid Build Coastguard Worker 49*da0073e9SAndroid Build Coastguard Worker1. In VSCode, use the Command Palette (`Ctrl+Shift+P` or `Cmd+Shift+P` on macOS) to run the "Dev Containers: Open Folder in Container..." command. 50*da0073e9SAndroid Build Coastguard Worker2. You will be prompted with two options: CPU dev container or CUDA dev container. Choose the one you want to run. 51*da0073e9SAndroid Build Coastguard Worker 52*da0073e9SAndroid Build Coastguard Worker## Step 7: Wait for Building the Environment 53*da0073e9SAndroid Build Coastguard Worker 54*da0073e9SAndroid Build Coastguard Worker1. After opening the folder in a DevContainer, VSCode will start building the container. This process can take some time as it involves downloading necessary images and setting up the environment. 55*da0073e9SAndroid Build Coastguard Worker2. You can monitor the progress in the VSCode terminal. 56*da0073e9SAndroid Build Coastguard Worker3. Once the build process completes, you'll have a fully configured PyTorch development environment in a container. 57*da0073e9SAndroid Build Coastguard Worker4. The next time you open the same dev container, it will be much faster, as it does not require building the image again. 58*da0073e9SAndroid Build Coastguard Worker 59*da0073e9SAndroid Build Coastguard WorkerYou are now all set to start developing with PyTorch in a DevContainer environment. This setup ensures you have a consistent and isolated development environment for your PyTorch projects. 60*da0073e9SAndroid Build Coastguard Worker 61*da0073e9SAndroid Build Coastguard Worker## Step 8: Build PyTorch 62*da0073e9SAndroid Build Coastguard Worker 63*da0073e9SAndroid Build Coastguard WorkerTo build pytorch from source, simply run: 64*da0073e9SAndroid Build Coastguard Worker ``` 65*da0073e9SAndroid Build Coastguard Worker python setup.py develop 66*da0073e9SAndroid Build Coastguard Worker ``` 67*da0073e9SAndroid Build Coastguard Worker 68*da0073e9SAndroid Build Coastguard WorkerThe process involves compiling thousands of files, and would take a long time. Fortunately, the compiled objects can be useful for your next build. When you modify some files, you only need to compile the changed files the next time. 69*da0073e9SAndroid Build Coastguard Worker 70*da0073e9SAndroid Build Coastguard WorkerNote that only contents in the `pytorch` directory are saved to disk. This directory is mounted to the docker image, while other contents in the docker image are all temporary, and will be lost if docker restarts the image or the server reboots. 71*da0073e9SAndroid Build Coastguard Worker 72*da0073e9SAndroid Build Coastguard WorkerFor an in-depth understanding of Dev Container and its caveats, please refer to [the full documentation](https://code.visualstudio.com/docs/devcontainers/containers). 73