VS Code on NCIL server

Going to https://ncil.psychology.dal.ca in a web browser is the easiest way to access the lab's data and run analyses, because the Jupyterlab environment is already set up with all the Python and R packages you need, and Aaron has set everything up to work with our analysis pipelines.

However, we love VS Code and that's what we teach in Neural Data Science. You can also run VS Code on our server, but it requires some setup as described below.

The setup for this isn't exactly hard, but it involves a lot of different steps, working with e command line, and other technical things. It's a good idea to read it over first and decide if you feel you're up to the task. If you've taken NESC 3505, you sohuld be ok!

Advantages of running VS Code on the NCIL Server

  • It's VS Code! Compared to using Jupyterlab, you have the more powerful VS Code environment, including GithHub Copilot 🤓And in fact, you will run the VS Code app on your computer, so it will be configured exactly the way you like.

  • It's Faster and saves battery life: When you run VS Code on your own computer, all of the commands that you execute run on your computer's CPU (processing "chip"). Our server is probably faster than your computer. Also, if you're running off laptop battery, it will drain much faster if you're running code on your computer, rather than on the server.

  • It's Faster for another reason: Data live on the NCIL server. You should never copy lab data to your personal computer. In practice, this means that if you're running VS Code on your own computer, with the NCIL file server mounted on yoru computer, then all of the data you process has to travel over the internet from Dal to your laptop, get processed, then travel back across the internet to get saved on the server. This is waaaaay slower than the direct wired connection between the NCIL server has to the data storage.

  • You control your packages: The Jupyter setup uses a common set of Python packages, which Aaron updates periodically. You may have reasons for installing additional packages, or updating the packages yourself. With the VS Code method, you install your own Python environment and have full control over it.

Setup

None of the steps are particularly complicated, but there are a few things you need to do, and be sure you follow the instructions exactly, to ensure it works.

First Time Login to Server

The steps below require that you have a home folder on the server. This gets created the first time you log in. So, here we'll log into the. server, then log out again, replacing username with your NCIL user ID :

ssh -p 6245 username@ncil.psychology.dal.ca

This should prompt you for your server password, and then the Synology Secure Signin app will prompt you to confirm the login. Once you're logged in, you just need to type exit to log off:

exit

Create SSH Key

An SSH key is used in place of a password to authenticate you when you connect to the server. You will generate it on your own computer, and then copy the "public" part of it to JumpCloud, which will ensure that it's used when you try to connect to the server.

WINDOWS USERS: This won't work in the VS Code Terminal. You need to open the Git Bash application and run the next two commands in that terminal. If you followed the Neural Data Science instructions for setting up your computer with VS Code etc, you will already have this installed. If you don't, you can get it using this link: 64-bit Git for Windows Setup. All commands other than ssh-keygen and ssh-copy-id can be done in the VS Code terminal (but you could also keep using Git Bash)

  • Open VS Code on your computer, and install the VS Code Remote-SSH extension as well as the Remote Explorer extension.

  • If you're on Windows, install the Windows OpenSSH Client (nothing extra to install for Mac users)

  • If you don't already have one (and if you aren't sure, you don't have one ;), create an SSH key. To do this in VS Code type Ctrl ` (where ` is the key to the left of the 1 key) to open a terminal window. Windows users open Git Bash. Then run the command below, replacing the example with your email address:

ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept the default file location, and just hit return when it asks for a passphrase.

  • Now copy the public ssh key to the server with the following command, replacing your_username with your NCIL user ID:

ssh-copy-id -p 6245 -i .ssh/id_ed25519.pub your_username@ncil.psychology.dal.ca

Install mini version of Anaconda in your server account

When using VS Code, each used must install their own Python and packages. This means you can add extra packages or update whenever you like

  • Next we need to install Python and the necessary packages for NCIL data science in your personal account on the server. To do this, you need to log on to the server. In the Terminal in VS Code, run this command replacing your_username with your NCIL user ID:

ssh -p 6245 your_username@ncil.psychology.dal.ca
  • You will now be logged in to the server, and all the commands you execute will be run on the server. Run the following command to install miniforge, which is a 'lite' version of Anaconda:

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

Important: while mostly you can accept the defaults when prompted, be sure to explicitly type yes when asked Do you wish to update your shell profile to automatically initialize conda?

  • Once that completes, you need to log out of the server, then log back in again to continue. This completes the activation of miniforge. Log off with:

exit
  • Then log in again, using the same SSH command as before replacing your_username with your NCIL user ID

Pro tip: In the terminal, you can use the up arrow key to cycle back through previous commands. So, rather than typing the command below, you should be able to press the up key once and the command will appear, and you just need to hit Enter.

