Jupyter Notebook has revolutionized the way data scientists and machine learning engineers work. Its interactive nature, coupled with its support for various programming languages, makes it an invaluable tool for data exploration, analysis, and model development.
However, to truly harness the power of machine learning, you need robust computing resources. This is where Civo's GPU-powered compute instances come into play. By providing access to high-performance GPUs at affordable prices, Civo empowers you to accelerate your ML workflows and achieve faster results.
In this tutorial, we'll guide you through the process of setting up a Jupyter Notebook environment on a Civo GPU instance. We'll cover everything from creating the instance to configuring Jupyter Notebook and running your first machine learning model. By the end of this tutorial, you'll be equipped to leverage the full potential of Civo's GPU instances to supercharge your ML projects.
Preparing your SSH key pair
To create and connect to a GPU compute instance on Civo, you need an SSH key pair. SSH, or Secure Shell, is a protocol that allows for secure remote access and control of your instance. It ensures that the communication between your local machine and the instance is encrypted and authenticated.
The SSH key pair consists of:
- A public key, which will be added to your Civo account.
- A private key, which you will use to authenticate when connecting to your instance.
Steps to Generate an SSH Key Pair
- Open your terminal or command prompt and run the following command:
ssh-keygen
- Follow the prompts to create the key pair. By default:
- The public key will be saved with a “.pub” extension.
- The private key will be saved without an extension in the same directory.
Click here to find more information about setting up SHH key on Civo.
Adding the Public Key to Civo
- Log in to your Civo account and navigate to the Manage> SSH Keys.
- Select Add an SSH Key and paste the contents of your public key file (the one ending in “.pub”) into the provided field.
- Save the key.
Your SSH key pair is now prepared, and you can proceed to create your Civo GPU compute instance.
Setting up a GPU compute instance on Civo
In this section, we’ll walk you through creating a GPU-enabled compute instance on Civo.
Creating a Civo GPU compute Instance
- Navigate to the Compute section on the Civo dashboard and click Create Instance.
- Follow the on-screen configurations. Below are the key settings to focus on:
- GPU Type and Size: Choose the GPU type (H100, A100, L40S and H200 coming soon) and size that best suits your needs. Consider your budget and workload requirements when selecting a size. If you don’t see these GPUs options please check your Click here to find more information about account quota.
- Image selection: Choose an operating system image (Ubuntu, Debian, Rocky) that aligns with your project requirements.
- SSH Key selection: Choose the SSH key you just added to your Civo account.
- Public IP Address: Create a public IP address to enable connection to your instance via SSH.
- Firewall: Ensure the firewall settings permit SSH access. The "default-default" option works for this purpose.
- Launch Your Instance
- Click Create Instance to deploy your GPU instance.
- Once the instance is running, make note of:
- The public IP address for accessing the instance.
- The initial user (username) you selected during the setup process (e.g., “ubuntu” for Ubuntu images, “root”, or “civo”).
By completing these steps, you’ll have a GPU compute instance ready to use.
Connecting to the compute instance
Now that your Civo instance is up and running, it’s time to connect to it using SSH. This section will guide you through establishing a secure connection to your instance. To connect to your instance, you will need the following:
- Public IP Address: This is the address of your instance on the internet. You can find it on the page of your created instance.
- Private Key: The private key you generated earlier.
Connecting via SSH
Open your terminal or command prompt and enter the following command:
ssh -i /path/to/your/private/key username@public_ip_address
- Replace “/path/to/your/private/key” with the full path to your private key file.
- Replace “username” with the initial user you selected when creating the instance. You can retrieve it from the "View SSH Information" tab on the instance dashboard.
- Replace “public_ip_address” with the IP address of your instance.
By following these steps, you will successfully connect to your Civo GPU instance and start utilizing its powerful resources.
Installing necessary tools and packages
Before you start utilizing Jupyter Notebook on your Civo GPU instance, you'll need to ensure your system is up-to-date and equipped with the necessary software. Follow the steps below to install and set up the required tools:
Using pip
- Update the System
Update your system packages to ensure compatibility with the latest versions of software:sudo apt update && sudo apt upgrade -y
- Install Python and Pip
Next, install Python and the package manager pip to facilitate package installations:
sudo apt install -y python3 python3-pip
- Install Jupyter Notebook
Finally, use pip to install Jupyter Notebook:
pip3 install notebook
Using Conda (Alternative Option)
If you encounter any issues with the “pip” method, you can use Conda as an alternative to install Jupyter Notebook. Conda is a widely used package manager, particularly popular in data science workflows for managing environments and dependencies.
- Install Conda
If Conda is not already installed, you’ll need to download and install Miniconda (a minimal Conda installer) using the following commands:wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh
- Activate Conda
Once installed, you need to activate “conda”. If you accept the default installation settings, Miniconda will be added to your shell configuration file automatically. To activate it, you might need to restart your shell or use the following command:source ~/miniconda3/bin/activate
- Create a new Conda environment (optional but recommended)
It's a good practice to create a separate environment for your projects:
conda create --name myenv python=3.8
- Activate the environment:
conda activate myenv
- Install required packages
Once the environment is activated, you can install packages like “jupyter”:
conda install jupyter
By following these steps, your Civo GPU instance will be fully prepared with the necessary tools for running Jupyter Notebook and leveraging GPU acceleration.
Launching and accessing your Jupyter Notebook
After installing Jupyter Notebook, the next step is to start the Jupyter server on your remote GPU instance and securely access it from your local machine. Follow these steps:
- Launch Jupyter Notebook on the Remote Instance
Run the following command to start the Jupyter Notebook server on your Civo GPU instance:
Once the server starts, you will see an output containing a URL with a token for accessing the notebook interface from your local machine. The URL will look similar to this: “http://localhost:8888/tree?token=your_token_here”.jupyter notebook --no-browser --port=8888
- Set Up SSH Tunnel for Secure Access
Next, you need to set up an SSH tunnel to securely forward the Jupyter Notebook server from your remote instance to your local machine. Open a new terminal on your local machine and run the following command:ssh -i /path/to/your/private/key -L 8888:localhost:8888 username@public_ip_address
- “-L 8888:localhost:8888”: This forwards port 8888 on your local machine to port 8888 on your remote instance, making the Jupyter Notebook accessible locally.
- “/path/to/your/private/key”: Replace this with the path to the private SSH key you generated earlier.
- “username”: Replace with the initial username you set during the instance creation.
- “public_ip_address”: Replace with the public IP address of your Civo GPU instance.
- Access Jupyter Notebook in Your Browser
Now that the SSH tunnel is set up, open a web browser on your local machine and navigate to the following URL:
http://localhost:8888/tree?token=your_token_here
You should now be able to access the Jupyter Notebook interface in your local browser, ready to start working with your Civo GPU instance and running notebooks. If you used Conda for installation, these same steps will work seamlessly as well.
Running a sample machine learning model
Now that your Jupyter Notebook is running on your Civo GPU instance, let's test the GPU and showcase its performance by running a simple machine learning model.
- Install PyTorch (if not already installed)
If you haven’t installed PyTorch yet, you can do so by running:pip3 install torch torchvision
- Verify GPU availability
Before running any model, it's essential to ensure that your GPU is available and properly set up. You can verify this by running the following command in a Jupyter Notebook cell:
This will return “True” if the GPU is available and ready for use. If it returns “False”, there may be an issue with the GPU drivers or setup.import torch torch.cuda.is_available()
- Test with a simple model
To demonstrate the GPU's capabilities, let's run a simple model that utilizes GPU acceleration. We'll use a small neural network model from PyTorch, a popular deep learning library.
- Run a simple model:
Copy and paste the following Python code into a Jupyter Notebook cell to run a sample neural network on the GPU:import torch import torch.nn as nn device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") class SimpleNN(nn.Module): def __init__(self): super(SimpleNN, self).__init__() self.fc1 = nn.Linear(28 * 28, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = torch.relu(self.fc1(x)) x = self.fc2(x) return x model = SimpleNN().to(device) print("Running the model 100000 times to allow GPU usage observation...") for _ in range(100000): input_data = torch.randn(1, 28 * 28).to(device) output_data = model(input_data) print("Completed 100000 iterations.")
- Monitor GPU usage:
While running the model, you can monitor GPU usage by running the following command on your Civo GPU instance:
This will show you detailed information about your GPU, including memory usage, temperature, and utilization.nvidia-smi
- Run a simple model:
What to expect?
- If everything is set up correctly, you should see the model output in your Jupyter Notebook.
- The “nvidia-smi” command will show activity on the GPU, confirming that the model is running on the GPU.
By running this simple model, you're not only testing the GPU availability but also demonstrating how Civo’s GPU instances can significantly accelerate machine learning tasks.
Best practices for saving and sharing work
When working with Jupyter Notebooks on Civo, saving your work effectively and sharing it for collaboration is key. One option is to save notebooks and data directly to your compute instance’s local storage. However, for a more reliable and scalable approach, consider attaching a Civo Volume. Volumes act as persistent storage, ensuring your data is retained even if the compute instance is restarted or deleted. You can mount the volume to a specific directory and save all your notebook files and datasets there for long-term use.
For sharing and collaborating, exporting notebooks in “.ipynb” format is ideal, as it preserves code, markdown, and outputs. Here’s how to export a notebook:
- Open the Jupyter Notebook you want to export.
- Click on File in the menu bar.
- Select Download to save the notebook as a “.ipynb” file by default
Once exported, you can then upload your notebooks to platforms like GitHub or share them with collaborators via email or cloud storage platforms like Google Drive:
Uploading to GitHub:
- Log in to your GitHub account and navigate to the repository where you want to upload the notebook (or create a new repository).
- Click on "Add file" and select "Upload files".
- Drag and drop your “.ipynb” file or use the file selector to choose it.
- Add a commit message (e.g., "Add notebook") and click "Commit changes".
Google Drive:
- Open Google Drive and click "New" > "File upload".
- Select the “.ipynb” file to upload.
- Once uploaded, right-click on the file, select "Share", and adjust sharing settings as needed.
- Share the link with your collaborators.
Alternatively, consider using version control systems such as Git for tracking changes and facilitating team collaboration effectively.
By integrating these practices, you ensure your work remains accessible, organized, and easy to share across different platforms and workflows.
Get $250 Free Credit to Kickstart Your Journey
Ready to accelerate your machine learning projects? Sign up for Civo today and receive $250 in free credit to explore our high-performance GPU instances. With flexible, affordable pricing and cutting-edge infrastructure, Civo is the perfect platform for your AI and data science needs.
👉 Claim Your $250 Free Credit Now and start building smarter, faster, and more efficiently!Takeaways
By completing this tutorial, you have laid the groundwork for leveraging Jupyter Notebook on Civo’s powerful GPU compute instances. Whether training deep learning models or analyzing large datasets, this environment provides the tools and performance needed to handle demanding machine learning workflows with ease.
Now that your environment is up and running, the possibilities are endless. Consider exploring advanced workflows like fine-tuning large language models, running distributed training across multiple GPUs, or integrating Jupyter with other tools like TensorBoard for deeper insights. Whether you're working on cutting-edge research or optimizing business processes, Civo’s flexible, high-performance platform has you covered.
We encourage you to experiment, push the limits, and adapt this setup to your specific use case. With the combination of Jupyter Notebook’s interactivity and Civo’s robust infrastructure, you’re well-equipped to tackle the challenges of modern data science and machine learning. Happy coding!
Further Reading
- Beginner’s Guide to Getting Started in Machine Learning: An introductory resource for those new to machine learning.
- How to Set Up GPU for TensorFlow on Civo: A comprehensive guide to configuring TensorFlow with GPU support on Civo’s platform.
- Deploy Your Own Private ChatGPT with Llama 3.2 and Civo GPU Clusters: A guide to creating a private AI assistant using Civo’s GPU clusters.
- Building a Retrieval-Augmented Generation (RAG) System on Civo Kubernetes: Detailed steps to build a retrieval-augmented generation system for advanced AI tasks