Ubuntu18.04 Conda安装cuda 12.1

8 min read Oct 13, 2024
Ubuntu18.04 Conda安装cuda 12.1

Installing CUDA 12.1 on Ubuntu 18.04 with Conda

This article guides you through installing CUDA 12.1 on Ubuntu 18.04 using conda, a popular package manager for Python. This combination allows for seamless integration of CUDA within your conda environment, facilitating high-performance computing and deep learning projects.

Understanding the Importance of CUDA

CUDA, or Compute Unified Device Architecture, is a parallel computing platform and programming model created by NVIDIA. It enables you to utilize the processing power of your NVIDIA GPU for general-purpose computing tasks, significantly accelerating workloads in fields like:

  • Machine Learning & Deep Learning: Training and running large-scale deep learning models.
  • Scientific Computing: Accelerating simulations and data analysis.
  • Graphics Processing: Enhancing image and video processing performance.

Why Use Conda?

Conda is a versatile package manager known for its ability to manage both Python and non-Python packages in isolated environments. This allows you to:

  • Control Dependencies: Install and manage specific versions of CUDA and other libraries without conflicts.
  • Environment Isolation: Create distinct environments for different projects, ensuring consistency and preventing conflicts.
  • Simplified Installation: Install packages with a single command, streamlining the process.

Pre-Installation Steps

Before installing CUDA, ensure your system meets the following prerequisites:

  • NVIDIA GPU: Ensure your system has a compatible NVIDIA GPU. Check NVIDIA's website for supported cards and drivers.
  • Ubuntu 18.04: This guide assumes you are using Ubuntu 18.04.
  • Internet Connection: A stable internet connection is needed to download and install packages.

Installing NVIDIA Drivers

1. Disable the Nouveau Driver

The Nouveau driver is an open-source driver that may conflict with CUDA. Disable it by editing the /etc/modprobe.d/blacklist.conf file:

sudo nano /etc/modprobe.d/blacklist.conf

Add the following line to the file:

blacklist nouveau

2. Install NVIDIA Drivers

Download the latest NVIDIA drivers from the official website () matching your GPU and operating system.

After downloading, run the installation script:

sudo sh NVIDIA-Linux-x86_64-*.run

Replace NVIDIA-Linux-x86_64-*.run with the actual name of the downloaded file. Follow the on-screen instructions to complete the installation.

Creating a Conda Environment

1. Install Miniconda

Download the Miniconda installer from the official website (). Choose the appropriate version for your system (Linux 64-bit).

Once downloaded, run the installer:

bash Miniconda3-latest-Linux-x86_64.sh

Follow the on-screen instructions to complete the installation.

2. Create a New Conda Environment

Create a dedicated conda environment for your CUDA installation:

conda create -n cuda_env python=3.8

This command creates an environment named cuda_env with Python 3.8. You can choose any Python version compatible with CUDA 12.1.

3. Activate the Conda Environment

Activate the newly created environment:

conda activate cuda_env

Installing CUDA 12.1

1. Install the CUDA Package

Use conda to install the CUDA 12.1 package:

conda install -c nvidia cuda=12.1

This command installs the CUDA package from the NVIDIA channel.

2. Verify Installation

After the installation is complete, verify that CUDA is installed correctly:

nvcc --version

This command should display the installed CUDA version (e.g., 12.1).

Testing CUDA

1. Create a CUDA Test Program

Create a simple CUDA program named test.cu:

#include 

__global__ void add(int *a, int *b, int *c) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    c[i] = a[i] + b[i];
}

int main() {
    int a[] = {1, 2, 3, 4};
    int b[] = {5, 6, 7, 8};
    int c[4];

    int *dev_a, *dev_b, *dev_c;

    // Allocate memory on the device
    cudaMalloc(&dev_a, sizeof(a));
    cudaMalloc(&dev_b, sizeof(b));
    cudaMalloc(&dev_c, sizeof(c));

    // Copy data from host to device
    cudaMemcpy(dev_a, a, sizeof(a), cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, b, sizeof(b), cudaMemcpyHostToDevice);

    // Launch the kernel
    add<<<1, 4>>>(dev_a, dev_b, dev_c);

    // Copy data from device to host
    cudaMemcpy(c, dev_c, sizeof(c), cudaMemcpyDeviceToHost);

    // Print the result
    for (int i = 0; i < 4; i++) {
        printf("c[%d] = %d\n", i, c[i]);
    }

    // Free device memory
    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_c);

    return 0;
}

2. Compile and Run the Program

Compile the program using the nvcc compiler:

nvcc -o test test.cu

Run the compiled program:

./test

The output should display the sum of the two arrays a and b, demonstrating that CUDA is working correctly.

Common Issues and Solutions

  • Driver Version Mismatch: Ensure you are using the correct NVIDIA driver version for your GPU and CUDA version.
  • Permission Errors: Use sudo for commands that require administrator privileges, such as installing drivers and packages.
  • Environment Activation: Double-check that your conda environment is active before installing CUDA.
  • CUDA Path: Verify that the CUDA path is set correctly in your environment variables.

Conclusion

By following these steps, you can successfully install CUDA 12.1 on Ubuntu 18.04 using conda, leveraging the power of NVIDIA GPUs for your computational needs. Remember to verify the installation by running a simple CUDA program and refer to the official documentation for troubleshooting any potential issues. This installation allows you to harness the computational power of CUDA within a managed conda environment, simplifying your development workflow and unlocking the potential for accelerated computing.

Featured Posts