Easily Install Jupyter Notebook on Ubuntu (Step by Step)

Published:20 May 2022 - 9 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Are you looking for a UI that eases up performing your data science and AI tasks? If so, then install Jupyter Notebook on Ubuntu (now JupyterLab) and explore the features that make JupyterLab stand out.

In this tutorial, you’ll learn how to install Jupyter Notebook on Ubuntu 20.04 server with secure access via SSL/TLS encryption.

Ready? Read on and start interactive computing with JupyterLab!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

  • An Ubuntu machine – This tutorial uses Ubuntu 20.04.
  • A non-root user with sudo privileges.
  • A registered domain name (your_domain.com) pointed at your server’s public IP address.
  • JupyterLab requires a minimum of 4GB RAM to function properly.

Setting up Anaconda Environments for JupyterLab

Before you install Jupyter Notebook on Ubuntu, you need to install Anaconda and configure the environments for JupyterLab. Why? Anaconda is a Python distribution that makes it easy to install, run, and update multiple versions of Python and data science tools like JupyterLab.

With Anaconda, you can create separate environments for each project you work on. This setup keeps your projects organized and prevents one project from breaking another when you upgrade or change dependencies.

To install Jupyter Notebook on Ubuntu, we need to set up Anaconda environments for JupyterLab:

1. SSH to your server, open your terminal and run the following apt update and apt install command.

This command updates your server’s package index and installs the dependencies required to add new repositories over HTTPS.

sudo apt update -y && sudo apt install curl gnupg2 ca-certificates lsb-release -y
Install Jupyter Notebook on Ubuntu  by Updating Server’s Package Index and Install the Required Dependencies
Install Jupyter Notebook on Ubuntu by Updating Server’s Package Index and Install the Required Dependencies

2. Open your web browser and navigate to the Anaconda downloads page. Scroll down to the bottom and choose the Linux installer for Python 3, as shown below.

Note down the installer’s name as you’ll need it in the following step, but don’t start the download. At this time, the latest Linux 64-bit installer is called Anaconda3-2021.11-Linux-x86_64.sh.

Selecting Anaconda Linux 64-bit Hashes
Selecting Anaconda Linux 64-bit Hashes

3. Now, run the below command to download the Anaconda installation Bash script and save it to the tmp directory. Be sure to replace the URL with the actual path to the latest version of Anaconda you noted in step three.

The Anaconda bootstrap script installer is the fastest way to get the latest Anaconda installer.

cd /tmp && curl https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh --output anaconda.sh
Downloading the Anaconda Installation Bash Script
Downloading the Anaconda Installation Bash Script

4. Run the sha256sum command below to generate the SHA-256 checksum of the downloaded file. Replace the path (/tmp/anaconda.sh) with the actual path on your server where you saved the installer.

This command verifies the data integrity of the installer with cryptographic hashes. Doing so ensures the downloaded file has not been tampered with and is exactly what was intended to be downloaded.

sha256sum /tmp/anaconda.sh

Note down the generated checksum to compare it with the sha256 checksum in the Anaconda package’s hashes (step six).

Verifying the Installer’s Integrity
Verifying the Installer’s Integrity

5. Next, navigate to the Anaconda hashes page on your browser.

Compare the generated checksum you noted in step five with the one listed in the sha256 checksum. If they match, you can proceed with installing Anaconda.

Comparing Hashes
Comparing Hashes

6. Execute the anaconda.sh script to install Anaconda on your server.

bash anaconda.sh

Type yes at the prompt to access the license terms agreement.

Accepting the Terms of License Agreement
Accepting the Terms of License Agreement

7. Press Enter to continue the installation.

Installing Anaconda
Installing Anaconda

8. Now, type yes and press Enter when prompted to initialize Anaconda. This part appends the Anaconda installation path to your .bashrc file. Doing so lets you access the .bashrc file from your user account without using the full path.

Initializing Anaconda3
Initializing Anaconda3

Once you have Anaconda installed, you will see the following output.

Verifying Successful Anaconda Installation
Verifying Successful Anaconda Installation

9. Run the command below to activate the default environment called base, a special environment used as the starting point for creating new environments. This environment comes with a set of some core system libraries and dependencies.

source ~/.bashrc

Your prompt will change to include the name of the currently activated environment in parentheses. In this case, it’s (base), as shown below.

