Ubuntu

Don’t use ubuntu system package, aka ruby-dev
It will run into permission issues
If you already did, remove it by apt remove --purge ruby-dev && apt remove

Use RVM, which also manages the ruby version and allow switching between multiple ruby versions
RVM
RVM ubuntu specific installation

Follow the instructions

# This is a snapshot, please retrieve the latest official instructions if possible
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install rvm

sudo usermod -a -G rvm $USER

echo 'source "/etc/profile.d/rvm.sh"' >> ~/.bashrc

# It is said that reboot is compulsory
sudo reboot

# Replace the version with latest / desired version
rvm install ruby-3.3.5

# If error regarding mkdir failing due to permission, you need
# rvm fix-permissions system; rvm fix-permissions user