How do I Install CUDA on Ubuntu 18.04?

195,429

Solution 1

I have installed CUDA 9.1 on Ubuntu 18.04, and runs very well.

I modify THE default gcc and g++, and use .run files instead of .deb files.

  1. Install gcc-6, g++-6 (CUDA requires gcc-6 !)

  2. In /usr/bin run

    sudo rm gcc, gcc-ar, gcc-nm, gcc-ranlib g++ 
    sudo ln -s gcc-6 gcc 
    sudo ln -s gcc-ar-6 gcc-ar 
    sudo ln -s gcc-nm-6 gcc-nm 
    sudo ln -s gcc-ranlib-6 gcc-ranlib 
    sudo ln -s g++-6 g++
    
  3. Install CUDA using .run files. Installing the latest driver (if required, download NVIDIA-Linux-xxxxxxx.run from Nvidia) would be better.

That's all.

I've tried to install using .deb files, but it causes package conflicts so I've switched to using this way.

Enjoy it!!

Solution 2

In a terminal, type:


sudo add-apt-repository ppa:graphics-drivers/ppa

sudo apt update

sudo ubuntu-drivers autoinstall

reboot


sudo apt install nvidia-cuda-toolkit gcc-6

nvcc --version

I have a gtx970 graphics card and a fresh install of Ubuntu 18.04

This worked for me

Solution 3

Enable multiverse repository, install nvidia drivers and nvidia-cuda-toolkit and gcc6 (preferably using update-alternatives to easily switch versions):

  1. In software & updates, select the restricted and multiverse repositories
  2. In the Additional Drivers tab in software & updates select the NVIDIA proprietary driver (390 for CUDA 9)
  3. sudo apt update && sudo apt install nvidia-cuda-toolkit, or install it from the ubuntu software center.
  4. CUDA requires gcc6, use update-alternatives to maintain both gcc7 and gcc6 as explained here.

Alternatively you can follow Taylor's instructions:

  1. After installing the proprietary NVIDIA driver, download CUDA 9 installation from their site (get Ubuntu the 17.04 runfile version)
  2. make the downloaded file executable with sudo chmod +x
  3. run it with --override flag
  4. Accept the terms and conditions, say yes to installing with an unsupported configuration, and no to “Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?”. Make sure you don’t agree to install the new driver.
  5. See above about using gcc6

The second method has the downside that it's not as easy to upgrade or remove.

Solution 4

I doubt the answers above qualify, because they seem to leave the system without the whole NVidia driver subsystem. I can guess why Cuda does not pull in the drivers, although I would probably prefer to have it otherwise. I am also not sure which is the proper way to get the latest driver, but right now this seems to do it:

sudo apt-get install nvidia-driver-390

Solution 5

The CUDA toolkit finally released the 18.04 ubuntu support

Share:
195,429

Related videos on Youtube

Gabs
Author by

Gabs

~"If you are depressed, you are living in the past. If you are anxious, you are living in the future. If you are at peace, you are living in the present."~ - Lao Tzu

Updated on September 18, 2022

Comments

  • Gabs
    Gabs over 1 year

    Is there any tutorial to install CUDA on Ubuntu 18.04?

    The instructions on the Nvidia website for 17.04 and 16.04 do not work for 18.04.

    I get a message telling me to reboot then re-run the installer. However when I do this I get that same message again.

    • user3667089
      user3667089 about 6 years
      It worked for me. Which part of it doesn't work for you?
    • Gabs
      Gabs about 6 years
      It says that a reboot is need and then re-run the installer, but I reboot the computer and try to reinstall and all I get is the same message...
    • taper
      taper over 5 years
      Try follow the instruction here which uses the runfile to install the driver and the cuda toolkit.
  • Eliah Kagan
    Eliah Kagan about 6 years
    Although the link to the package page is useful, and there is enough information here that this isn't a "link-only answer" or anything, I do recommend that you edit this to explain how to do what you are recommending.
  • ubfan1
    ubfan1 about 6 years
    Right, if you don't install the Nvidia drivers from the Ubuntu repositories, you may have to reinstall them after every kernel update.
  • Mr.WorshipMe
    Mr.WorshipMe about 6 years
    I recommend using update-alternatives instead of replacing gcc, see askubuntu.com/questions/26498/choose-gcc-and-g-version for more details
  • Mr.WorshipMe
    Mr.WorshipMe about 6 years
    You probably also need nvidia-driver-390-dev
  • luboskrnac
    luboskrnac over 5 years
    This should be accepted answer
  • salotz
    salotz over 5 years
    Good answer, but in my situation the Ubuntu repo nvidia driver was the wrong sub-version! At the time of posting this the nvidia-396 package is version 396.37 which doesn't work for my GeForce GTX 950. So I had to install 396.18 manually! So be sure to check the driver is the one you really need.
  • Volkan Paksoy
    Volkan Paksoy over 5 years
    This mostly worked for me. Only problem was ubuntu-drivers was not found and had to fix it by running: sudo apt-get install ubuntu-drivers-common (found it here: askubuntu.com/a/361868/766963)
  • Alex Reinking
    Alex Reinking over 5 years
    This worked for me, though I had to switch back to nouveau from the UI before running autoinstall (it reported conflicts installing v396 while v390 was installed)
  • Luis
    Luis over 5 years
    This worked perfectly. However sudo apt upgrade broke everything again, be careful with upgrades!
  • MrMartin
    MrMartin about 5 years
    For me, the autoinstall command results in The following packages have unmet dependencies: nvidia-driver-415
  • loscuropresagio
    loscuropresagio about 5 years
    A part from some unmet dependencies in autoinstall, this worked fine for me. Should definitely be the accepted answer.
  • mitchus
    mitchus about 5 years
    worked perfectly on 18.04
  • bhnn
    bhnn about 5 years
    This worked perfectly for u18.04 and a nvidia 840M. I'm confused though, the question was "How do you install CUDA?": Is the above answer really everything from docs.nvidia.com/cuda/cuda-installation-guide-linux/… condensed into those 2 steps of update drivers and install cuda toolkit or is this just the preparation to make sure the official installation guide works properly?
  • dirac3000
    dirac3000 almost 5 years
    Note it installed CUDA9.1 for me with a gtx970.
  • Alex Chan
    Alex Chan almost 5 years
    this answer is awesome!
  • MJimitater
    MJimitater almost 4 years
    If I want to test this installation by running the CUDA samples, of course nvcc isn't found at /usr/local/cuda/bin/nvcc, sind apt-get installs it in /usr/bin/nvcc. Is a soft link sufficient? EDIT: Just tried it, created a softlink at usr/local/cuda/bin/nvcc (had to create directories first), and it worked!! Can't believe it actually
  • Eric Wiener
    Eric Wiener over 3 years
    I love you (also btw this installed CUDA9.1 for me)
  • Eric Wiener
    Eric Wiener over 3 years
    If you get the error, sudo: ubuntu-drivers: command not found, run sudo apt-get install ubuntu-drivers-common. If you then get the error E: Unable to correct problems, you have held broken packages., run sudo apt update && sudo apt-get upgrade -y
  • vidstige
    vidstige over 2 years
    This should be the accepted answer