DEV Community

Cover image for Connect a VM with VS Code for Remote Development
Godfrey Nyarko
Godfrey Nyarko

Posted on

Connect a VM with VS Code for Remote Development

Have you ever thought about using VS Code while accessing your virtual machine? This blog covers how to do that. By connecting your virtual machine with VS Code, you can leverage the features of both.

Prerequisites:
You should have some knowledge of VS Code and virtual machines. The following are needed:

  • VS Code
  • Git
  • VirtualBox
  • Virtual machine
  • Vagrant

In this guide, an Ubuntu virtual machine is used. To set up your Ubuntu virtual machine, click here.

Getting Started:

Starting up the Virtual Machine:

  1. Launch your Git application.
  2. Type vagrant up to start the virtual machine.
  3. Type vagrant global-status to check if the virtual machine is running.
  4. Type vagrant ssh-config to get the configuration of the virtual box for setting up remote access in VS Code. Copy and save this configuration to your notepad.

Installing the Remote-SSH Extension in VS Code:

  1. Open VS Code and click on the Extensions tab.
  2. Search for Remote SSH in the extension search box and click Install.

Installing Remote SSH

Connecting to the Virtual Machine:

  1. Click on the Remote SSH tab.
  2. Click on the + sign in the SSH options panel.
  3. Enter a name of your choice in the new search box and press Enter.
  4. Select C:\Users\<your-username>\.ssh\config and press Enter.
  5. Select Open Config from the pop-up message in the lower right-hand corner.
  6. Copy and paste the configuration from your notepad into the new panel. (Remember to delete anything in the opened config file before pasting the configuration)
  7. Optionally, change the Hostname from the default to your preferred name.
  8. Close the panel containing the Host details. Configuring VM
  9. Click Refresh on the control panel of Remote SSH. Refresh Remote SSH Panel
  10. Click on > to find your new SSH platform. Accessing VM
  11. Select and click on the arrow sign on your new SSH platform to connect.
  12. Select Linux from the pop-up search bar (since Ubuntu is a Linux distribution). Opening VM
  13. Wait for the connection to be established.
  14. After connecting, click on the three dots in the upper bar of VS Code and select Terminal.
  15. Click on New Terminal to open the terminal in VS Code.

You will notice that your VS Code terminal can now access your virtual machine terminal. Remember to always vagrant up your virtual machine using Git before opening VS Code after a shutdown or restart of your laptop or PC.

Top comments (2)

Collapse
 
andersonpull profile image
Anderson Oliveira Bezerra

Cool, I'll try, I've always tried to use it on the iPad with vscode online but I always lost the connection when I took the focus out of vscode, I'm going to try this method, thank you very much and congratulations for the article.

Collapse
 
arabian619 profile image
Godfrey Nyarko

Sure, you should try it out. Thank you