Jupyter Lab is a web-based interface that allows users to work with code, data, CLI, and Jupyter Notebooks all in an interactive environment entirely in the browser. This guide will provide the steps required to set up this installation on your Ubuntu server in a Python Virtual Environment and connect to the server using SSH tunneling.
1. Before you install all of the required dependencies for Jupyter, ensure your Ubuntu packages are up to date (Note: This may break your current VM if services are currently active)
sudo apt update
2. Next, install pip and the Python header files, which are used by some of Jupyter’s dependencies:
sudo apt install -y python3-pip python3-dev
- You may get a pop-up similar to the below, hit enter when this appears:
3. Now create a python virtual environment to manage your projects you create (Jupyter will be installed in this virtual environment so that you isolate any issues that could potentially occur when working on a project)
First upgrade pip and then install the 'virtualenv' package:
sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv
- The
-H
flag ensures that the security policy sets thehome
environment variable to the home directory of the target user.
4. Now that you have 'virtualenv' installed, you can start creating where your virtual environment will live. Start by creating a directory for this environment and change directories into it (You can call this directory whatever you’d like):
mkdir ~/my_v_env
cd ~/my_v_env
5. Within this directory, create the python virtual environment using the following command (This will create a new directory within the directory you created. You can call it whatever you would like as well. Inside this directory, it will install a local version of python and pip which will be used to install and configure an isolated python environment for Jupyter):
virtualenv my_project
6. Before installing Jupyter, we need to activate the virtual environment. You can do that by typing:
source my_project/bin/activate
7. This should now update your command line to the following format which will indicate you are in your virtual environment and ready to install Jupyter:
(my_project)user@host:~/my_v_env$
8. To install Jupyter, input the following command:
pip install jupyter
9. Once installed, you can run Jupyter Lab or Jupyter Notebook with:
jupyter lab
or
jupyter notebook
You will then see an output similar to the following:
[I 2024-10-28 20:05:39.075 ServerApp] jupyter_lsp | extension was successfully linked.
[I 2024-10-28 20:05:39.078 ServerApp] jupyter_server_terminals | extension was successfully linked.
[I 2024-10-28 20:05:39.081 ServerApp] jupyterlab | extension was successfully linked.
[I 2024-10-28 20:05:39.084 ServerApp] notebook | extension was successfully linked.
[I 2024-10-28 20:05:39.416 ServerApp] notebook_shim | extension was successfully linked.
[I 2024-10-28 20:05:39.521 ServerApp] notebook_shim | extension was successfully loaded.
[I 2024-10-28 20:05:39.522 ServerApp] jupyter_lsp | extension was successfully loaded.
[I 2024-10-28 20:05:39.523 ServerApp] jupyter_server_terminals | extension was successfully loaded.
[I 2024-10-28 20:05:39.530 LabApp] JupyterLab extension loaded from /home/ubuntu/my_project/lib/python3.10/site-packages/jupyterlab
[I 2024-10-28 20:05:39.530 LabApp] JupyterLab application directory is /home/ubuntu/my_project/share/jupyter/lab
[I 2024-10-28 20:05:39.530 LabApp] Extension Manager is 'pypi'.
[I 2024-10-28 20:05:39.557 ServerApp] jupyterlab | extension was successfully loaded.
[I 2024-10-28 20:05:39.561 ServerApp] notebook | extension was successfully loaded.
[I 2024-10-28 20:05:39.561 ServerApp] Serving notebooks from local directory: /home/ubuntu
[I 2024-10-28 20:05:39.561 ServerApp] Jupyter Server 2.14.2 is running at:
[I 2024-10-28 20:05:39.561 ServerApp] http://localhost:8888/lab?token=a712995500e04dc5d73974f293232af79acc3ee11eb63bec
[I 2024-10-28 20:05:39.561 ServerApp] http://127.0.0.1:8888/lab?token=a712995500e04dc5d73974f293232af79acc3ee11eb63bec
[I 2024-10-28 20:05:39.561 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 2024-10-28 20:05:39.566 ServerApp] No web browser found: Error('could not locate runnable browser').
[C 2024-10-28 20:05:39.566 ServerApp] To access the server, open this file in a browser: file:///home/ubuntu/.local/share/jupyter/runtime/jpserver-496316-open.html
Or copy and paste one of these URLs: http://localhost:8888/lab?token=a712995500e04dc5d73974f293232af79acc3ee11eb63bec
http://127.0.0.1:8888/lab?token=a712995500e04dc5d73974f293232af79acc3ee11eb63bec
10. If you aren’t running Jupyter on a server, you can connect to it using the link provided in the above output, for example:
http://localhost:8888/lab?token=a712995500e04dc5d73974f293232af79acc3ee11eb63bec
If you’re connecting to Jupyter using a server, you will need to connect using SSH tunneling. For that we will need to CTRL+C to stop the process and exit to reconnect again.
11. To create an SSH tunnel, it is similar to the normal SSH command except we add a few parameters:
ssh -L 8888:localhost:8888 ubuntu@<Public_Server_IP>
- ssh - opens an SSH connection
- -L - specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (server). This means that whatever is running on the second port number (e.g. 8888) on the server will appear on the first port number on your local computer.
For assistance with configuring firewall rules to open ports if necessary, please refer to this article.
12. Once you’re back in your server, activate the virtual environment again and launch Jupyter Lab or Jupyter Notebook:
cd ~/my_v_env
source my_project/bin/activate
jupyter lab OR jupyter notebook
Copy paste the URL from the output in your local browser and Voilà!
Comments
0 comments
Article is closed for comments.