ssh -p 6245 your_username@ncil.psychology.dal.ca
  • Now install the nb_conda package, which is necessary for VS Code to find your Python environments:

mamba install -y nb_conda

Install the NCIL data science environment

Next you will create the NCIL data science environment, which will install all the packages you'll need. The NCIL data science environment (list of packages) lives in a GitHub repository, so the first thing we need to do is connect your server accout to GitHub

  • Run the following in the same terminal (logged onto NCIL):

gh auth login
  • This will log you in to GitHub so you can push and pull between repositories and your server account. Choose the default prompts, including Login with a web browser when prompted. This will show you a code, and then instruct you to press Enter to open GitHub in a web browser. Do this, and you will get an error - that's expected, because you can't launch a web browser from the server. You will see instructions with a URL (https://github.com/login/device) to paste into your web browser, which you should do, and then copy-paste the one-time code that the auth command had shown you before asking you to hit Enter. This should get you all logged in to GitHub and good to go.

  • Next, in the same terminal window run:

gh repo clone NeuroCognitiveImagingLab/NCIL_data_science_environments
cd NCIL_data_science_environments
mamba env create -f ncil.yml
  • Accept all the prompts/defaults it gives you. This will take a while to run. When it's done, type exit to log off the server in that terminal window.

Install Remote-SSH Extension in VS Code

  • Click on the Extensions icon in your VS Code Activity Bar

  • Search for and install:

    • Remote Development

    • Remote-SSH

    • Remote Explorer

Start a server session in VS Code

  • Go back to VS Code, and type Cmd-Shift-p (Ctrl-Shift-p on Windows), then in the box that appears start typing remote-ssh You should see a set of options appear; select Remote-SSH: Add New SSH Host... Then type the following, replacing your_username with your NCIL user ID:

ssh -p 6245 your_username@ncil.psychology.dal.ca
  • Hit Enter to select the default option for the SSH configuration file to update.

  • In VS Code, type Cmd-Shift-p (Ctrl-Shift-p on Windows)

  • Start typing Remote-SSH and select Remote-SSH: Connect Current Window to Host

  • The connection you created during setup should appear, probably as ncil.psychology.dal.ca. Select this. This should connect your VS Code window to the server. You'll be able to tell because in the bottom left corner, it will say SSH: and the server address.

  • Last thing is to set up the remote to start in the NCILscience directory. To do this, click the Open Folder button that should show in the main VS Code window. If not, select the File Explorer in the VS Code Activity bar (the icons on the left side of the window). In the box that appears, delete what's there and paste in: /nfs/NCILscience then hit Enter.

Install VS Code Extensions

Even if you have extensions installed in VS Code on your computer, you will need to re-install them in your server account.

  • Click on the Extensions icon in your VS Code Activity Bar

  • Search for and install (at least) the following:

    • Python

    • Jupyter

    • GitHub Copilot

    • Any others you like to use (nothing else is required, though)

Congratulations, you're done the setup! You should close the VS Code window that you were working in. If you want to start working right away, follow the steps below to login as you will anytime in the future you want to work.

Connect VS Code to the server to run a session

This is what you'll do every time you want to work on the server via VS Code.

  • Open VS Code and click the Remote Explorer extension icon in the Activity Bar

  • You should see ncil.psychology.dal.ca listed there, with at least one folder location (NCILscience). Click on that and then click the right-pointing arrow that appears to the right of the folder name. This should connect you to the server in that window, and you're good to go!

  • You can confirm you're connected by checking the indicator in the bottom left of your VS Code window show that you're connected to the server.

  • You can also create shortcuts to project folders that you're working on, rather than having to navigate to a specific folder every time you log on. Each shortcut you create will show up listed in the Remote Explorer. To do this, open a new VS Code window and open the Remote Explorer. But rather than clicking on NCILscience, instead click on the arrow next to the server name. This will connect and show the Open Folder button. This time, paste in /nfs/NCILscience, but then scroll through the list of fodlers on NCILscience that shows up, and navigate to the folder you want to work in (e.g., the BIDS folder for your project), and then hit Enter. Now that location will be saved as a shortcut in Remote Explorer so you can login directly to that project.

Run a Jupyter Notebook in VS Code on the Server

Normally this will most "just work". However, the first time you do this, and possibly in the future for new botebook files you open, you will need to choose the correct kernel - the miniforge Python NCIL environment that you installed earlier. To do this, with the notebook file open, click "Select Kernel" in the top right of the VS Code window. If it already says Python, click on it anyway. You will probably see a list of environments, and openfully ncil is one of them. If not, click Python environments and

Last updated