Published on: May 6, 2024
11 min read
Learn how to automate the installation of VS Code on a VM running on a cloud provider and how to access it from your local laptop.
DevSecOps teams can sometimes find they need to run an instance of Visual Studio Code (VS Code) remotely for team members to share when they don't have enough local resources. However, installing, running, and using VS Code on a remote virtual machine (VM) via a cloud provider can be a complex process full of pitfalls and false starts. This tutorial covers how to automate the installation of VS Code on a VM running on a cloud provider.
This approach involves two separate GitLab projects, each with its own pipeline. The first one uses Terraform to instantiate a virtual machine in GCP running Linux Debian. The second one installs VS Code on the newly instantiated VM. Lastly, we provide a procedure on how to set up your local Mac laptop to connect and use the VS Code instance installed on the remote VM.
Here are the steps to create a Debian Linux distribution VM on GCP.
Note: This installation uses:
Before you create the first GitLab project, you need to create a service account in GCP and then generate and download a key. You will need this key so that your GitLab pipelines can communicate to GCP and the GitLab API.
Compute Network Admin
Compute Admin
Service Account User
Service Account Admin
Security Admin
Download the JSON file with the service account key you created in the previous step.
On your computer, encode the JSON file to base64
(replace /path/to/sa-key.json
to the path where your key is located):
base64 -i /path/to/sa-key.json | tr -d \\n
NOTE: Save the output of this command. You will use it later as the value for the BASE64_GOOGLE_CREDENTIALS
environment variable.
Next, you need to create and configure the first GitLab project.
Create a group in your GitLab workspace and name it gcpvmlinuxvscode
.
Inside your newly created group, clone the following project:
[email protected]:tech-marketing/sandbox/gcpvmlinuxvscode/gcpvmlnxsetup.git
Drill into your newly cloned project, gcpvmlnxsetup
, and set up the following CI/CD variables to configure it:
BASE64_GOOGLE_CREDENTIALS
to the base64
encoded JSON file you created in the previous section.TF_VAR_gcp_project
to your GCP project
ID.TF_VAR_gcp_region
to your GCP region
ID, e.g. us-east1, which is also its default value.TF_VAR_gcp_zone
to your GCP zone
ID, e.g. us-east1-d, which is also its default value.TF_VAR_machine_type
to the GCP machine type
ID, e.g. e2-standard-2, which is also its default value.TF_VAR_gcp_vmname
to the GCP vm name
you want to give the VM, e.g. my-test-vm, which is also its default value.Note: We have followed a minimalist approach to set up this VM. If you would like to customize the VM further, please refer to the Google Terraform provider and the Google Compute Instance Terraform provider documentation for additional resource options.
After configuring your project, manually trigger the provisioning of your VM as follows:
When the pipeline finishes successfully, you can see your new VM on GCP:
Important note: Only run the cleanup job when you no longer need the GCP VM and/or the VS Code that you installed in it.
A manual cleanup job is included in your pipeline by default. To remove all created resources:
destroy
job, select Play ({play}).Perform the steps in this section only after you have successfully finished the previous sections above. In this section, you will create the second GitLab project that will install VS Code and its dependencies on the running VM on GCP.
Note: This installation uses:
Note: Since you will be using the ssh
command multiple times on your laptop, we strongly suggest that you make a backup copy of your laptop local directory $HOME/.ssh
before continuing.
Next, you need to create and configure the second GitLab project.
Head over to your GitLab group gcpvmlinuxvscode
, which you created at the beginning of this post.
Inside group, gcpvmlinuxvscode
, clone the following project:
[email protected]:tech-marketing/sandbox/gcpvmlinuxvscode/vscvmsetup.git
Drill into your newly cloned project, vscvmsetup
and set up the following CI/CD variables to configure it:
BASE64_GOOGLE_CREDENTIALS
to the base64
encoded JSON file you created in project gcpvmlnxvsc
. You can copy this value from the variable with the same name in project gcpvmlnxvsc
.gcp_project
to your GCP project
ID.gcp_vmname
to your GCP region
ID, e.g. us-east1.gcp_zone
to your GCP zone
ID, e.g. us-east1-d.vm_pwd
to the password that you will use to ssh to the VM.gcp_vm_username
to the first portion (before the "@" sign) of the email associated to your GCP account, which should be your GitLab email.After configuring the second GitLab project, manually trigger the provisioning of VS Code and its dependencies to the GCP VM as follows:
On the left sidebar, select Build > Pipelines and click on the button Run Pipeline. On the next screen, click on the button Run pipeline.
The pipeline will:
xauth
on the virtual machine. This is needed for effective X11 communication between your local desktop and the VMgit
on the VMVisual Studio Code
on the VM.At this point, you can wait until the pipeline successfully completes. If you don't want to wait, you can continue to do the first step of the next section. However, you must ensure the pipeline has successfully completed before you can perform Step 2 of the next section.
Now that you have an instance of VS Code running on a Linux VM on GCP, you need to configure your Mac laptop to be able to act as a client to the remote VM. Follow these steps:
XQuartz
on your Mac. You can execute the following command on your Mac to install it:brew install xquartz
Or, you can follow the instructions from the following tutorial from the University of North Dakota.
After the pipeline for project vscvmsetup
successfully executes to completion (pipeline you manually executed in the previous section), you can connect to the remote VS Code as follows:
XQuartz
on your Mac (it should be located in your Applications folder). Its launching should open up an xterm
on your Mac. If it does not, then you can select Applications > Terminal from the XQuartz
top menu.xterm
, enter the following command:gcloud compute ssh --zone "[GCP zone]" "[name of your VM]" --project "[GCP project]" --ssh-flag="-Y"
Where:
[VM name]
is the name of the VM you created in project gcpvmlnxvsc
. Its value should be the same as the gcp_project
variable.[GCP zone]
is the zone where the VM is running. Its value should be the same as the gcp_vmname
variable.[GCP project]
is the name of your GCP project assigned name. Its value should be the same as the gcp_project
variable.Note: If you have not installed the Google Cloud CLI, please do so by following the Google documentation.
If you have not used SSH on your Mac before, you may not have a .ssh
in your HOME
directory. If this is the case, you will be asked if you would like to continue with the creation of this directory. Answer Y.
Next, you will be asked to enter the same password twice to generate a public/private key. Enter the same password you used when defining the variable vm_pwd
in the required configuration above.
Once the SSH key is done propagating, you will need to enter the password again two times to log in to the VM.
You should now be logged in to the VM.
The assumption here is that you already have a GitLab project that you would want to open from and work on the remote VS Code. To do this, you will need to clone your GitLab project from the VM. First, you will be using a personal access token (PAT) to clone your project.
pat-gcpvm
and ensure that it has the following scopes: read_repository
, write_repository
, read_registry
, write_registry
, and ai_features
xterm
where you are logged on to the remote VM, enter the following command:git clone https://[your GitLab username]:[personal_access_token]@gitlab.com/[GitLab project name].git
Where:
[your GitLab username]
is your GitLab handle.[personal_access_token]
is the PAT you created in the previous section.[GitLab project name]
is the name of the project that contains the GitLab Code Suggestions test cases.xterm
where you are logged in to the VM, enter the following command:code
Wait for a few seconds and Visual Studio Code will appear on your Mac screen.
That's it! You're ready to start working on your cloned GitLab project using the VS Code that you installed on a remote Linux-based VM.
While using the remotely installed VS Code from your local Mac, you may encounter a few issues. In this section, we provide guidance on how to mitigate them.
If, while running VS Code, you are having issues with your keyboard keys not being mapped correctly, e.g. letter e is backspace, letter r is tab, letter s is clear line, etc., do the following:
If while running VS Code, you get a message saying:
"Error loading webview: Error: Could not register service worker: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state."
xterm
window:killall code
You may need to execute this command two or three times in a row to kill all VS Code processes.
xterm
window:ps -ef | grep code
xterm
window:code
Here are some useful commands to run on the VM that can help you debug SSH issues:
sudo systemctl status ssh
journalctl -b -a -u ssh
sudo systemctl restart ssh.service
Or
sudo systemctl restart ssh
sudo -s
This article described how to:
As a result, you can basically use your laptop as a thin client that accesses a remote server, where all the work takes place.
The automation to get all these parts in place was done by GitLab. Sign up for a free 30-day GitLab Ultimate trial to get started today!