众所周知,Nvidia不支持rt内核,莫名其妙在Apollo的issue上看到一个解决方案,特此记录
ubuntu-drivers devices
- 在nvidia官网下载.run驱动文件,建议就放在主目录下,例如
NVIDIA-Linux-x86_64-535.113.01.run
- 在主目录下(.run文件所在位置),新建脚本
sudo gedit install_nvidia.sh
#!/bin/bash
BUILD_BASE=`pwd`
NV_FILE="NVIDIA-Linux-x86_64-xxx.run" # 这里改成自己下载的.run文件名(之前安装过的)
#NV_URL="https://us.download.nvidia.cn/XFree86/Linux-x86_64/430.50/${NV_FILE}" # 之前已经下好了显卡驱动.run文件,就不用从网上下载了,直接注释掉,而且如果要下载的话,则慢
NEED_TO_COMPILE_NV_KO=1
function clean_env() {
[ -d ./${NV_DIR} ] && rm -rf ./${NV_DIR}
}
function check_env() {
# check if in rt kernel
uname -r | grep rt 1>/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Not in rt kernel, Please install apollo kernel and reboot machine first."
exit 2
fi
# check if nv ko already in kernel
if [ ! -f /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko ]
then
export NEED_TO_COMPILE_NV_KO=1
fi
}
function prepare_nv() {
## download nv install file from nvidia home page
#if [ ! -f ./${NV_FILE} ]
#then
# echo "Downloading ${NV_FILE} from nvidia website..."
# wget ${NV_URL} -O ${NV_FILE}
# if [ $? -ne 0 ]
# then
# echo "Downloading ${NV_FILE} failed, please check your network connection!"
# rm -rf ./${NV_FILE}
# exit 1
# fi
#fi
###########上面是下载驱动的代码,我们已经提前下载好了,就不需要这段代码了,直接注释掉############
# +x
chmod +x ./${NV_FILE}
echo "Extracting nvidia install run file..."
./${NV_FILE} -x 1>/dev/null 2>&1
NV_DIR="`echo ${NV_FILE} | awk -F '.run' '{print $1}'`"
NV_VERSION="`echo ${NV_FILE} | awk -F '-' '{print $4}' | awk -F '.run' '{print $1}'`"
export NV_DIR
export NV_VERSION
export NVIDIA_SOURCE="${NV_DIR}/kernel"
}
function install_lib() {
NV_LIB_OUTPUT_PATH="/usr/lib/x86_64-linux-gnu/"
NV_BIN_OUTPUT_PATH="/usr/bin/"
[ -f ./${NV_DIR}/libnvidia-ml.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-ml.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}
[ -f ./${NV_DIR}/libnvidia-fatbinaryloader.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-fatbinaryloader.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}
[ -f ./${NV_DIR}/libnvidia-ptxjitcompiler.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-ptxjitcompiler.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}
[ -f ./${NV_DIR}/libcuda.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libcuda.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}
[ -f ./${NV_DIR}/nvidia-modprobe ] && /bin/cp -f ./${NV_DIR}/nvidia-modprobe ${NV_BIN_OUTPUT_PATH}
[ -f ./${NV_DIR}/nvidia-smi ] && /bin/cp -f ./${NV_DIR}/nvidia-smi ${NV_BIN_OUTPUT_PATH}
chmod +x /usr/bin/nvidia*
chmod +s /usr/bin/nvidia-modprobe
# link for nvidia
/bin/rm -rf /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1 /usr/lib/x86_64-linux-gnu/libnvidia-ml.so
/bin/ln -s /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.${NV_VERSION} /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1
/bin/ln -s /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1 /usr/lib/x86_64-linux-gnu/libnvidia-ml.so
/bin/rm -rf /usr/lib/x86_64-linux-gnu/libcuda.so /usr/lib/x86_64-linux-gnu/libcuda.so.1
/bin/ln -s /usr/lib/x86_64-linux-gnu/libcuda.so.${NV_VERSION} /usr/lib/x86_64-linux-gnu/libcuda.so.1
/bin/ln -s /usr/lib/x86_64-linux-gnu/libcuda.so.1 /usr/lib/x86_64-linux-gnu/libcuda.so
# take effect
/sbin/ldconfig 1>/dev/null 2>&1
}
function build_nv() {
if [ ${NEED_TO_COMPILE_NV_KO} == 0 ]
then
return
fi
NVIDIA_MOD_REL_PATH='kernel/drivers/video'
NVIDIA_OUTPUT_PATH="/lib/modules/`uname -r`/${NVIDIA_MOD_REL_PATH}"
CPUNUM=`cat /proc/cpuinfo | grep processor | wc | awk -F " " '{print $1}'`
export IGNORE_PREEMPT_RT_PRESENCE=true
cd ${NVIDIA_SOURCE} && make -j ${CPUNUM} module
cd ${BUILD_BASE}
unset IGNORE_PREEMPT_RT_PRESENCE
mkdir -p ${NVIDIA_OUTPUT_PATH}
[ -f ${NVIDIA_SOURCE}/nvidia.ko ] && cp ${NVIDIA_SOURCE}/nvidia.ko ${NVIDIA_OUTPUT_PATH}
[ -f ${NVIDIA_SOURCE}/nvidia-modeset.ko ] && cp ${NVIDIA_SOURCE}/nvidia-modeset.ko ${NVIDIA_OUTPUT_PATH}
[ -f ${NVIDIA_SOURCE}/nvidia-drm.ko ] && cp ${NVIDIA_SOURCE}/nvidia-drm.ko ${NVIDIA_OUTPUT_PATH}
[ -f ${NVIDIA_SOURCE}/nvidia-uvm.ko ] && cp ${NVIDIA_SOURCE}/nvidia-uvm.ko ${NVIDIA_OUTPUT_PATH}
depmod -a
}
# check environment
check_env
# prepare for nvidia
prepare_nv
# build nvidia.ko
build_nv
# install user lib
install_lib
# clean environment
clean_env
echo "Done to install nvidia kernel driver and user libraries."
- 将上述脚本中第四行代码中的xxx更改为我的新 nvidia 驱动版本号 ,修改完毕,保存。
- 添加可执行权限:chmod +x ./install_nvidia.sh
- 运行脚本:sudo ./install_nvidia.sh
- 成功安装后 reboot
- 重启后输入nvidia-smi