Set permanent environment variables in Google Colab

Viewed 2434

How do you set environment variables in Colab that persist after a runtime restart?

I have tried:

  1. Changing ~/.bashrc
!echo "LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/opt/ros/melodic/lib" >> /etc/environment

  1. Adding line to /etc/environment
!echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc

Questions that don't have the answer:

Setting environment variables in Google Colab : All methods listed here do not persist after runtime restart

1 Answers

You can make it permanent, even after restart.

Just write a startup file. It will be run everytime jupyter restart. (but not if factory reset)

%%file /root/.ipython/profile_default/startup/startup.py
import os
os.environ['LD_LIBRARY_PATH']='/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/opt/ros/melodic/lib'
Related