Skip to content

Instantly share code, notes, and snippets.

@hiraksarkar
Last active March 13, 2021 16:40
Show Gist options
  • Save hiraksarkar/2af99db628c2528cc5362c21da9985cc to your computer and use it in GitHub Desktop.
Save hiraksarkar/2af99db628c2528cc5362c21da9985cc to your computer and use it in GitHub Desktop.
Quick intro to using colab virtual machine for quick GPU access

Note

I have changed only two directory names from the original post to be found here

Open account with ngrok.com

Get the autorization code after creating a free account with ngork. You will meet with a screen like following

Install stuff (in colab)

In you colab do the following. replace authorization code with what you would get in ngrok website. Change password with any password you want.

# Install useful stuff
! apt install --yes ssh screen nano htop ranger git > /dev/null
# SSH setting
! echo "root:password" | 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 ngrok
authtoken = `authorization code`

Create a tunnel with ngrok (in colab)

In case you error here, in most of the cases that's the port number you need to change

get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
! sleep 3
# Get the address for SSH
import requests
from re import sub
r = 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_ssh
print(str_ssh)

You will see something like the follwoing

ssh root@2.tcp.ngrok.io -p 11781

Mount google drive (if not mounted)

This step will need to you to log in with your google account in order to get an API key

from google.colab import drive
drive.mount('/googledrive')

Create softlink (in colab)

If you get error with the following step then you have to delete already existing directories in /root/.local/share/ of your ngrok server

! mkdir -p /googledrive/MyDrive/colabdrive
! mkdir -p /googledrive/MyDrive/colabdrive/root/.local/share/code-server
! ln -s /googledrive/MyDrive/colabdrive /
! ln -s /googledrive/MyDrive/colabdrive/root/.local/share/code-server /root/.local/share/

Start code-server for accessing vscode

Run the following in terminal of google colab (you have click on the terminal tab)

curl -fsSL https://code-server.dev/install.sh | sh > /dev/null
code-server --bind-addr 127.0.0.1:9999 --auth none &

Tunnel the port to your local machine to open vscode in your local browser

ssh -N -f -L localhost:9999:localhost:9999 root@2.tcp.ngrok.io -p 11781
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment