Installing Nvidia’s CUDA 9.1 on Fedora 27

 

If you’re like me, you to want to make sure that you’re using an up-to-date version of your operating system. This is not a problem for the vast majority of software, but when it comes to CUDA, it seems that NVidia is always a bit behind the times and it seems that only end-of-life versions of Fedora are ever officially supported. This doesn’t prevent you from installing CUDA on non-supported versions of Fedora, it just means you have to jump through a few more hoops.

Below you’ll find how I installed CUDA 9.1 on my Lenovo Ideapad Y700 laptop with NVidia Optimus technology. In the near future I’ll update this post to include desktop instructions, though it seems like there shouldn’t be any major issues there.

Update: A few days ago, I finally updated my desktop to Fedora 27 and installed CUDA 9.1 there. The install went quite smoothly, and I was able to build all the samples by simple going into the main sample directory and typing ‘make’. On my laptop, this worked to a degree, but since I had to use bumblebee, it couldn’t locate an NVidia library for one of the samples. On my desktop, this was not an issue. Below, I’ve updated the text to be a bit more general.

  1. The first thing you need to do is install NVidia’s proprietary driver.
    1. For an Optimus laptop this means making use of Bumblebee. However, it turns out that CUDA 9.1 requires the 387.26 driver or later, which is a short-lived branch driver and not in the bumblebee managed repository. This means you’ll have to install the unmanaged repository and then manually download the driver yourself from here. Once you’ve downloaded the driver, run the following commands to enable the bumblebee repos
       sudo dnf -y --nogpgcheck install http://install.linux.ncsu.edu/pub/yum/itecs/public/bumblebee/fedora$(rpm -E %fedora)/noarch/bumblebee-release-1.2-1.noarch.rpm
      sudo dnf -y --nogpgcheck install http://install.linux.ncsu.edu/pub/yum/itecs/public/bumblebee-nonfree-unmanaged/fedora$(rpm -E %fedora)/noarch/bumblebee-nonfree-unmanaged-release-1.2-1.noarch.rpm

      Then, copy the downloaded driver to the appropriate directory as mentioned on the bumblebee page

      sudo cp Downloads/NVIDIA-Linux-x86_64-387.34.run /etc/sysconfig/nvidia/

      Finally, install bumblebee

      sudo dnf install bumblebee-nvidia bbswitch-dkms VirtualGL.x86_64 VirtualGL.i686 primus.x86_64 primus.i686 kernel-devel

      Once finished, reboot your system for the changes to take effect.

    2. On a desktop you can do the install in a few different ways. The best options would be to make use of either the RPMFusion or  negativo17 repositories. This way, the driver will stay up-to-date automatically and everything is managed by the package manager. The negativo17 site has instructions for installation, so simply follow as it says. They also have CUDA in their repository, but I have no experience installing it from there. For RPMFusion, once you’ve enabled the repos, the installation of the driver should, most likely just be
      sudo dnf install akmod-nvidia

      If you’re system is quirky like mine, the above repos may not work. In that case you can follow the instructions from “If Not True Then False” to install the driver downloaded directly from NVidia. As mentioned above, make sure you download version 387.26 or later, otherwise you’ll see an error about insufficient driver version when trying to run CUDA code.No matter the method you choose, make sure to reboot after installing and to verify that you are running the NVidia driver you can use the following commands:

      nvidia-installer --version

      and/or

      glxinfo | grep OpenGL

      The glxinfo output should give the vendor string as NVIDIA Corporation.

  2. Next, install some useful libraries
    sudo dnf install libGLU-devel libXi-devel libXmu-devel glut glut-devel
    sudo dnf groupinstall 'C Development Tools and Libraries'
  3. Go to the CUDA Downloads page and download the .run file for Fedora 25.
  4. Once downloaded change directories to Downloads and run
    sudo sh cuda_9.1.85_387.26_linux.run --override
  5. Press and hold enter to scroll through the EULA, then type accept.
  6. You will be informed that you are installing on an unsupported configuration, just type ‘y’ to install anyway.
  7. You will be asked to install the driver, make sure to answer ‘n’ for no to this question.
  8. Answer yes to installing the CUDA 9.1 toolkit. Then answer the remaining questions at your discretion, though I always choose to install the symbolic link and you’ll want to install the samples so that you can test the installation.
  9. Once done, navigate to /usr/local/cuda-9.1/include/crt and open host_config.h in your favorite text editor. CUDA will complain if your gcc version is above 6, and Fedora 27 uses 7.2 as of this writing. Change line 119 of host_config.h to something like
    #if __GNUC__ > 10

    to prevent that from being a problem.

  10. One last thing to do in order for CUDA to work with gcc 7.2/glibc 2.26 which some may not be comfortable with. There are problems with the floatn.h that cause compilation of CUDA code to fail. To fix this you can edit /usr/include/bits/floatn.h. First make a back-up copy of the file somewhere safe in case you want to undo the changes. Then insert the following just below the #endif on line 35:
    #if CUDART_VERSION
    #undef __HAVE_FLOAT128
    # define __HAVE_FLOAT128 0
    #endif

    For the first time since the install, glibc updated. This overwrote the change I made to this header file and I had to add these lines back. I’m planning on looking into what in CUDA is conflicting with the float128 stuff in glibc to see if there is another work-around that only modifies CUDA specific files.

  11. Now, we need to modify our .bash_profile to include the CUDA library locations. Here’s what mine looks like:
     # .bash_profile# Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi# User specific environment and startup programs
    
    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/cuda-9.1/bin
    LIBRARY_PATH=$LIBRARY_PATH:$HOME/lib
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-9.1/lib64:$HOME/lib
    CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:$HOME/NVIDIA_CUDA-9.1_Samples/common/inc:$HOME/include
    
    export PATH
    export LIBRARY_PATH
    export LD_LIBRARY_PATH
    export CPLUS_INCLUDE_PATH

    You may not need to have the $HOME/bin, $HOME/lib or $HOME/include in your profile. I include these as I use some custom libraries across a lot of the codes that I write, so having them in a centralized location makes using them in different projects much easier as I can simply link to them. Putting the executables in a the home directory bin folder also makes it so that I can run my codes from any directory without having to point to their location. Essentially, I do all that because I’m lazy.

  12. After that, navigate to the directory where you installed the samples. You can try simply typing make. If you are using bumblebee, some samples may not build because certain libraries cannot be found. I tried fixing this on my laptop by including the directories where those libraries exist in my .bash_profile, which caused the GUI desktop environment to fail to load, likely do to conflicts with the libraries needed for the Intel driver. The fix here would be to edit the specific make file for the affected codes to add the appropriate directory for the linker, however, I have yet to test this.

