Skip to main content

Setting up Jupyter Notebook environment on GPU

This document covers setting up Jupyter Notebook using KakaoCloud's GPU.

Basic information

About this scenario

This document explains the process of setting up Jupyter Notebook in combination with KakaoCloud GPU service, which is useful for data analysis and visualization. Jupyter Notebook is a web-based open-source application created by Jupyter that serves as one of the integrated development environments (IDEs) for Python. It allows you to easily use code, explanations, formulas, and graphs, making it widely used in data analysis.

By setting up Jupyter Notebook in KakaoCloud's GPU environment, you can perform data processing and model training more quickly and improve the speed of research and development more efficiently.

Step-by-step process

Step 1. Set up NVIDIA GPU environment

Install NVIDIA GPU drivers and libraries on the KakaoCloud GPU instance. This is the basic setup required to configure the Jupyter Notebook environment.

  1. Refer to the Setting up NVIDIA GPU environment document to install the necessary drivers and libraries.

  2. Configure the inbound policy of the security group to allow access to port 8080 of the GPU instance host. This step is essential for remote access to Jupyter Notebook.

    1. Go to the KakaoCloud Console > VPC > Security Group, then click the [Manage inbound rules] button.

    2. In the Inbound policy tab, add the following inbound policy.

      ProtocolSource IPPort numberPolicy description (Optional)
      TCP{Your Public IP}/328080Jupyter Notebook
  3. Navigate to the directory where the private key file is located.

  4. Execute the following command to connect to the created instance.

    ssh ubuntu@${GPU_PUBLIC_IP} -i ${PRIVATE_KEY_FILE}
    # For convenience, access with root privileges.
    sudo -i
    ParameterTypeRequiredDescription
    GPU_PUBLIC_IPStringRequiredPublic IP address of the instance in the GPU environment
    PRIVATE_KEY_FILEStringRequiredPrivate key file
  5. Run the following command to verify the installation of the NVIDIA driver and environment.

    nvcc -V
    # nvcc: NVIDIA (R) Cuda compiler driver ...

Step 2. Set up Jupyter Notebook environment

  1. Install Python 3.8 and verify the installed version using the following command.

    # Install Python 3.8
    apt-get update -y
    apt-get install python3.8 -y
    python3 --version
  2. Use Anaconda to install and manage the Jupyter Notebook environment. First, download the Anaconda installation file.

    wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
  3. Run the downloaded file to install Anaconda on your VM.

    sh Anaconda3-2022.05-Linux-x86_64.sh
  4. Press Enter to continue with the installation.

    # In order to continue the installation process, please review the license
    # agreement.
    # Please, press ENTER to continue
    # >>> ## Press 'Enter' ##
  5. Agree to the EULA and other licenses to proceed with the Anaconda installation. After reviewing the screen content, press the q key to move to the next step.

    # ==================================================
    # End User License Agreement - Anaconda Distribution
    # ==================================================
    #
    # Copyright 2015-2022, Anaconda, Inc.
    # ...
    # --More--
    # >>> ## Press 'q' ##
  6. If you accept the EULA license, type yes.

    # Do you accept the license terms? [yes|no]
    # [no] >>> ## Type 'yes' ##
  7. Press Enter to start the installation.

    # - Press ENTER to confirm the location
    # - Press CTRL-C to abort the installation
    # - Or specify a different location below
    #
    # [/root/anaconda3] >>> ## Press 'Enter' ##
  8. Once the installation is complete, set the environment variables as shown below. Verify the installation by using the conda command.

    export PATH=~/anaconda3/bin:$PATH 

    # Verify installation
    conda help

Step 3. Install and run Jupyter Notebook

  1. Install Jupyter Notebook and generate the configuration file.

    conda install jupyter-notebook
    jupyter-notebook --generate-config
  2. Use the printf command to encrypt the password that will be used to access Jupyter Notebook.

    printf "from notebook.auth import security\nsecurity.passwd()\n" | ipython
    # Enter password: ${Enter your desired password}
    # Verify password: ${Re-enter your password}

    For example, if you set the password to "test," an encrypted password string will be displayed after Out[2]:. Copy and save this value separately.

    In [2]: Enter password: test
    Verify password: test
    Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$oBt2YGaflk+sL3Lq60urcg$4Vc/yVxAR1x43bzlEDNy/Viu+U0kfOBDXbnsIdquEeI'
  3. Edit the Jupyter Notebook configuration file to specify the access IP and port. This can be done using the sed command below.

    sed -i "s/$(cat ~/.jupyter/jupyter_notebook_config.py \
    | grep c.NotebookApp.ip)/c.NotebookApp.ip = '0.0.0.0'/g" \
    ~/.jupyter/jupyter_notebook_config.py

    sed -i "s/$(cat ~/.jupyter/jupyter_notebook_config.py \
    | grep "c.NotebookApp.port =")/c.NotebookApp.port = 8080/g" \
    ~/.jupyter/jupyter_notebook_config.py
  4. Use the previously copied and saved encrypted string to set the password in the Jupyter Notebook configuration file.

    sed -i "s|^c\.NotebookApp\.password =.*$|c.NotebookApp.password = '${ENCRYPTED_PW}'|g" ~/.jupyter/jupyter_notebook_config.py
    ParameterTypeRequiredDescription
    ENCRYPTED_PWStringRequiredEncrypted password for accessing Jupyter Notebook
    - The value following Out[2]
  5. Verify that the encrypted password was entered correctly.

    # Verify that the previously set string is displayed.
    cat ~/.jupyter/jupyter_notebook_config.py | grep "c.NotebookApp.password ="

    # Example result
    c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$oBt2YGaflk+sL3Lq60urcgs4Vc/yVxAR1x43bzlEDNy/Viu+U0kfOBDXbnsIdquEeI'
  6. After completing the configuration, run Jupyter Notebook on port 8080.

    mkdir -p ~/jupyter
    nohup jupyter notebook --allow-root > ~/jupyter/jupyter.out 2>&1 & echo $! > ~/jupyter/jupyter.pid

    # Check if jupyter notebook is running.
    sleep 3
    curl http://localhost:8080/login
  7. Install the necessary libraries.

    pip3 install numpy
    pip3 install pandas
    pip3 install matplotlib
    pip3 install seaborn
    pip3 install scikit-learn
    pip3 install tensorflow==2.5.0 --use-deprecated=legacy-resolver
    pip3 install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 \
    --extra-index-url https://download.pytorch.org/whl/cu113
  8. Download the example files.

    git clone -b jupyter-notebook https://github.com/kakaoenterprise/kc-handson-config.git
    mv ./kc-handson-config/hands-on-test.ipynb ./hands-on-test.ipynb

Step 4. Access to Jupyter Notebook

  1. Verify that the Jupyter Notebook process is running, then access it in your browser at ${GPU_PUBLIC_IP}:8080/login.

  2. Enter the password you set to log in.

    Access Confirmation

  3. After entering the password, you will be directed to the Jupyter Notebook working directory page. Access the downloaded handson-on-test.ipynb file.

    Jupyter Notebook Working Directory Page

  4. Run the example code to verify that it prints “Hello kakaocloud!”.

    Verify Jupyter Notebook Execution Result