That’s it! You have successfully installed and activated Anaconda environments on your machine.

Activating the base Environment
Activating the base Environment

Creating a Password for JupyterLab

Now that you’ve activated the base environment, your JupyterLab instance will be accessible over the internet. While this may be convenient, it also creates a security risk as your JupyterLab server could be accessed by anyone who knows the URL of your JupyterLab server.

To mitigate this risk, you’ll need to set a password for your JupyterLab server. But first, you’ll have to create a configuration file. This file will be used to store your password hash and other configuration options for your server.

1. Run the below jupyter command to create a configuration file in your home directory.

JupyterLab is the latest version of Jupyter Notebooks, but the command to create a password hash is the same for both versions.

jupyter notebook --generate-config

You can see in the output below that Jupyter created the configuration file (jupyter_notebook_config.py) in the /home/ata/.jupyter directory. This directory is the default location for Jupyter Notebook configuration files.

Creating a Configuration File
Creating a Configuration File

2. Next, run the following command to generate the password hash.

 jupyter notebook password

You’ll be prompted to enter and verify your password, as shown below. Ensure you provide a secure password and press Enter.

Generating the password hash
Generating the password hash

3. Finally, run the cat command below to print the configuration file’s contents.

cat /home/ata/.jupyter/jupyter_notebook_config.json

You should see a line that starts with “password” and its value, which is your password hash. Copy and save this hash to a safe location, as you will need it to log in to your JupyterLab server later.

Viewing the Hashed Password
Viewing the Hashed Password

Securing the JupyterLab Server With Let’s Encrypt SSL

You’ve now generated a password hash, but is it enough to secure your JupyterLab server? While a password is better than nothing, it’s still not as secure as an SSL/TLS. Fortunately, Let’s Encrypt is just around the corner to secure your server.

But before you can generate Let’s Encrypt SSL certificates, you first need to install the Certbot client. Certbot is an open-source tool that allows you to generate free Let’s Encrypt SSL certificates.

1. Run the following command to install certbot.

sudo apt install certbot -y
Installing Certbot
Installing Certbot

2. Run the following commands to allow access to ports :80 and :443, especially if your JupyterLab server is behind a firewall.

# Open port 80 for the initial verification process. 
sudo ufw allow 80
# Open port 443 so that Certbot can renew your certificates automatically.
sudo ufw allow 443
Allowing Traffic Access to Ports :80 and :443
Allowing Traffic Access to Ports :80 and :443

3. Finally, run the certbot command below to generate the SSL certificates (certonly) with the following command.

sudo certbot certonly --standalone

Provide your email address when prompted to receive email alerts about expiring certificates. Enter A to agree to the terms of service, and type Y and press Enter if you’re willing to share your email address with the Electronic Frontier Foundation.

Providing Email to Receive Alerts
Providing Email to Receive Alerts

Provide your domain name when prompted, and press Enter. You’ll see the following output if the SSL certificates are generated successfully.

Below, you can see your SSL certificate has been saved in /etc/letsencrypt/live/your_domain.com.

certificate has been saved
certificate has been saved

4. Lastly, run the ls command below to list the files in the /etc/letsencrypt/live/your_domain directory.

sudo ls -a /etc/letsencrypt/live/your_domain.com

The files shown below are your SSL/TLS key and certificate files. At this point, you can now configure JupyterLab to use these files for SSL/TLS encryption.

Listing Available Certificates
Listing Available Certificates

Configuring the JupyterLab Server

Now that you have all the pieces to configure your JupyterLab server, it’s time to put everything together.

1. In your text editor, open the JupyterLab configuration file (/home/ata/.jupyter/jupyter_notebook_config.py) in your text editor, and change the following lines.

2. Scroll down to the c.NotebookApp.certfile and c.NotebookApp.keyfile lines, as shown below.

Uncomment each line by removing the leading # character. Set each line’s value to your actual SSL/TLS certificate file (etc/letsencrypt/live/your_domain/fullchain.pem and /etc/letsencrypt/live/your_domain/privkey.pem).

These two lines tell JupyterLab to use your Let’s Encrypt SSL certificates for encryption and where to find them. As a result, your users can access your JupyterLab server using the https protocols.

Sets path to your certificates
Sets path to your certificates
Sets path to your certificates
Sets path to your certificates