This is a big step forward from CUDA 8 in terms of what needs to be done so that things work. The changes to floatn.h are definitely an ugly hack, and something that I’d rather not have done, but it seems to be okay so far.

Post in the comments below if you were successful with the installation or not, and let us all know of any problems you run into. I’ll do my best to respond and help when I can.

14 thoughts on “Installing Nvidia’s CUDA 9.1 on Fedora 27”

  1. Hi,
    thanks for taking the time to write that up.
    Your little floatn.h hack saved my day.
    I was trying to avoid the gcc7 incompatibility by compiling gcc6 myself, and while that was a success the floatn.h problem occurred nonetheless. floatn.h belongs to glibc-headers, not gcc, according to “rpm -qf /usr/include/bits/floatn.h”

    In a later installment, I found out you don’t have to compile gcc6 yourself but can use a gcc6.4 version packaged as “cuda-gcc” in the fedora-nvidia repository by negativo17.

    Like

  2. Hello! First of all, thank you very much! Your tutorial is the most clarifying and easy to understand I found on the internet so far. I’m using Fedora 26 in my notebook with NVidia Optimus GPU, I’m not sure if this should work perfectly in Fedora 26 but I followed step-by-step and I think the installation was ok.
    I tried to run the ‘clock’ and ‘simplePrintf’ sample codes but I’m getting this error: CUDA error at ../../common/inc/helper_cuda.h:1160 code=35(cudaErrorInsufficientDriver) “cudaGetDeviceCount(&device_count)”.

    Does this mean my driver installation didn’t work properly?
    When I execute systemctl status bumblee-nvidia.service I get this message:
    “bumblebee-nvidia.service – Compiling NVidia Driver
    Loaded: loaded (/etc/systemd/system/bumblebee-nvidia.service; enabled; vendor
    Active: active (exited) since Sat 2017-12-30 11:43:03 -03; 18min ago
    Process: 1752 ExecStart=/usr/sbin/bumblebee-nvidia (code=exited, status=0/SUCC
    Main PID: 1752 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
    CGroup: /system.slice/bumblebee-nvidia.service”.
    So, do you know what should I do to get CUDA working?
    Thanks!

    Like

  3. Well… I’m just a newbie beginner trying to start with CUDA development. I didn’t know that we have to always use the optirun to run programs with the NVidia GPU, once I ran the Nsight Eclipse Edition with the optirun I tried to create a new Cuda Project and it recongnized my gpu! Then I ran some of the cuda sample codes with optirun and it worked!
    So I think that was the problem I was getting.
    Now I’ll look for some tutorials for Cuda and OpenCV with CUDA.
    Again, thank you very much for you tutorial step-by-step.

    Like

  4. Another point! After all installations I couldn’t find any floatn.h inside of the directory specified…
    I tried to use ‘locate floatn.h’ but it returns nothing.
    I’m trying to install OpenCV 3.4.0 with CUDA after the ‘make’ command, it is compilling I get the error:
    ‘Error generating
    /home/ed-dorneles/lib/opencv/opencv-3.4.0-master/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o’

    I have no idea if this have something to do with the floatn.h.
    I would appreciate any suggestion, thanks!

    Like

  5. I believe that floatn.h is part of glibc. If it’s not installed on your system that may be the problem. Check to see if you glibc, and if not install it, then try running your install again.

    Like

  6. Works for me! Thank you!

    I ended up swapping Step 3 with the rpm cuda toolkit installer from nvidia (running the bash script was presenting some problems) with no complications.

    Like

  7. I have Fedora 27, fully updated. I installed my Nvidia GeForce 460 GTX drivers (290.25) using the if !1 0 method, because that’s the way I’ve always did it (been using Fedora since 2010 I believe) and it always worked just fine. It’s the first time I attempt to install Cuda though, as I never needed it before.

    At step 4 (sh cuda…run) works fine until I get this:

    ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.1 functionality to work.

    Unless someone has a really good idea at how to fix this, I’m just giving up. I’ve been researching Cuda installation methods for days and this one appeared like the suitable one.

    Thanks for any help, and really great tutorial, very clear, good work!

    Like

    1. I believe that message is normal and shouldn’t stop CUDA from working as long as you do have the driver installed. Basically it’s just the install script telling you that it did not install the driver. Try to complete the remaining steps and then compile and run some of the samples.

      Like

      1. Thanks! I did what you said, and the samples run just fine. I just assumed the message meant there was something wrong, while it was just a regular normal warning.

        However I installed Cuda for use with Blender 2.79. Now that doesn’t work. Although Cuda 2.79 isn’t directly supported, everyone seems to say it works fine for them. I tried tricks for previous versions (which were more troublesome, if Google results are faithful) and none of them seem to be what causes my problem.

        I don’t assume you are familiar with that software, so I’m not really expecting help (though it would be appreciated if you are in fact familiar with it). I just wanted to thank you for you help, and let you know how this ended up.

        Like

  8. Negativo17’s fedora-nvidia repository (which is installed when you follow the directions at the post you linked to above) now includes `cuda-gcc` and `cuda-gcc-c++` packages, which are builds of gcc 6.4.0 intended for use when compiling CUDA code. After installing those packages in Fedora 27 or 28, you can build all of the examples in CUDA 9.2 (installed from Nvidia’s own Fedora 27 repo, which also works on F28) by going into the directory created by `cuda-install-samples-9.2.sh` and running `HOST_COMPILER=cuda-g++ make`. No header modifications or other nonsense needed.

    Like

Leave a comment