Jupyter Notebook is a powerful, web-based interactive computing platform that allows users to create and share documents containing live code, equations, visualizations, and explanatory text. In this tutorial, we will walk you through the step-by-step process of installing and configuring Jupyter Notebook on Ubuntu 22.04. By the end of this guide, you will have a fully functional Jupyter Notebook environment running on your Ubuntu server.
Prerequisites
Before we dive into the installation process, let’s make sure you have everything you need:
- A server running Ubuntu 22.04
- A valid domain name pointed to your server’s IP address
- Root access to your server
If you have these prerequisites in place, we can proceed to the next section.
Getting Started
Before we install Jupyter Notebook, it’s always a good idea to update and upgrade all the software packages on your server. Open your terminal and run the following commands:
sudo apt update -y sudo apt upgrade -y
Once the packages are updated and upgraded, we can move on to the next step.
Install Python
Jupyter Notebook relies on Python, so the first thing we need to do is install Python and a few other dependencies. Run the following command to install Python and pip:
sudo apt-get install python3 python3-pip -y
After the installation is complete, you can verify the Python version by running the following command:
python3 --version
Make sure you have Python 3.10.6 or a compatible version installed. Additionally, update pip to the latest version by running:
pip3 install --upgrade pip
To confirm that pip has been successfully updated, run the following command:
pip3 --version
You should see the version information for pip.
Create a Python Virtual Environment
To keep our Jupyter Notebook installation clean and isolated, we will create a Python virtual environment. This allows us to install and manage packages specific to the Jupyter Notebook project without affecting the global Python environment.
First, create a directory where you want to store your Jupyter Notebook project. Let’s call itmyproject
:
mkdir ~/myproject
Navigate to the project directory:
cd ~/myproject
Next, create a virtual environment:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
You will notice that your command prompt changes to indicate that you are now working within the virtual environment.
Install Jupyter Notebook
With the virtual environment activated, we can install Jupyter Notebook using pip:
pip install jupyter
This command will download and install all the necessary packages and dependencies required to run Jupyter Notebook. Once the installation is complete, you can start Jupyter Notebook by running:
jupyter notebook
If everything is set up correctly, you should see output similar to the following:
[I 14:23:26.729 NotebookApp] Serving notebooks from local directory: /root/myproject
[I 14:23:26.729 NotebookApp] Jupyter Notebook 6.4.12 is running at:
[I 14:23:26.729 NotebookApp] http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
[I 14:23:26.729 NotebookApp] or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
[I 14:23:26.730 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 14:23:26.734 NotebookApp] No web browser found: could not locate runnable browser.
[C 14:23:26.734 NotebookApp] To access the notebook, open this file in a browser: file:///root/.local/share/jupyter/runtime/nbserver-3214-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625 or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
This output indicates that Jupyter Notebook is running and accessible at http://localhost:8888
. However, since we haven’t configured any security measures yet, it is not recommended to access Jupyter Notebook directly from your browser at this point.
Secure Jupyter Notebook with a Password
To secure your Jupyter Notebook installation, it’s essential to set up a password. This ensures that only authorized users can access and interact with your notebooks.
First, generate a Jupyter Notebook configuration file:
jupyter notebook --generate-config
This command will create a default configuration file at /root/.jupyter/jupyter_notebook_config.py
. Open the file with your favorite text editor:
nano /root/.jupyter/jupyter_notebook_config.py
Scroll down to the section labeled ## The encrypted password to use
. Uncomment the line and set a password by running the following command:
python3 -c "from notebook.auth import passwd; print(passwd())"
This command will prompt you to enter and verify a password. Once you’ve entered and verified the password, it will generate a hashed version of the password. Copy the hashed password and paste it into the configuration file, replacing the existing password.
Save and close the file.
Configure Jupyter Notebook as a Systemd Service
To ensure that Jupyter Notebook starts automatically and runs in the background whenever your server boots up, we will configure it as a systemd service.
Create a new systemd service unit file:
sudo nano /etc/systemd/system/jupyter.service
Add the following content to the file:
[Unit]
Description=Jupyter Notebook
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/root/myproject/venv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_notebook_config.py --allow-root
User=root
Group=root
WorkingDirectory=/root/myproject/venv
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Save and close the file.
Reload systemd to pick up the new service file:
sudo systemctl daemon-reload
Start Jupyter Notebook:
sudo systemctl start jupyter
Enable Jupyter Notebook to start at system boot:
sudo systemctl enable jupyter
To check the status of the Jupyter Notebook service, run:
sudo systemctl status jupyter
If everything is set up correctly, you should see output indicating that the service is active and running.
Configure Nginx as a Reverse Proxy for Jupyter Notebook
By default, Jupyter Notebook runs on port 8888, which is not accessible over the standard HTTP port 80. To enable users to access Jupyter Notebook via a web browser, we will configure Nginx as a reverse proxy.
Install Nginx:
sudo apt-get install nginx -y
Create a new Nginx configuration file for Jupyter Notebook:
sudo nano /etc/nginx/conf.d/jupyter.conf
Add the following configuration to the file:
[Unit] Description=Jupyter Notebook [Service] Type=simple PIDFile=/run/jupyter.pid ExecStart=/root/myproject/venv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_notebook_config.py --allow-root User=root Group=root WorkingDirectory=/root/myproject/venv Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
Save and close the file.
Verify the Nginx configuration for syntax errors:
sudo nginx -t
If there are no syntax errors, restart Nginx to apply the changes:
sudo systemctl restart nginx
You can check the status of Nginx to ensure that it is running without any issues:
sudo systemctl status nginx
If Nginx is active and running, you should see output indicating that the service is up and operational.
Access Jupyter Notebook
Now that everything is set up and configured correctly, you can access Jupyter Notebook in your web browser.
Open your favorite browser and visit http://example.com
(replace example.com
with your actual domain name). You will be prompted to enter the password you set earlier. Once authenticated, you will be taken to the Jupyter Notebook dashboard, where you can create new notebooks, open existing ones, and run code cells.
Congratulations! You have successfully set up Jupyter Notebook on your Ubuntu 22.04 server and configured it to run behind an Nginx reverse proxy. You now have a powerful and secure environment for data analysis, machine learning, and interactive coding.
If you need any assistance or have any questions, feel free to reach out to us at Shape.host. Our Cloud VPS solutions provide scalable and reliable hosting for your Jupyter Notebook projects. Get in touch with us today to learn more about how we can help you unleash the full potential of Jupyter Notebook.