Remote development using Visual Studio Code🔗

Visual Studio Code is a code editor developed by Microsoft available for Linux, macOS and Windows. The editor supports several useful features for code development, such as attaching and editing files on a remote system over SSH.

Running VSCode on a compute node via the portal🔗

The best way to use VSCode is to run it on compute nodes using the OnDemand portals. There are two example scripts that can be used for launching VSCode either using loaded modules, or inside a container, at /apps/portal/vscode/. They set various environmental variables and may load modules, which are then used during the launch of code-server. After customizing the scripts, you can create the directory ~/portal/vscode and place your script in there, so that you have e.g. ~/portal/vscode/vscode-tensorflow.sh. The two example scripts are reproduced in brief below:

Using modules:

# Control which module system version is used; module spider code-server
export CODESERVER_MODULE_VERSION=4.9.1

# Control which directory to store user data
# Defaults to ~/.local/share/code-server
export CODESERVER_USER_DATA_DIR=

# Control which directory to store extensions
# Defaults to ~/.local/share/code-server/extensions
export CODESERVER_EXTENSIONS_DIR=

# You can also load modules here
module load PyTorch
# Or and source your custom environment
#source your/custom/env/bin/activate

In a container:

# Choose an image on which you have codeserver installed.
# The .def file of the default image can be used as a guide to how to
# codeserver in a container of your choice.
export CODESERVER_CONTAINER=/apps/containers/codeserver/codeserver-PyTorch-2.0.0.sif

# You can optionally specify additional arguments to apptainer via e.g.
# export APPTAINER_ARGS="-B $TMPDIR:/scratch"
# This example would bind-mount $TMPDIR to a directory /scratch in the
# container, so that when /scratch is written to in the container,
# the resulting file ends up in $TMPDIR.

# Control which directory to store user data
# Defaults to ~/.local/share/code-server
export CODESERVER_USER_DATA_DIR=

# Control which directory to store extensions
# Defaults to ~/.local/share/code-server/extensions
export CODESERVER_EXTENSIONS_DIR=

After setting up your desired script and placing it in the ~/portal/vscode/ folder, it should appear in the drop-down menu when launching a VSCode session on the portal.

Running VSCode on the login node via SSH🔗

For certain light editing tasks, it can be acceptable to run VSCode on the login node, either interactively or through a remote session. Running VSCode through a remote session can require some special configuration depending on your environment. These instructions are for running a local VSCode session on your computer, and connecting to login nodes. Heavy or long-running use of VSCode on the login node is strongly discouraged and may lead to your program being terminated.

Finding your Python interpreter🔗

You will need to specify the Python interpreter path in VSCode; you can find out the correct path by typing which python into the terminal. You will see something like

[cid@vera1 ~]$ which python
/apps/Arch/software/Python/3.9.6-GCCcore-11.2.0/bin/python

Press F1, and search for interpreter - you should find the option Python: Select interpreter. Click it, and select Enter interpreter path. Copy and paste the output from which python, in our case /apps/Arch/software/Python/3.9.6-GCCcore-11.2.0/bin/python.

Connecting via SSH🔗

First, you need to install the extension Remote - SSH in VSCode. Press F1, and search for Remote SSH. Select Remote - SSH: Add New SSH Host. You should fill in the necessary SSH command e.g.

ssh WRITE_YOUR_CID_HERE@alvis1.c3se.chalmers.se

to connect to the alvis1 login node. You can also load an SSH configuration file via Remote-SSH: Open SSH Configuration File.... You should now be able to connect using Remote-SSH. However, you generally need to carry out further configuration in order to load modules and set up your environment or launch inside a container.

Using a RemoteCommand to set up a module environment🔗

Using a RemoteCommand allows you to connect to the login node via your local VSCode application, while landing in an environment that already has your modules loaded. You can set up your SSH configuration file for this as follows (assuming we want to connect to the alvis1 login node):

Host alvis1-module-load
    HostName alvis1.c3se.chalmers.se
    user WRITE_YOUR_CID_HERE
    RemoteCommand source load_modules.sh; bash -l
    RequestTTY yes

where load_modules.sh is a file containing the modules load commands that you want to run, e.g.

module load TensorFlow

Open your settings in VSCode, and search for SSH. Make sure Enable remote command is turned on.

Finally, connect to alvis1-module-load using Remote-SSH, and enter your credentials. Open a terminal and type module list to verify that your modules have been loaded.

[cid@alvis1 ~]$ module list

Currently Loaded Modules:
  1) GCCcore/11.2.0                              29) Tcl/8.6.11-GCCcore-11.2.0
  2) zlib/1.2.11-GCCcore-11.2.0                  30) SQLite/3.36-GCCcore-11.2.0
  ...

Using a RemoteCommand to launch inside a container🔗

Using a RemoteCommand also allows you to run inside a container. This does not require any special configuration of the container. You can set up your SSH configuration file for this as follows (assuming we want to connect to the alvis1 login node):

Host alvis1-apptainer-shell
    HostName alvis1.c3se.chalmers.se
    user WRITE_YOUR_CID_HERE
    RemoteCommand apptainer shell /PATH/TO/APPTAINER/IMAGE
    RequestTTY yes

where you specify the path to the apptainer image (for example, /apps/containers/PyTorch/PyTorch-2.0.0.sif).

Open your settings in VSCode, and search for SSH. Make sure Enable remote command is turned on.

Finally, connect to alvis1-apptainer-shell using Remote-SSH, and enter your credentials. You will see something like

Apptainer>

Enter the command python --version and see that it correctly prints the version:

Apptainer> python --version
Python 3.10.9

Normally, the VSCode software will find the correct Python path in a container. If you have a non-standard Python path, or you have different versions of Python, follow the instructions for setting the Python interpreter above. You may need to use /usr/bin/which rather than just which.

Troubleshooting🔗

Killing previous hosts🔗

You may need to kill any pre-existing session of VSCode after changing your configuration - you can do this by hitting F1 in VSCode and typing kill - this should bring up the option Kill VS Code server on host. Select it, select e.g. alvis1-module-load (or whatever else you named your host), and enter your credentials. Wait until you get a confirmation popup which tells you that the remote session has been killed.