45

I know on GCP, we can set up a vscode server and connect to that. But what I'm after here, is to know whether it is possible to connect to the runtime instance on Google Colab (the free one ie: https://colab.research.google.com) from a locally run vscode. If I'm not mistaken, we can connect to any remote Jupyter kernel in vscode instead of creating a new instance locally and connecting to it.

So I want to know if it is possible to first create a Python 3 notebook and then from your local machine, fire up vscode, connect to the Colab runtime and code inside vscode ?

There is nothing local here, all files, notebooks, everything resides on Google Colab, it's just the coding and executing the code (debugging, etc) using vscode instead of Google's own editor.

Update

Thanks to the answer, I could successfully connect to Google Colab. However, when I exited the ssh and tried to log in again I faced this:

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
2048 SHA256:yxFwLslfRq7YZFWNIhAD8TfJdp6sTfFbR2CXOWcysOA root@7561da0610da (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:6Yo/7I9JPyYfKJYvtiVelNFHrIL7R1xaB09fDWbVYf4 root@7561da0610da (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:r1HvJi/Y9twPkXoayNA4cSF55eH4MdOETHhXNSiC4ok root@7561da0610da (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Copy authtoken from https://dashboard.ngrok.com/auth
··········
Root password: aCsRocquey6953P9tHhF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I restarted the Google Colab runtime, I even changed the ngrok token and tried again, no luck!

3
  • 1
    No, this is not supported.
    – Bob Smith
    Dec 28, 2019 at 5:10
  • 3
    Then would you kindly post that as an answer so we can call it a day!?
    – Hossein
    Dec 28, 2019 at 5:26
  • It's possible but not allowed like it's mentioned here.
    – yemy
    Jan 3 at 13:36

8 Answers 8

21

I've just found another method without using ssh.

# Install jupyterlab and ngrok
!pip install jupyterlab==2.2.9 pyngrok -q

# Run jupyterlab in background
!nohup jupyter lab --ip=0.0.0.0 &

# Make jupyterlab accessible via ngrok
from pyngrok import ngrok
print(ngrok.connect(8888))

It will then show a JupyterLab URL

http://f1fe6fb39df6.ngrok.io  # for example

You can click it to run JupyterLab now. Or use the URL with VSCode for remote Jupyter kernel.

You can also use my library to make it short.

!pip install kora -q
from kora import jupyter
jupyter.start(lab=True)
2
  • 2
    It turned out that on the end of Jan 2021 some you have to install jupyter-client==6.1.5 and jupyterlab==1.2.18 or another pair to meet all the dependencies and turn on the whole server
    – A.Ametov
    Jan 25, 2021 at 12:17
  • 1
    The latest non-3 version (2.2.9) still works, so I specify jupyterlab==2.2.9 @A.Ametov thanks for your suggestion.
    – korakot
    Jan 25, 2021 at 15:11
12

Yes, it is very possible. Just managed it today.

What you need to do is create an ssh connection with the google collab. Write this on a google collab jupyter Notebook:

import random, string
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))

#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
#Setup sshd
! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null
#Set root password
! echo root:$password | chpasswd
! mkdir -p /var/run/sshd
! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc
! echo "export LD_LIBRARY_PATH" >> /root/.bashrc

#Run sshd
get_ipython().system_raw('/usr/sbin/sshd -D &')

#Ask token
print("Copy authtoken from https://dashboard.ngrok.com/auth")
import getpass
authtoken = getpass.getpass()

#Create tunnel
get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
#Print root password
print("Root password: {}".format(password))
#Get public address
! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

Check your ngrok status to get your portnum(via the website)

After that you can connect to collab via ssh:

This is the terminal command:

$ ssh <user>@0.tcp.ngrok.io -p <portNum>

(it will ask you for the password generated by the above snipet) You should be able to connect now.

But if you want to use vscode, repeat the connection with ssh via the Remote SSH extension

sources:

remote ssh: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh connection: Connect to google collab with ssh from console from PC

9
  • Thanks a lot. going to test it and if anything happens I'll report back ;)
    – Hossein
    Dec 31, 2019 at 3:15
  • I could successfully connect for the first time, but when I exited and tried again I couldnt log in! I reset the ngrok token and tried again, but each time I get an error . I updated the question with error log. would be grateful if you could tell me whats going on
    – Hossein
    Jan 5, 2020 at 5:20
  • Did you change the Port in the vscode configuration file? Jan 5, 2020 at 22:29
  • 2
    I guess I did choose a different port when I attempted that! is that what its causing the error? How can I solve it now? Please have a look at this link . This is my vscode ssh configs. both of them are empty! I get a new error this time though! I get : Traceback (most recent call last): File "<string>", line 1, in <module> IndexError: list index out of range
    – Hossein
    Jan 7, 2020 at 3:09
  • 2
    I am able to connect without a problem. but I wonder the storage and machine that I am given and connected to through vscode is from colab? If so, is there anyway to make the files I create persisted? as it happens once the sesssion has timed out..everything is gone. FYI: I am using colab pro Apr 4, 2020 at 9:55
11

The current accepted answer didn't work for me. What worked for me was running the following in google collab:

import random, string
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(64))