3. Next, uncomment the c.NotebookApp.ip line and set the value to * to bind JupyterLab to listen to all IPv4 addresses instead of just the localhost address (127.0.0.1).

The default value for this setting binds JupyterLab to only the localhost address. So you change this line’s value if you want to allow access to your JupyterLab server from anywhere.

Allowing Access to All Requests
Allowing Access to All Requests

4. Uncomment the c.NotebookApp.open_browser line and set the value to False if your server does not have a GUI. If you don’t have a GUI on your server and try to open a browser tab automatically, JupyterLab will fail and generate an error.

Disabling Web Browser Autoload
Disabling Web Browser Autoload

5. Uncomment the c.NotebookApp.password line and set the value to the hashed version of your password. JupyterLab will use this password for authentication.

Setting the JupyterLab Password
Setting the JupyterLab Password

6. Finally, uncomment the c.NotebookApp.port line and set the value to 9000, the default port that JupyterLab uses. Save the changes to the configuration file, and close the text editor.

Changing the Listening Port

Starting the JupyterLab Server

You’re now able to install Jupyter Notebook on Ubuntu, but how do you know it’s working? You’ll need to allow JupyterLab to access the SSL certificates when the JupyterLab server starts. And to do so, you’ll need access to JupyterLab’s GUI.

1. First, run the chmod and chown commands below to give the ata user ownership of the Let’s Encrypt directory and files.

sudo chmod 750 -R /etc/letsencrypt
sudo chown ata:ata -R /etc/letsencrypt
Setting up the Correct Permissions
Setting up the Correct Permissions

2. Next, run the following command to start your JupyterLab server.

jupyter lab
Starting Your JupyterLab Server
Starting Your JupyterLab Server

Don’t close the terminal window because the server is still running in the foreground. If you accidentally close the window or your connection is interrupted, your JupyterLab server will shut down.

3. Open your web browser and navigate the https://your_domain:9000 URL to access your JupyterLab server’s login page.

Provide your password hash, and click the Log in button to access your JupyterLab server.

Logging in to JupyterLab Server
Logging in to JupyterLab Server

Once you enter the correct password, you will be logged into your JupyterLab server, as shown below.

JupyterLab provides a robust UI re-design of the classic Jupyter Notebook. The UI offers a more pleasing aesthetic, but it also comes with many features. These features are a great help for users to get their work done efficiently.

Accessing JupyterLab server
Accessing JupyterLab server

4. Now, click on the New drop-down button —> Python 3 to create a new Python 3 notebook document, as shown below.

Your browser redirects to a new page where you can start writing code in your notebook (step six).

Creating a new Python 3 Notebook Document
Creating a new Python 3 Notebook Document

5. Provide a name for your notebook (ATA Notebook) in the box and click on the Rename button.

Your new ATA Notebook will be saved in the directory where you started your JupyterLab server. In this case, the notebook is held in the ata user’s home directory.

Naming the new Notebook
Naming the new Notebook

6. Navigate to the JupyterLab dashboard, and you’ll see the new notebook listed, as shown below.

Click on the ATA notebook to open it and start writing code.

Viewing Available Notebooks
Viewing Available Notebooks

7. Next, copy and paste the following code into your notebook, and press the Shift+Enter keys to execute the code.

This block code calculates the value of two squared (2²) and prints the result to the output cell.

x = 2 # Sets x value to 2
y = x**2 # Sets y value to the result of two squared (2²)
print(y) # Prints the value of y

The output (4) will be displayed in the next cell, as shown below, which indicates your JupyterLab server is working correctly.

Testing your JupyterLab Server
Testing your JupyterLab Server

You can edit and re-execute the code cells to test out different values for x, then press CTRL+S to save the changes.

8. Lastly, return to the terminal window where your server is still running.

When you’re finished working with your JupyterLab server, press CTRL+C keys and enter Y to shut down the server.

Shutting Down the JupyterLab Server
Shutting Down the JupyterLab Server

Conclusion

In this tutorial, you’ve learned how to configure and install Jupyter Notebook on Ubuntu server. You’ve created a new Jupyter notebook document and wrote a sample code to test your JupyterLab is working.

At this point, you already have a fully functional JupyterLab server. But with this newfound knowledge, you can now develop data-driven applications and collaborate with other users on notebooks. Why not learn to analyze data, create charts and graphs, and much more?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!