Run Jupyter Lab on a Remote Server

Geronimo
2 min readDec 9, 2020

Jupyter lab is a great web application for Python coding. This post instructs you how to run jupyter lab on a remote server using an ssh tunnel. I assume you know jupyter lab and already used it locally. Now, you want to run jupyter lab on a remote ubuntu server and access this jupyter lab server from your local machine (e.g. notebook). This way you can code directly on a remote server (e.g. a DGX machine) trough the web browser of your personal end device. — Hey, that’s cool.

  1. Turn on your laptop. Turn on the remote server. Enable an ssh connection between your local machine and the remote server.
  2. Establish an ssh connection and set up an ssh tunnel. For instance:
$ ssh mercury@192.168.127.123 -L 8889:localhost:8889

opens an ssh tunnel on port 8889 and logs in the user mercury on the server 192.168.127.123.

3. Now, you are logged in on the remote server. Install conda, jupyter lab on the remote server if not already done. I am doing this in the snippet below and also create the conda environment named jlabenv.

mercury@DGX:~$ sudo apt install conda
(base) mercury@DGX:~$ conda create --name=jlabenv
(base) mercury@DGX:~$ conda activate jlabenv
(jlabenv) mercury@DGX:~$ conda install jupyterlab

4. Start jupyter lab without opening a browser and let it connect on the port you set the ssh tunnel on. Below, I type a command to start jupyter lab as a background process using nohup and dump the output to a specified logfile:

$ nohup jupyter lab --no-browser --port=8889 > ./jupyterlab.log &

The &-sign allows me to get a new prompt.

5. Get the token to connect to the jupyter lab server. You can copy the token from the log file. Just open it with your favourite editor. I choose vim:

$ vim ~/tmp/jupyterlab.log

6. Copy and paste the given URL to access the jupyter server from your local machine.

Here you go!

Just make sure not to close the ssh tunnel. Otherwise you’d loose the connection to the server. If you lose the connection, don’t worry, you can just reconnect and return to the jupyter lab server where you left.

Want to check if jupyter lab is still running? That’s easy:

Find the process using ps:

$ ps aux | grep jupyter-lab

Want to shut down jupyter lab? You can for example use pgrep to get the Process ID (PID) and kill to kill the process:

$ kill $(pgrep jupyter-lab)

--

--