Introduction

Visual Studio Code is a popular source code editor from Microsoft, with support for many languages, such as C/C++, Fortran and Python. One notable feature is the option to do remote development over SSH. In this setup, the VS code GUI runs locally (e.g. on your laptop), but connects to a remote server (e.g. running on Snellius). Tasks such as editing files and running processes then work transparently on the remote server, including access to a terminal from within VS Code.

Using VS Code remotely on Snellius can be done in three ways:

  • Use VS Code via Open OnDemand through a browser (Preferred option) 
  • Reserve(allocate) a dedicated or shared node. Use the "Remote - SSH extension" of VS Code in conjunction with SSH ProxyCommand/ProxyJump by leveraging the login nodes as steppingstone.
  • Run VS Code in a VNC remote desktop via a compute node

launching VS Code from the interactive login nodes is no longer permitted due to system resource optimization.

Using VS Code in the Browser 

Advantages:

  • No software installation required.
  • Resources can be selected and job is launched from the Open OnDemand menu.
  • The resources you requested are available to you on a non-shared basis.
  • You can perform remote development on the partition you like, including gpu partitions.
  • Development and debug work has little impact on other users' experience on the login node.

Disadvantages:

  • Allocation of a node depends on node availability and priority. If Snellius is full, then it may take a while before you get yourself a node.
  • Since you are allocating a compute node, this compute time will be charged from your budget.
  • VS code may be less responsive in the browser and some shortcuts may not work in the same way you are used to on your local machine. 

Please find more information in the Open OnDemand for Snellius documentation.

Running the VS Code Server on a compute node

Advantages:

  • The VS Code GUI can still be used locally as you are used to. The extra steps are allocating a node for yourself, making some changes in your ssh config and connecting to the remote server on the login node over SSH, but these steps are fairly simple to do.
  • A node's resources are available to you on a non-shared basis.
  • You can perform Multi-node tests with MPI interactively specifying hostnames within your allocation.
  • You can perform remote development on the partition that you want to run on eventually which also includes a gpu partition which means you can test your code on the GPU.
  • Biggest advantage of all: your development work has less influence on other jobs and users and login node does not get overloaded with your junk.

Disadvantages:

  • Allocation of a node depends on node availability and priority. If Snellius is full, then it may take a while before you get yourself a node.
  • Since you are allocating a compute node, this compute time will be charged from your budget.
  • The connection exists as long as the allocation exists.
  • Customizing the environment (e.g. loading modules) in which the VS Code Server runs can be tricky.


Detailed documentation from Microsoft on remote development with this method can be found here.

Install VS Code locally

  1. Download VS Code for your local system from https://code.visualstudio.com/#alt-downloads.
  2. Install VS Code using the downloaded installer.

Install the Remote SSH extension

  1. Open VS Code on your local system and go to the Extensions tab.

  2. In the search field type “remote ssh”. Choose the Remote - SSH extension and install it using the little blue Install button.


Reserve a node on Snellius and create/append a ssh config file

  1. Log into Snellius using a terminal and allocate a node for yourself using the salloc command, you can check the allocation using the squeue  command:
    $ salloc -N 1 --exclusive -p genoa -t 04:00:00
    salloc: Pending job allocation 6605837
    salloc: job 6605837 queued and waiting for resources
    salloc: job 6605837 has been allocated resources
    salloc: Granted job allocation 6605837
    salloc: Waiting for resource configuration
    salloc: Nodes tcn989 are ready for job
    [satishk@tcn989 ~]$ squeue
                 JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
               6605837     genoa interact  satishk  R       0:17      1 tcn989
    
    
    Here you see that tcn989  was allocated to me.

    Note: Please do NOT close this terminal session.

  2. On your local system, prepare a user specific ssh configuration. Mac or Linux users add  the following lines to the file under the default path ~/.ssh/config 
# SURF

Host snel_gen
 Hostname snellius.surf.nl
 User <snellius_username>
 Port 22

Host <allocated_node_name>
  HostName <allocated_node_name>
  User <snellius_username>
  ProxyCommand ssh -Y <snellius_username>@snel_gen -W %h:%p

In the above example I would replace the <snellius_username> and <allocated_node_name> with appropriate allocation such as in the example in the point above this one, the username is satishk  and the allocated node name is tcn989

