How to Install Jupyter Notebook on Ubuntu 24.04, 22.04 or 20.04

This guide will demonstrate how to install Jupyter Notebook on Ubuntu 24.04, 22.04, or 20.04 LTS Linux releases utilizing the command-line terminal with Python and PIP.

Jupyter Notebook is a pivotal tool for interactive computing across many programming languages. It offers an environment where users can create and share documents that contain live code, equations, visualizations, and narrative text. The versatility of the Jupyter Notebook stems from its comprehensive list of features, making it a favored choice for data scientists, researchers, and educators alike. With its web-based interface, Jupyter Notebook enables users to manage documents that combine code execution with rich text elements, enhancing the presentation and understanding of complex data analyses.

Key Technical Features and Highlights:

  • Interactivity: Execute code in various languages and see results in real-time.
  • Data Visualization: Integrate charts, graphs, and multimedia seamlessly.
  • Live Code Sharing: Facilitate collaboration with shareable notebooks.
  • Support for Numerous Languages: Compatible with languages like Python, R, Julia, and Scala.
  • Extension Ecosystem: Expand functionality with a wide array of plugins.
  • Integration with Data Science Tools: Works effortlessly with libraries like NumPy, Pandas, and Matplotlib.
  • Educational Resource: Ideal for teaching coding and data science concepts.
  • Version Control Friendly: Easily track changes and collaborate on projects.

These attributes underscore Jupyter Notebook’s capability to serve as an essential tool for data exploration, documentation, teaching, and learning. Its adaptability to various coding environments and the ease with which it allows users to visualize data and share insights make it an invaluable asset in the modern data analysis workflow.

Now, let’s dive into the technical steps.

Pre-Installation Steps for Jupyter Notebook on Ubuntu

Update System Packages on Ubuntu Before Jupyter Notebook Installation

Before proceeding with any major installation, it is crucial to start by refreshing the package repositories of your Ubuntu system. This step prepares your system for new installations and updates.

To update your package repositories, open your terminal and enter the following commands:

sudo apt update
sudo apt upgrade

By executing these commands, you ensure that your system’s package repositories are up-to-date, laying a solid foundation for installing Python 3 seamlessly.

Install Python 3 for Jupyter Notebook

With your system packages updated, the next step is to install Python 3. Ubuntu’s package manager, apt, simplifies this process.

To install Python 3, along with its essential components, run the following command in your terminal:

sudo apt install python3 python3-dev python3-venv python3-pip

This command not only installs Python 3 but also includes the Python 3 development files (python3-dev), the virtual environment package (python3-venv), and the Python package manager (python3-pip).

Terminal output showing Python 3 packages installation for Jupyter Notebook on Ubuntu.
Terminal output of Python 3 packages installation required for Jupyter Notebook.

Creating a Virtual Environment for Jupyter Notebook

Starting with Ubuntu 20.04 and later versions, it’s a best practice to use virtual environments for managing Python packages through Pip. Virtual environments are essential for Python development, as they provide isolated spaces for different projects and their dependencies, reducing the risk of version conflicts.

To set up a virtual environment, use the following command in your terminal:

python3 -m venv myenv

Here, myenv is the name of your virtual environment, which you can rename according to your project’s needs. Utilizing virtual environments is a key practice in Python programming, ensuring that each project operates in its own isolated environment, thus promoting cleaner and more stable code development.

Install Jupyter Notebook on Ubuntu via PIP

Activate Virtual Environment for Jupyter Notebook Installation

To begin the Jupyter Notebook installation, start by activating the virtual environment you previously set up. This step is crucial for maintaining an organized, conflict-free Python workspace.

Activate your virtual environment with the following command:

source myenv/bin/activate

Replace myenv with your virtual environment’s name if it differs. Activating the virtual environment ensures that all installations and operations are confined to this isolated environment, preventing any unintended system-wide changes.

Upgrade Python Pip Before Installing Jupyter

Staying updated with the latest versions of software tools is a key practice in technology. Before proceeding with the Jupyter Notebook installation, upgrade Python’s package manager, Pip, to its most recent version. To upgrade Pip, execute this command:

pip install --upgrade pip

Upgrading Pip ensures you have the latest features and security enhancements essential for a smooth and secure installation process.

Install Jupyter Notebook Using Pip Command

With Pip updated, you’re ready to install Jupyter Notebook. This powerful open-source tool is essential for tasks like data cleaning, numerical simulation, statistical modeling, and machine learning. To install Jupyter Notebook, run the following command:

pip install jupyter

This step commands Pip to download and install Jupyter Notebook, providing you with a versatile environment for your data science and programming tasks.

Launch Jupyter Notebook

After the successful installation of Jupyter Notebook in the virtual environment, it’s time to launch it. Starting Jupyter Notebook is straightforward. Use the command below:

jupyter notebook

This command initiates the Jupyter Notebook interface, typically opening it in your default web browser. You’re now ready to create and work with interactive documents containing live code, visualizations, and text annotations.

Enable Jupyter Notebook Service by Default on Ubuntu

If you frequently use Jupyter Notebook, you might find it convenient to have the virtual environment enabled and the Jupyter Notebook service running by default whenever you start your system. This optional step involves a bit more configuration but can significantly streamline your workflow.

Enabling Virtual Environment by Default

Edit .bashrc File

