[gymnasium / mujoco] colaboratory で "HalfCheetah-v5" を動かそうとして FatalError: gladLoadGL error が発生したときの対策

# 発生したエラー

```python
observation space:  Box(-inf, inf, (17,), float64)
action space:  Box(-1.0, 1.0, (6,), float32)
/usr/local/lib/python3.11/dist-packages/glfw/__init__.py:917: GLFWError: (65550) b'X11: The DISPLAY environment variable is missing'
  warnings.warn(message, GLFWError)
/usr/local/lib/python3.11/dist-packages/glfw/__init__.py:917: GLFWError: (65537) b'The GLFW library is not initialized'
  warnings.warn(message, GLFWError)
---------------------------------------------------------------------------
FatalError                                Traceback (most recent call last)
<ipython-input-8-ef0876ab189d> in <cell line: 0>()
     46 print('action space: ', env.action_space)
     47 
---> 48 env.reset()
     49 done = False
     50 

11 frames
/usr/local/lib/python3.11/dist-packages/gymnasium/envs/mujoco/mujoco_rendering.py in __init__(self, model, data, width, height, max_geom, visual_options)
     65 
     66         # Keep in Mujoco Context
---> 67         self.con = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
     68 
     69         self._set_mujoco_buffer()

FatalError: gladLoadGL error
```


# 解決策

色々と試している内に動くようになった。
原因はわからないままだが、時間がないので、動いたコードだけメモしておく。

おそらく、不要なものが色々含まれていると思われる。

```python:colaboratory
# https://pytorch.org/rl/main/reference/generated/knowledge_base/MUJOCO_INSTALLATION.html
!apt -q update
# !apt -q install libglfw3 libglew2.1 libgl1-mesa-glx libosmesa6
!apt -q install libgl1-mesa-dev libglfw3
```

```python:colaboratory
# It is required for installing box2d
# !pip install -q swig==4.2.1

# !pip install -q gymnasium[mujoco]==1.0.0 pyvirtualdisplay
!pip install -q pyvirtualdisplay
!pip install -q --upgrade mujoco gymnasium[mujoco]
!pip install -q pybullet

!pip install -q ipython>=8.0.0 parapara-anime

%env MUJOCO_GL=egl
```

```python:colaboratory
# 必要なライブラリのインポート
import glob
import os
from base64 import b64encode

import gymnasium as gym

import pybullet_envs
import pybullet as p
from IPython.display import HTML

p.connect(p.DIRECT)


def play_mp4(dir):
    """
    保存したmp4をHTMLに埋め込み再生する関数．
    """
    path = glob.glob(os.path.join(dir, "*.mp4"))[0]
    mp4 = open(path, "rb").read()
    url = "data:video/mp4;base64," + b64encode(mp4).decode()
    return HTML(
        """<video width=400 controls><source src="%s" type="video/mp4"></video>""" % url
    )


# ------------------------------------------
# Record video sample
# https://gymnasium.farama.org/main/api/wrappers/misc_wrappers/#gymnasium.wrappers.RecordVideo
# ------------------------------------------
dir = "./tmp/monitor"
os.makedirs(dir, exist_ok=True)

trigger = lambda t: t % 10 == 0
env = gym.make("HalfCheetah-v5", render_mode="rgb_array")
env = gym.wrappers.RecordVideo(env, video_folder=dir, episode_trigger=trigger, disable_logger=True)

print('observation space: ', env.observation_space)
print('action space: ', env.action_space)

env.reset()
done = False

# 終了シグナル(done=True)が返ってくるまで，ランダムに環境を動かす．
while (not done):
    action = env.action_space.sample()
    state, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated

env.close()

del env
play_mp4()
```


# 関連記事

https://qiita.com/Mittag-tech/items/7d1cfb97c58547ccdfbf

https://gymnasium.farama.org/main/api/wrappers/misc_wrappers/#gymnasium.wrappers.RecordVideo