Visual Studio Code for programming a Raspberry Pi
Visual Cues
Professional and casual Python developers save time and keystrokes in a Visual Studio Code development environment for remote coding of a Raspberry Pi.
Writing software code is heavy work, and professional code writers leverage software tools to create an effective environment that allows developers to work on code without affecting the users or breaking anything in the live environment.
Development environments are typically set up on a local machine or server that reflects the target production environment and contains the code being developed or modified. An integrated development environment (IDE) is a software application programming tool, deployed in a development environment to assist developers in maximizing their productivity and efficiency.
The IDE needs to be compatible with the programming language of interest and typically contains source code editors, a debugger, a compiler, and designers that can all be accessed through a single interface. Combining all of the development tools into one software suite helps the coder work on multiple tasks through one interface.
The repetitive commands used to write, run, and change code can be optimized in an IDE. Microsoft Visual Studio Code (VS Code) [1] is a tool for professional code developers (but see the "IDEs for All" box) that is usually used on standard Windows, Linux, and macOS machines; however, it can also be deployed to help make the life of a Raspberry Pi programmer easier.
IDEs for All
If you don't consider yourself a professional coder, why invest in an IDE? Maybe you're a code resurrectionist, or Frankenstein programmer.
Resurrectionists were paid body snatchers who exhumed the bodies of the recently dead for anatomists in the United Kingdom during the 18th and 19th centuries. In Mary Shelley's classic horror novel Frankenstein; or, The Modern Prometheus, Dr. Frankenstein stitches together an assortment of stolen body parts and attempts to imbue life into his creation.
Likewise, code resurrectionists look through Python code to find pieces that they can bring back to life by combining them with other pieces of code to make something useful. The cycle is similar to the instructions "wash, rinse, and repeat" found on a shampoo bottle: Find code snippets, combine them, modify them, write code when desperate, deploy the mix, and test. An IDE is the perfect tool for this programming cycle.
Environmental Decisions
VS Code is a streamlined code editor with support for development operations like debugging, task running, and version control. Although the Raspberry Pi has developed through the years into a fairly powerful single-board computer, it is probably not appropriate to run a full development environment and run the Python application being developed all on the same Rasp Pi
VS Code is an editor first and foremost. Instead of installing it on a Raspberry Pi with a keyboard, mouse, and video screen attached [2], you can deploy the VS Code tools for remote debugging. For this exercise, I installed VS Code on a workstation running Ubuntu 20.04; then, VS Code installed its headless server component designed for the ARM7 processor on the Raspberry Pi (Figure 1). This installation setup allows you to write and modify code from a local workstation and run and test the code on a Raspberry Pi (see the "Materials" box).
Materials
- Workstation: 64-bit Ubuntu 20.04.1 LTS
- Visual Studio Code: version 1.47.3
- Pi3B+, 8GB SD card
- Raspberry Pi OS (previously called Raspbian) 2020-05-27-raspios-buster-lite
Preparation
The first exercise in creating a development environment with VS Code is to establish an SSH connection from the Ubuntu workstation to the Raspberry Pi and configure it so that no password is required.
To generate a public and private public key infrastructure (PKI) key pair and transfer the public key to the Raspberry Pi host (Figure 2), issue the following commands from a command-line interface (CLI) on the workstation:
ssh-keygen <optional passphrase> ssh-copy-id -i ~/.ssh/id_rsa.pub pi@<Pi IP Address>
Being security conscious, best practice is to include a passphrase when prompted. You should retain the file defaults, unless you have the knowledge to customize the configuration.
The command
ssh-copy-id -i ~/.ssh/<mykey> <user>@<host>
logs in to the Raspberry Pi, copies the public key file, and configures the key in the authorized_keys
file to grant access. The copying may ask for a password or other authentication to connect with the Pi.
After the SSH configuration, the first time you log in requires you to supply a password. After that, SSH connections should be made without password prompting. You will know you are successful when you enter
ssh pi@<Pi IP Address>
and it drops you into a remote prompt without the need for a password.
Now that you have a Raspberry Pi SSH connection working without password prompting, you need to tailor the Pi for a development environment. Some additional software needs to be added so that it is prepared to support the changes that VS Code is going to make to establish a remote connection to the Pi. The following commands entered from the CLI on the Raspberry Pi adds the needed software:
sudo apt update sudo apt upgrade sudo apt install git python-pip python3-pip
The update
and upgrade
commands should always precede any Raspberry Pi software install requests. The commands refresh the environment and prepare it for the new installs.
The apt install
command loads the Git distributed version control system and pip
, the package manager for Python packages for the Python 2 and 3 environments. Packages contain the files needed for a module, which is a Python code library. If VS Code wants to install Python software, it is going to call pip
to make it happen.
Host Workstation Installation
On the Ubuntu workstation, you should install VS Code from the Ubuntu Software Center. For Ubuntu 20.04, the Software Center will list two versions of VS Code: Visual Studio Code and Visual Studio Code Insiders. The description provided in the window for both are identical, so what is the difference?
Visual Studio Code is the stable version, whereas Visual Studio Code Insiders is the daily release with the most recent code pushes. The Insider builds have new features, bug fixes, and other recently closed issues. If you like working on the edge of software development, consider a side-by-side install (i.e., Insiders installed next to the stable build), so you can use either version independently.
If using the latest releases of VS Code appeals to you, you might want to consider removing the Remote-SSH extension and replacing it with the Remote-SSH (Nightly) extension. The Remote-SSH extension lets you use any remote machine with an SSH server as your development environment. The nightly build version of the extension provides the developer early feedback and testing. This extension works best with VS Code Insiders. You must uninstall the stable version of the extension before using the nightly version. Be aware that bleeding edge builds may lead to the occasional broken build.
After firing up VS Code, you will see a welcome screen. Typically, I would start by exploring all the options of the VS Code interface, but I will not use that approach for the moment and pick it up later when VS Code is connected to the Raspberry Pi. For now, I will consider how to configure the application environment for Python.
Some changes are needed in the VS Code installation before proceeding. To begin, at the bottom of the left-hand vertical Activity Bar, select Extensions (the Tetris, or falling block, icon). Extensions add features to VS Code, and you can install those you need or uninstall those you don't require. Recall that the goal is to create a development environment for Python coding on the Raspberry Pi.
In the Extensions Search field, search for extensions that have "python" in the name, and click the Install button for ms-python.python
to install the Microsoft Python extension (Figure 3). For a development environment, you should add support for Python code formatting (discussed in greater detail later) by installing the Python-autopep8 (himanoa.python-autopep8
) extension.
Recall that you did some configuration work earlier on the Raspberry Pi to enable SSH, so you should add that feature to VS Code by installing the Remote-SSH (ms-vscode-remote-ssh
) extension.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.