Code in PyCharm in your PC and execute in a linux virtualbox

I need to develop a software that only executes in Linux and I program in Mac. Moreover, I want to execute it in isolation as the software can potentially destroy the OS and I will run it with root.  Let’s configure what we need to develop in our beloved machine A and run/debug in our virtualized machine B with PyCharm.

Requirements

  • Virtualbox 5.1+ with a Linux (Debian 9 in my case).
  • Updated PyCharm.
  • A python project in PyCharm.

The idea is to set a python remote interpreter in PyCharm that connects through SSH to our linux machine.

Configure the guest (Linux)

We want to setup SSH and allow connections with the root user –and install python.

  1. Enable the root user: sudo passwd root and set a password for the root user (source).
  2. Install ssh server: sudo apt install openssh-server -y
  3. Allow root to login through ssh, open with sudo the file /etc/ssh/sshd_config and change the following line:
    FROM:
    PermitRootLogin without-password
    TO:
    PermitRootLogin yes<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>
  4. Restart ssh: sudo systemctl restart ssh (source)
  5. Install python: sudo apt install python3-pip -y.
  6. Go to the settings page of the virtualbox guest, and then to Network -> Adapter1. If you have not edit it, it should be something like this:
  7. Click on Port Forwarding and you will see something like this, but empty, without Rule1. We want to add such rule. For that we click on the + top right button and fill only Host Port with 2222 and Guest Port with 22.

Configure PyCharm

In your project, go to Preferences –> Project Interpreter –> click on the cog of the top right  –> Add Remote and configure it as the following image:

This will allow you to run and debug 🙂

Now we need to upload the code and we want to configure PyCharm to auto upload it when we edit it.

Let’s go to Tools –> Deployment –> Add (+)  on the top-left corner. Add any name you want and select type SFTP. You want to do something like this:

And under Mappings something like this. What really matters is the Deployment path on server, as it is where is going to store the values; and to press User this server as default. This will make this configuration the one picked when we set auto-upload and in this path is where run/debug will try to find our code.

Press OK.

Finally, let’s go to Tools –> Deployment –> and check Automatic upload.

Now you are ready.

Troubleshooting and tips

If PyCharm does not find files or have connection issues it is a good idea to go to Deployment –> Browse remote host and go to the folder where your project, as you set before in Deployment path on server, contains the files.

Happy coding 🙂

  • Created 2018-01-25
  • Updated 2018-01-25
Posted in Software

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.