Jupyter Notebook is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. It’s widely used in data science, machine learning, and scientific research for its versatility and ease of use.
- Interactive Code Execution: Write and run code in real time, seeing the results immediately.
- Rich Text Support: Combine code with Markdown, LaTeX, and HTML for comprehensive documentation.
- Data Visualization: Embed plots and charts directly in your notebook for a visual representation of your data.
- Extensible: Support for over 40 programming languages, including Python, R, and Julia.
With these features, Jupyter Notebook transforms the way we work with data, making it accessible and interactive. It’s particularly beneficial for those who need to iterate quickly and collaborate seamlessly.
With the introduction out of the way, let’s explore how to install Jupyter Notebook on Ubuntu 24.04, 22.04, or 20.04 LTS using the command-line terminal.
Pre-Installation Steps for Jupyter Notebook
Update System Packages on Ubuntu Before Jupyter Notebook Installation
Before proceeding with any central 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 seamlessly installing Python 3.
Install Python 3 for Jupyter Notebook
After updating your system packages, you should 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
).
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. It ensures that each project operates in its own isolated environment, thus promoting cleaner and more stable code development.
Install Jupyter Notebook via PIP
Activate Virtual Environment for Jupyter Notebook Installation
To begin the Jupyter Notebook installation, activate 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 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 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 the 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
Understanding SSH Tunneling for Jupyter Notebook
SSH tunneling is a method for forwarding 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 interact securely 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
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.
Exploring Notebook Functionality
Upon opening a Jupyter Notebook, you can 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.
Conclusion
In this guide, we’ve covered the installation and initial setup of Jupyter Notebook on Ubuntu 24.04, 22.04, or 20.04, walked through using the web interface, and experimented with Python code snippets. My final recommendation is to explore Jupyter’s extensive features, like data visualization and documentation, to fully leverage its capabilities. Keep experimenting and pushing the limits of your projects.