My efforts to install Ruby 2.3.6 on Gitpod

install Ruby 2.3.6 on Gitpod

Gitpod is the only IDE that’s integrating seamlessly with Github. However, by default, they only provide the latest Ruby 2.5 and 2.6. It is good to always provide Ruby’s latest version. It’s an ideal approach. Unfortunately, the web development project is not that ideal. One of my client’s project is still requiring Ruby 2.3.6 for various reasons I can’t describe here. So, how to install Ruby 2.3.6 on Gitpod IDE?

In short, it’s not easy – at least for me. I spend about two hours playing and make it works. It’s not easy but it’s fun. Seriously. 

Initialization

Add those two files into the Rails root folder:

  • .gitpod.yml
  • .gitpod.Dockerfile

Why I use Ubuntu 16.04 as the base image?

At the time I write this, the Gitpod default base image for Dockerfile (gitpod/workspace-full) is running Ubuntu 19.04. The problem is it’s no longer possible to install older rubies including 2.3.6 on this Ubuntu version. Here’s the reference on Stackoverflow.

If you insist on installing it, you will end up with such error: No binary rubies available for ubuntu/19.04/x86_64/ruby-2.3.6. 

Based on my trials and errors, the latest Ubuntu version which allows me to install Ruby 2.3.6 is Ubuntu 16.04.

Let’s continue

Once you have those YML and Dockerfile in place, then commit and push it to the Github. After that, you can fire up the Gitpod IDE from there. 

Install RVM

This is my favorite way to install ruby version. Here’s how I install RVM on Gitpod IDE:

Run:

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Wait until it finished.

Then, run:

curl -sSL https://get.rvm.io | bash

If everything goes smoothly, you’ll see something similar to this:

Before you install Ruby 2.3.6 on Gitpod, read this

Normally, you’ll continue with “rvm install 2.3.6”. But if you continue with that, you’ll see an error about permission denied. This because Gitpod IDE didn’t allow you to run any command with sudo. They said it’s for security reasons.

Why RVM is asking for sudo? Because RVM has a feature called autolibsIt will automatically install dependencies on your system. This is why it’s asking for sudo permission.

Instead of letting autolibs to automatically install any packages required, it will be helpful, in this case, to see the required packages instead of installing them. We can do it by running this command: rvm autolibs read-fail

After that, you’ll be able to run rvm install 2.3.6 without any errors. If there are any missing dependency packages, it will stop and tell you gracefully.

Add missing dependencies for RVM

To fix it, you’ll need to update the Dockerfile with these missing packages. Commit and push the change to Github (I did it directly on Github, honestly), then re-launch the Gitpod again. Sounds a bit complicated? Yes, but it’s just for the starters.

Once all of the missing dependencies are on the Dockerfile, you’ll never have to do this again. Ever. Then, your fellow developers in the future and your future self will say thank you, because all they have to do to get the Rails apps to get up and running is just firing up the Gitpod.

Why Gitpod? It sounds too complicated

Not really. Especially if you’re already familiar with how Docker works. It is even better if you know or have the list of all required packages/dependencies for your Rails project. 

Setting up the .gitpod.Dockerfile once, and re-use it forever. If you need to install new packages, just add it to the Dockerfile and re-launch the Gitpod. It’s simple.

Conclusions

In short, to install Ruby 2.3.6 on Gitpod:

  • Put up all required packages on .gitpod.Dockerfile, commit and push it to Github
  • Fire up the Gitpod from Github.
  • Open the terminal inside Gitpod
  • Install rvm
  • Set up rvm autolibs as explained above
  • Install Ruby 2.3.6: rvm install 2.3.6

Key takeaways

  • Gitpod restricted the usage of sudo for security reasons.
  • But you’ll be able to run sudo on Dockerfile.
  • Installing old rubies won’t work with the default Gitpod base image for Docker. You need older Ubuntu as the base image.

More updates

After making some changes here and there, here’s the Dockerfile I come up. It’s better because there is no need to install RVM manually as I explained above.

FROM ubuntu:16.04

RUN apt-get update

### base ###
RUN apt-get update && \
    apt-get install -yq build-essential sudo vim wget links curl rsync bc git git-core apt-transport-https libxml2 \
        libxml2-dev libcurl4-openssl-dev openssl sqlite3 libsqlite3-dev gawk libreadline6-dev libyaml-dev autoconf \
        libgdbm-dev libncurses5-dev automake zlib1g-dev libgmp-dev libssl-dev libmysqlclient-dev libpq-dev gnupg2 && \
    rm -rf /var/lib/apt/lists/*

### Gitpod user ###
# '-l': see https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
RUN useradd -l -u 33333 -G sudo -md /home/gitpod -s /bin/bash -p gitpod gitpod \
    # passwordless sudo for users in the 'sudo' group
    && sed -i.bkp -e 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' /etc/sudoers
ENV HOME=/home/gitpod
WORKDIR $HOME
# custom Bash prompt
RUN { echo && echo "PS1='\[\e]0;\u \w\a\]\[\033[01;32m\]\u\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \\\$ '" ; } >> .bashrc

### Gitpod user (2) ###
USER gitpod
# use sudo so that user does not get sudo usage info on (the first) login
RUN sudo echo "Running 'sudo' for Gitpod: success" && \
    # create .bashrc.d folder and source it in the bashrc
    mkdir /home/gitpod/.bashrc.d && \
    (echo; echo "for i in \$(ls \$HOME/.bashrc.d/*); do source \$i; done"; echo) >> /home/gitpod/.bashrc

## Install RVM - following gitpod way
RUN curl -sSL https://rvm.io/mpapis.asc | gpg --import - \
    && curl -sSL https://rvm.io/pkuczynski.asc | gpg --import - \
    && curl -fsSL https://get.rvm.io | bash -s stable \
    && bash -lc " \
        rvm requirements \
        && rvm install 2.3.6 \
        && rvm use 2.3.6 --default \
        && rvm rubygems current \
        && gem install bundler --no-document \
        && gem install solargraph --no-document" \
    && echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*' >> /home/gitpod/.bashrc.d/70-ruby
ENV GEM_HOME=/workspace/.rvm

Gitpod Cloud IDE: 30% off for first 3 months. CODE: KEVINHQ. Click here