Many of us are provided access to GPU clusters or storage space on a server (Linux-based) for various purposes, be it for research or just as such. Such accounts are generally used by sshing via the terminal, which restricts us to terminal-based editors for coding, apart from making the task of project maintenance tedious.

I wasn’t very comfortable using vim for large projects, and was in search for a solution when I came across JupyterLab. Here, I provide a step-by-step tutorial to set up a public server for JupterLab, so that you can have a password-authenticated, browser-based IDE. No more cumbersome sshing!

Setting up a proxy for internet access

(Ironically?) ssh into the server.

In case your account does not have internet access, you will first need to set up a proxy. For this, you will need to know the details about the proxy server provided by your institute. I will consider netmon.iitb.ac.in for the remaining steps.

Add these lines to ~/.profile file

export HTTP_PROXY = <LDAP-ID>:<LDAP-PWD>@netmon.iitb.ac.in:80
export HTTPS_PROXY = <LDAP-ID>:<LDAP-PWD>@netmon.iitb.ac.in:80

Execute source ~/.profile to reflect these changes in the current session. Other possible alternatives are using a terminal based browser to login to internet.iitb.ac.in, but the one mentioned above is the least tiring!

Setting up JupyterLab and packages locally

pip3 install --user jupyter jupyterlab

This installs jupyter and jupyterlab in ~/.local/bin directory; you don’t need root access for this. You can locally install other packages required for the project to ~/packages directory by using

pip install --target packages/ <package-name>

Also add packages/ to the PYTHONPATH variable so that Python can look for packages in this directory. This can be done by adding this line to ~/.profile

export PYTHONPATH=$PYTHONPATH:~/packages

Remember to use --user instead of --target <DIR> when the package to be installed contains executables. This is because executables are searched for in the PATH variable, unlike library packages. Also, execute source ~/.profile in order to reflect these changes in the current session.

Configuration

Generate a configuration file for Jupyter Notebook in ~/.jupyter

jupyter notebook --generate-config 

Generate a password for authentication

jupyter notebook password

The password is stored as a hashed value in json format in ~/.jupyter/jupyter_notebook_config.json. Edit the ~/.jupyter/jupyter_notebook_config.py file by changing these lines

c.NotebookApp.allow_password_change = False
c.NotebookApp.allow_root = False
c.NotebookApp.base_url = '/'
c.NotebookApp.default_url = '/lab'
c.NotebookApp.disable_check_xsrf = False
c.NotebookApp.ip = IP # IP address on which you want to serve the lab
c.NotebookApp.nbserver_extensions = { 'jupyterlab' : True }
c.NotebookApp.notebook_dir = path/to/source/code
c.NotebookApp.open_browser = False
c.NotebookApp.password_required = True
c.NotebookApp.port = 8888 # port on which you want to host the lab

After making these changes, enable the serverextension for jupyterlab by executing

jupyter serverextension enable --py jupyterlab

Starting the server

For hosting the server, you will need screen or an equivalent utility that allows commands to be run even when the connection to the server is interrupted.

  • Start a new screen using screen -S jupyterlab.
  • Simply execute jupyter lab to start the server.
  • Press Ctrl+A and then Ctrl+D to detach from the screen.
  • Close the connection to the server.

Using JupyterLab

Open a browser and visit IP:PORT (these are the values you had entered in the jupyter_notebook_config.py file). You will be redirected to a login page, where you need to enter the password. After authentication, you will be able to use a browser-based IDE, complete with terminal support, directory listing and lots more!

Cheers!