Note: For Windows users, if you are using MobaXterm, please follow the link here.

Connect to the allocated node on Snellius


  1. Point the remote server within VS Code to the config file that you just created or added the above lines to:


  2. You should be able to see two more entries when you connect to the remote host using

    That is snel_gen and <allocated_node_name>  such as tcn989 or a gcn  node if you have allocated a node from the GPU partition. 

  3. Please choose the allocated_node_name  and NOT snel_gen  to connect to otherwise you will end up in the login node.

Terminal access

VS Code provides terminal access on the remote system from within the GUI. Use the Terminal > New Terminal menu option, or press Ctrl + Shift + ` (Windows/Linux) or ^ + Shift + ` (macOS). Note that the last character of the key combination is a backtick.

A new area will open within the GUI holding the terminal for the remote system where the VS Code Server is running:

Installing additional extensions

For improved editing of source code VS Code provides many extensions. For example the C/C++ Extension Pack. In some cases when loading a file of a new type for the first time VS Code will show a prompt to install the corresponding extension. But to manually install such an extension use the Extensions tab (or View > Extensions), then search for the extension by name and use the little blue Install button. For some extensions a reload of VS Code is needed, which can be done by pressing on the little blue Reload Required button.

VS Code extensions will be installed on the remote server. In general, ~/.vscode-server will hold all files related to the VS Code Server installation.

Below we list some useful settings for some of these extensions.

C/C++ Extension Pack

After opening a folder with C/C++ sources you can set up the correct compiler. Press F1 and type "c++ edit" and pick C/C++: Edit Configurations (UI).

Change the Compiler path to /hpc/eb/compilerwrappers/compilers/gcc.

Python Extensions

Installing Python Extensions

There are various python extensions that can be installed. First, let's open the extension menu. Press F1 and type 'install extensions', and click on it. 

You should now see the extensions pane on the left hand side

In the 'Search Extensions in Marketplace' box, type 'Python'. At the very least, you'll probably want the Python extension by Microsoft installed. There is also the Python Extension Pack, which bundles this Python extension and a few others. To install it, simply press the blue 'install' button.

If you remove 'Python' from the search bar, you'll see your currently installed extensions, and you will see that this one was installed on the remote machine:

Making sure VS Code uses the right software environment

There are two scenarios: either you are using packages from the environment modules installed on Snellius, or you are using a python (or conda) virtual environment.

In the first case, things are a bit difficult since unfortunately, VS Code (and the Remote-SSH extension) are not aware of environment modules. The only way to alter the environment is by having the Remote-SSH extension execute a so-called RemoteCommand  before it starts the server. This can be done in the SSH Config file. But first, we'll need to adjust two settings in the remote SSH configuration. To do so, connect with the machine using the 'Connect to remote machine' steps described earlier. Once connected, press F1 and type 'Open Remote Settings'. Note that this option is only  available if you are currently connected to the remote machine (i.e. if the green connection icon in the bottom left shows you are connected to the remote machine). Then, add the following three lines to that settings file:

    "remote.SSH.useLocalServer": true,
    "remote.SSH.enableRemoteCommand": true,
    "remote.SSH.remotePlatform": "",

Unless you have also altered other remote SSH settings, the file will probably look something like this now:

{
    "remote.autoForwardPortsSource": "output",
    "remote.SSH.useLocalServer": true,
    "remote.SSH.enableRemoteCommand": true,
    "remote.SSH.remotePlatform": "",
}

Now, we can define the RemoteCommand  in the SSH Configuration file of the local machine. To do this, type F1, then 'Open SSH Configuration File'. You should get a dropdown of the configuration files, one of which contains the system you added. You can then add the RemoteCommand , after which the configuration file should something like this.
In this is an example where we load the PyTorch/1.11.0-foss-2021a-CUDA-11.3.1  module (outdated, mind you).

Host snellius.surf.nl
  HostName snellius.surf.nl
  User <your_snellius_login>
  RemoteCommand source /etc/profile; module load 2021; module load PyTorch/1.11.0-foss-2021a-CUDA-11.3.1; bash

