The world’s leading publication for data science, AI, and ML professionals.

Access remote code in a breeze with JupyterLab via SSH

Run remote Jupyter Lab in the local system using SSH

Photo by the author
Photo by the author

During our daily development work, there’s a need for us to access a remote system to

  • co-develop with peers in a shared workplace
  • train deep learning models on a powerful machine
  • deploy some scripts in production

The remote server might be referring to the public cloud, the private data center of an organization, a personal workstation, or others.

Photo by the author
Photo by the author

There is one common attribute with these remote servers – it is located in a fixed location, often with top-notch security and a good cooling system. Fundamentally, since the server is located remotely, the user would need to access the server via SSH or other UI-based remote desktop application.

UI-based remote desktop applications such as Anydesk, Teamviewer might not be the preferred option due to a few reasons

  • These applications might not be free for long term use
  • The need to have the application always running in the background
  • The remote server is headless
  • Increase of networking latency with the inclusion of graphical interface

Hence, the majority opt for SSH as a secured method to access a remote system. With SSH work solely works in the terminal, at times it might be difficult to edit configurations or code with just vim/vi.

Photo by the author.
Photo by the author.

Here’s where forwarding the web-based IDE – JupyterLab to the local system comes into handy.

Content:

  • Walkthrough of the workflow
  • Run JupyterLab in the background
  • Run JupyterLab as a root user
  • But I’m using Jupyter Notebook

Walkthrough of the workflow

Photo by the author
Photo by the author

To allow the running of JupyterLab in sync with the remote system, there are two main steps

  1. Initiate the running of JupyterLab without interface.
  2. Forward remote port to local port

Step 1: Initiate the running of JupyterLab without an interface

Photo by the author
Photo by the author

Ssh into the remote system in the form of

ssh <remote-user>@<remote-host>

With JupyterLab installed, run the following command

jupyter lab --no-browser --port <port-number>
Photo by the author
Photo by the author

Step 2: Forward remote port to local port

Photo by the author
Photo by the author

To forward the port, open another terminal and run the following command

ssh -N -L localhost:<local-port>:localhost:<remote-port> <remote-user>@<remote-host>
  • -N: Used for port forwarding. This suppresses the execution of a remote command.
  • -L: Bind remote port to local port.
Photo by the author
Photo by the author

Go ahead and open http://localhost:/ in the browser.

Photo by the author
Photo by the author

Enter the token key by copying from the terminal window from step 1.

Photo by the author
Photo by the author

There are three important points to take notes of:

  • In the current setup, the terminal running JupyterLab in the remote system has to be kept open. Alternatively, check out Run JupyterLab in the background, allowing the terminal to be closed without killing the process.
  • Likewise, the terminal forwarding the port has to be kept open during the whole operation.
  • The port forwarding terminal will not generate any output message to prompt that operation was successful (due to the flag -N being used).

Run JupyterLab in the background

There is an option to run JupyterLab in the background and allow the terminal to be closed without killing the JupyterLab process.

Simply add an ampersand & at the end of the command.

jupyter lab --no-browser --port <port-number> &amp;
Photo by the author
Photo by the author

To kill the process, use the following command to identify the process

ps -ef | grep Python | sort
Photo by the author
Photo by the author

Kill the process with

kill -9 <process-id>

Run JupyterLab as a root user

Running JupyterLab as a root user is not recommended.

Photo by the author
Photo by the author

However, the workaround for it is by adding a flag to explicitly allow running in the root user mode.

jupyter lab --no-browser --port <port-number> --allow-root
Photo by the author
Photo by the author

But I’m using Jupyter Notebook

There is still a vast community that opts for Jupyter Notebook as their go-to IDE. Fear not. All of the commands introduced in this article are compatible with Jupyter Notebook too.

Simply swap the command of JupyterLab into Jupyter Notebook.

Example:

jupyter notebook --no-browser --port <port-number>

The installation of the JupyterLab package also allows the running of the Jupyter Notebook too!

Photo by the author
Photo by the author

Till next time!


Related Articles