# install pyngrok for opening a tunnel
!pip install pyngrok -q
#Setup sshd
! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null

#Set root password
! echo root:$password | chpasswd
! mkdir -p /var/run/sshd
! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc
! echo "export LD_LIBRARY_PATH" >> /root/.bashrc

#Run sshd
get_ipython().system_raw('/usr/sbin/sshd -D &')

#Ask token
print("Copy authtoken from https://dashboard.ngrok.com/auth")
import getpass
authtoken = getpass.getpass()

#Create tunnel
from pyngrok import ngrok
ngrok.set_auth_token(authtoken)
print(ngrok.connect(22, "tcp"))
!whoami
print("Password: {}".format(password))

The output looks something like (just the important part):

.
.
.
Copy authtoken from https://dashboard.ngrok.com/auth
··········
NgrokTunnel: "tcp://5.tcp.ngrok.io:12345" -> "localhost:22"
root
Password: <64-digit-password>

Then you can do ssh [email protected] -p 12345 and when asked, enter the <64-digit-password> and there you go!

1
  • This is the only solution that worked for me.
    – zuccinni
    Jan 27, 2022 at 10:58
8

There is a python package that was built for this, colab-ssh. Although, you have to manually open colab from and browser to create an instance because colab dosn't have API to do that yet.

4

I encountered the same issue with second login and I finally figured that out.

This issue is raised by no execution permission for the ./ngrok file. In my understand,when downloaded ngrok and unziped it at the first time, it gained the execute permission. So you were able to create a tunnel. But when the Colab runtime was restarted, I think it automatically recycled those permissions, because when I tried ./ngrok authtoken $authtoken ...., it gave me permission denied error.

Therefore the only thing need to be done is to reassign ./ngrok the execute permission, for example , run chmod 755 ./ngrok. And script will work.

4

Using colab notebooks as a ssh remote machine runs against Google's terms and can risk you being kicked off of colab entirely:

https://research.google.com/colaboratory/faq.html#limitations-and-restrictions

1
  • 1
    I wish the limitation was more elaborately worded: "using a remote desktop or SSH" (is disallowed). Can we be sure that the use case stated in this Question falls under this disallowance? (I am assuming you are correct, but best to check).
    – P A N
    Feb 5 at 14:41
2

If you exit a session, you can not reconnect, because I believe it changes the port. I tried it too, then it throws an error, saying it's not possible.

But, if you run through the setup again and go the the https://dashboard.ngrok.com/status, you will see, that the port changed. Then you will be able to use it as a new remote connection. This way, I managed it to connect again.

As far as I understand, once a session is terminated, Google will not keep your files.

2

I suppose you are trying to use google colab GPUs while utilizing the VSCode environment on your local machine, which is of course better for both editing the code and also debugging it.

Indeed there is an extension in VSCode named remote-ssh that let's you connect to any remote virtual server through SSH tunnel. So the problem is that colab doesn't give you any public address to let you connect. Therefore we need to use another software to do so.

I provided the sample code for this procedure in a gist: https://gist.github.com/alistvt/693899f088921ecc0dbe7d9b6f3bdd02

I hope it comes handy to all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.