Now, close your connection to the remote machine (press F1 and type 'Close remote connection'). Then, reconnect. Now, the environment should  be altered. You can easily check this: in the VS code terminal, if you type module list , you should now see the PyTorch module as being loaded. More importantly, the VS Code python extension can now search the PYTHONPATH  that is set by this module for python modules. In our example with PyTorch, it will recognize an import torch  as a valid import, and subsequently can autocomplete when you type e.g. torch. .

If for some reason, you cannot get the environment correct using the RemoteCommand  option, the alternative is to modify your ~/.bashrc  on Snellius to include the module load  statements for the modules you'd like to have loaded. We generally strongly discourage loading modules in your .bashrc  as it changes the environment of any login you do to the system. 

For the second case, i.e. when you use virtual environments, things are simpler. You can simply select the Python interpreter from your virtual environment, and VS Code will find all Python modules installed in that environment. To select a Python interpreter, press F1, then type 'Select interpreter'. This should show a dropdown list of all python executables VS Code manages to find in 'standard' places: by default, it will look in paths like .conda  and .virtualenvs  in your homedir, and scan the PATH  for python interpreters. If you have a python virtual environment installed in a non-standard location, you'll have to make sure that the Python extension knows where to find your virtual environment. Click 'File' => 'Preferences' => 'Settings' and search for 'venv'. In the 'Python: Venv Path' setting, you can point to the paths where your virtual environments are installed. 

Running VS Code in a visualization VNC remote desktop

Advantages:

  • You can use the VS Code GUI almost in the same as on your local system. There's no need to use a VS Code Server.
  • You can fully utilize the computational power of a compute node for (interactively) testing your code.

Disadvantages:

  • You need to use the VNC remote desktop workflow. This isn't complex, but it adds a few steps in setting up.
  • You can't directly access files on your local system (e.g. laptop) from within the VS Code GUI, as it runs remotely on a compute node.
Note that the general setup instructions and workflow for using a visualization remote desktop on Snellius are described here. In this section we only describe the specific steps needed to get VS Code working in such a remote desktop.

Installing VS Code on Snellius

We cannot provide VS Code as a software module on Snellius, as it uses a personalized license. SURF cannot legally install such software.

These are the (one-time) steps needed to install VS Code in your home directory on Snellius:

  1. Go to the VS Code download page at https://code.visualstudio.com/Download
  2. Below the Linux Penguin icon click the blue 64 bit link in the .tar.gz line and save the resulting file code-stable-x64-......tar.gz on your local system. Then transfer it to your home dir on Snellius.
  3. Extract the file somewhere in your home dir. You should end up with a directory called VSCode-linux-x64

Running VS Code in the VNC remote desktop

  1. Start a remote desktop job using the remotevis module and vnc_desktop command. Again, see the general remote desktop documentation mentioned above for more detail.
  2. Within the remote desktop open a terminal window
  3. In the terminal window run:
# Load the necessary modules to make VS Code work correctly
# If you need to load any more modules to be available it is best to do that here as well.
$ module load 2020 GCCcore/9.3.0 VirtualGL/2.6.4-GCCcore-9.3.0

# Navigate to the directory where you installed VS Code
$ cd ~/VSCode-linux-x64

# Start the VS Code GUI
# The vglrun prefix is needed for correct OpenGL functionality.
# The --no-sandbox is needed as the sandbox needs root rights to work, which you don't have as a regular user.
~/VSCode-linux-x64$ vglrun ./code --no-sandbox


At this point, the VS Code GUI will be running within the remote desktop window and can be used as normal.


When you are done working with the remote desktop don't forget to cancel the corresponding SLURM job

Connection Timing Out (what to do)

If you have an older version of Remote-SSH or VS Code, or connect to a system that you haven't connected in a while, you could run into timeout issues.
The reason is that the newer versions require a specific server for each remote host. This is explained in  "Connect to Remote Host" point 4.

The solution for that is quite simple:

  • F1 → Remote-SSH: New Connection
  • Follow the steps for establishing a new connection (enter user@remote  or the entry in your .ssh/config if you have that)
  • Select Linux from the list
  • That's all

In other instances you may want to increase the timeout, this can be done in this way: 

  • F1 → Remote-SSH : Settings
  • Scroll down to "RemoteSSH use Local Server and uncheck the checkbox
  • Scroll up to "Connection Timeout" and set it to 30 (not needed but good to have)