Skip to main content

Install Jupyter Notebook on Ubuntu 22.04: A Safe Setup

Laptop displaying a notebook-style code and chart interface on a sunlit desk

To install Jupyter Notebook on Ubuntu 22.04, use a Python virtual environment and run the server as an ordinary user. This keeps project packages separate from Ubuntu’s system Python and gives you a straightforward way to start, verify and stop a single-user notebook session.

The walkthrough supports a local Ubuntu machine or a remote server reached through SSH. It does not expose the notebook port publicly. Commands should be run in your own authorised environment; package compatibility depends on the Python and Notebook versions you install.

Before installing Jupyter Notebook on Ubuntu 22.04

You need an ordinary user account, sudo permission for system packages, network access to the configured Ubuntu and Python package repositories, and a browser on your own computer. For a remote machine, confirm that SSH access works first.

Use a new project directory if you already have an environment with this name. Avoid changing the system Python interpreter or installing project packages with sudo pip.

1. Install Python prerequisites

sudo apt update
sudo apt install python3 python3-venv python3-pip
python3 --version

These commands refresh package metadata and install the Python tools used below. They do not perform a full operating-system upgrade. Plan broader updates and any required restart through your usual maintenance process.

2. Create a dedicated virtual environment

mkdir -p ~/notebook-project
cd ~/notebook-project
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install notebook
jupyter notebook --version

Activating the environment makes its Python and installed command-line tools available in the current shell. Use python -m pip so package installation follows the interpreter you selected. The Notebook interface can differ between releases; the current notebook package should not be confused with screenshots of older classic releases.

If installation reports an unsupported Python version, read the package’s requirements and choose a supported combination. Do not force an incompatible installation or replace Ubuntu’s system interpreter to make it work.

3. Start Jupyter on localhost

jupyter notebook --no-browser --ip=127.0.0.1 --port=8888

Leave this terminal open. Jupyter prints connection information and normally provides a token-protected URL. Keep that token private: notebook access allows code execution with the permissions of the account running the server.

On a local Ubuntu desktop, open the printed localhost URL in your browser. Keep authentication enabled. If the requested port is occupied, check the actual port reported by the server rather than assuming it started on 8888.

4. Connect to a remote notebook through SSH

From your own computer, run the following command in another terminal. Replace the account and hostname with your authorised SSH destination.

ssh -N -L 8888:127.0.0.1:8888 youruser@your-server

Open http://127.0.0.1:8888 locally and authenticate with the token from the remote Jupyter session. The tunnel forwards the local port to the remote machine’s loopback interface; an inbound public rule for port 8888 is unnecessary for this design.

If the local port is already used, try ssh -N -L 8889:127.0.0.1:8888 youruser@your-server and open local port 8889. If Jupyter selected a different remote port, change the final port in the tunnel accordingly.

5. Verify the notebook and Python kernel

Create a Python notebook through the interface. Run these simple cells, save the notebook and reopen it:

import sys
print(sys.executable)
print(2 + 2)

Check that the interpreter belongs to the intended virtual environment and that the calculation returns 4. A successful browser login alone does not confirm that the selected kernel uses the environment you expected.

If you need a named kernel for this environment, activate it and register one deliberately:

python -m pip install ipykernel
python -m ipykernel install --user --name notebook-project --display-name "Python (notebook-project)"

Select that kernel in the notebook interface. Kernel registration records a path to the interpreter; moving or deleting the environment can leave a stale entry.

Common installation problems

  • venv creation fails: confirm that the matching Ubuntu venv package is installed and inspect the complete error.
  • jupyter is not found: reactivate ~/notebook-project/.venv and check python -m pip show notebook.
  • The browser cannot connect: verify the server is running, check its chosen port and confirm the SSH tunnel has not closed.
  • A package imports in the terminal but not the notebook: compare sys.executable in both environments and choose the correct kernel.

Stop cleanly and keep the project reproducible

Stop the server with Ctrl+C in its terminal and confirm shutdown when prompted. Close the SSH tunnel separately, then run deactivate in the environment shell. Save notebook files in a backed-up project location.

For a project you need to reproduce, record dependency versions with python -m pip freeze > requirements.txt. Review updates before applying them to an established environment. A multi-user service needs a separate design for identity, HTTPS, isolation and operations; consider JupyterHub rather than sharing one user’s token.

Official references

For storage troubleshooting before a large notebook installation, see the Linux disk-space troubleshooting guide.

A practical guide for your next challenge. A fresh perspective for everyday life. Discover both through Nandan’s latest articles.

Post Comments

Leave a Reply

Your email address will not be published. Required fields are marked *