Open the .bashrc file in your home directory using a text editor like nano:

nano ~/.bashrc

Add Activation Script

At the end of the .bashrc file, append the line to automatically activate your virtual environment when opening a new terminal session:

source /path/to/your/virtualenv/myenv/bin/activate

Ensure you replace /path/to/your/virtualenv/myenv with the actual path of your virtual environment.

Save and Exit

After adding the activation script, save the changes and exit the editor. In nano, this is done by pressing CTRL + X, followed by Y, and Enter.

Setting Up Jupyter Notebook Service

Create a Systemd Service File

Create a new service file for systemd:

sudo nano /etc/systemd/system/jupyter.service

Configure the Service

In the service file, input the following configuration:

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/path/to/your/virtualenv/myenv/bin/jupyter notebook
User=your_username
Group=your_group
WorkingDirectory=/path/to/your/working/directory
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Modify the ExecStart, User, Group, and WorkingDirectory with your specific details. The ExecStart line should specifically point to the Jupyter executable within the virtual environment.

Reload Systemd Daemon

After configuring the Jupyter service file, you need to reload the systemd daemon to apply the changes:

sudo systemctl daemon-reload

This command informs systemd that a new service file has been added or an existing one has been modified.

Enable and Start the Service

Following the daemon-reload, proceed to enable and start the Jupyter service:

sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service

These commands will set the Jupyter Notebook service to start automatically on boot and initiate it immediately.

Set Up SSH Tunneling for Jupyter Notebook on Ubuntu

Understanding SSH Tunneling for Jupyter Notebook

SSH tunneling is a method to forward network traffic securely over an encrypted SSH connection. It’s particularly useful for securely accessing remote services like Jupyter Notebook. In this setup, SSH tunneling allows you to securely interact with Jupyter Notebook running on a remote server from your local machine.

Establishing an SSH Tunnel

To begin, open a new terminal window on your local machine. Use the following SSH command to create a secure tunnel:

ssh -L 8888:localhost:8888 your_server_username@your_server_ip

Breaking Down the Command

  • ssh: Initiates an SSH connection.
  • -L 8888:localhost:8888: Specifies local port forwarding. Here, the local port 8888 is tunneled to the same port on the remote server.
  • your_server_username: Replace this with your actual username on the remote server.
  • your_server_ip: Replace this with your server’s IP address.

For instance, if your username is joshua and the server’s IP is 203.0.113.0, the command would be:

ssh -L 8888:localhost:8888 joshua@203.0.113.0

Handling Port Conflicts

If port 8888 is already in use on your local machine, replace 8888 with any available port.

Initializing Jupyter Notebook

After establishing the SSH tunnel without any errors, you can start Jupyter Notebook on the remote server. Run the following command on the server:

jupyter notebook

Accessing Jupyter Notebook

Once Jupyter Notebook starts, it will provide a URL in the terminal output. This URL, typically starting with http://localhost:8888, includes a security token. Open your preferred web browser on your local machine, and navigate to this URL.

Note: If prompted, enter the token number provided in the Jupyter Notebook’s output. This token authenticates your session.

Jupyter Notebook Web Interface on Ubuntu

Accessing Jupyter Notebook’s Web Interface

After establishing a secure SSH tunneling connection, you can access the Jupyter Notebook via a web browser. Jupyter Notebook offers a user-friendly interface and a wide range of features, making it an indispensable tool for data scientists and programmers.

Navigating Files and Folders

Jupyter Notebook displays all files and folders in its starting directory. To efficiently manage your projects, initiate Jupyter Notebook in the project files’ directory. This practice ensures quick and easy access to all necessary resources.

Jupyter Notebook web interface on Ubuntu 24.04, 22.04, 20.04.
A look at the Jupyter Notebook web interface on Ubuntu Linux.

Exploring Notebook Functionality

Upon opening a Jupyter Notebook, you have the flexibility to run Python code in cells or use markdown for documentation. This versatility makes the Jupyter Notebook ideal for combining code, visualizations, and detailed explanations in a single document.

Experimenting With Python Code in Jupyter

To understand Jupyter’s capabilities better, let’s execute a Python code snippet. Consider the following example:

# Defining a simple greeting function
def greet(name):
    return f"Hello, {name}!"

# Let's greet 'John'
print(greet('John'))

In this example, we define a greet function that accepts a name and returns a personalized greeting. When we pass ‘John’ as an argument to the greet function, it returns “Hello, John!” and prints the greeting.

Python Hello World code in Jupyter Notebook on Ubuntu 24.04, 22.04, 20.04.
Running Python Hello World code in Jupyter Notebook on Ubuntu.

Conclusion

Throughout this guide, we’ve walked step by step to get Jupyter Notebook up and running on Ubuntu 24.04, 22.04, or 20.04. We’ve covered everything from the initial install to diving into the web interface and even tinkering with Python code snippets. My final nugget of advice? Dive deep into Jupyter’s vast sea of features. It’s more than just a tool; it’s your gateway to making your data science and coding projects come alive.

Jupyter Notebook really shines when you start playing around with its myriad of functionalities, from visualizing data to documenting your progress. So, don’t hesitate to experiment and push the boundaries of your projects. Trust me, the more you use it, the more indispensable it becomes. Here’s to many insightful and productive coding sessions ahead!

Leave a Comment