Colab on steroids: free GPU instances with SSH access and Visual Studio Code Server | Towards Data Science

As most of you already know, Google Research offers free instances of Jupyter Notebook environment, with GPU and TPU, that runs entirely in the cloud. The platform is called Colaboratory (“Colab” for short) and it is a great resource when you want to perform quick experiments or prototype new machine learning models because no setup is required: all you need is a browser!
However, a simple Jupyter Notebook interface could be quite limiting when things get tough: we could need to have a SSH session! Let’s do it!
Sign up for free on ngrok and get an Authtoken from the authentication setting on the left.
Inside a notebook cell in Colab copy and paste the code below, insert your token from ngrok and run the cell.
# Install useful stuff! apt install --yes ssh screen nano htop ranger git > /dev/null# SSH setting! echo "root:carbonara" | chpasswd! echo "PasswordAuthentication yes" > /etc/ssh/sshd_config! echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config! service ssh restart > /dev/null# Download ngrok! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip! unzip -qq -n ngrok-stable-linux-amd64.zip# Run ngrokauthtoken = "PUT_YOUR_TOKEN_HERE"get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')! sleep 3# Get the address for SSHimport requestsfrom re import subr = requests.get('http://localhost:4040/api/tunnels')str_ssh = r.json()['tunnels'][0]['public_url']str_ssh = sub("tcp://", "", str_ssh)str_ssh = sub(":", " -p ", str_ssh)str_ssh = "ssh root@" + str_sshprint(str_ssh)
As output, you should receive something like (the address and the port could be different):
ssh root@0.tcp.ngrok.io -p 14407
Copy and paste it in your preferred terminal emulator, insert carbonara when a password is required and enjoy your SSH session! Of course, you can change the password with whatever you want!
Not used to Linux command line? Check this post: Basic Linux Console Commands Every Data Scientist Should Know.
SSH is fine but having a complete code editor directly in your browser is better! Let’s see now how to install Visual Studio Code in Colab instance.
Firstly, we need to mount our Google Drive.
# Mount Google Drive and make some folders for vscodefrom google.colab import drivedrive.mount('/googledrive')! mkdir -p /googledrive/My\ Drive/colabdrive! mkdir -p /googledrive/My\ Drive/colabdrive/root/.local/share/code-server! ln -s /googledrive/My\ Drive/colabdrive /! ln -s /googledrive/My\ Drive/colabdrive/root/.local/share/code-server /root/.local/share/
You will be prompted to authorize the access by inserting a token generated on a second page.
Now, let’s download, install and run the server version of Visual Studio Code.
! curl -fsSL https://code-server.dev/install.sh | sh > /dev/null! code-server --bind-addr 127.0.0.1:9999 --auth none &
Let’s start another SSH session with port forwarding (substitute the ngrok address and port with yours):
ssh -L 9999:localhost:9999 root@0.tcp.ngrok.io -p 14407
then open your browser at http://127.0.0.1:9999
Since we have mounted Google Drive before, settings, extensions of Visual Studio Code and project files (if saved under /colabdrive) will persist through sessions.
Enjoy Visual Studio Code in Colab!
https://gist.github.com/yashkumaratri/204755a85977586cebbb58dc971496da
https://stackoverflow.com/questions/48459804/how-can-i-ssh-to-google-colaboratory-vm
https://medium.com/@meet_patel/how-to-ssh-into-google-colab-and-run-scripts-from-terminal-instead-of-jupyter-notebook-3931f2674258
Contacts: LinkedIn | Twitter