How I migrated my development environment to a Raspberry Pi
Tags: vscode, raspberrypi, node, ubuntu
Published: 2021-04-12 14:39:19

In my spare time I develop an open source strategy game called **Solaris**, [check it out](https://solaris.games).

One of the problems with software development is that you need to set up a development environment on every device that you code on, also there can be differences between dev and production which often lead to unforeseen issues.

An ideal solution would be to be able to develop on *any* device without having to go through a long setup process other than installing VS Code (even this is optional as you will see later) on my development machine and have all of the code and apps hosted on a **remote server**. In this case, a Raspberry Pi.

#### Introducing the Raspberry Pi
The [Raspberry Pi](https://www.raspberrypi.org/) is an awesome little device capable of a lot of different tasks, like most tinkerers I have a few lying around doing nothing.

[Solaris](https://solaris.games) runs on Ubuntu Server, the Pi is the perfect candidate to mimic production as it can run the same software in an almost identical environment.

### The Plan
The end result of this exercise will be set up the development environment to be **as close as possible** to what runs in production:
- Install everything required to run Solaris.
- Run the client and server applications in dev mode.
- Use **Visual Studio Code** with `ssh` to edit files and debug the applications.

#### Pi Installation and Setup
The setup for Ubuntu running on the Pi is super simple. Using the [Pi imager software](https://www.raspberrypi.org/blog/raspberry-pi-imager-imaging-utility/), I just chose **Ubuntu Server** and using the [advanced options](https://www.raspberrypi.org/blog/raspberry-pi-imager-update-to-v1-6/), set up the hostname, WiFi and SSH there and then. There was no need to even plug in a keyboard and monitor once this installation was complete, just insert the SD card, power the Pi and SSH in from another machine for the rest of the setup process.

#### Setting Up Node and MongoDB
Solaris requires **Node.js** and **MongoDB** to run, it was straightforward to get these running as they are very well supported and documented.
- [MongoDB installation](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/).
- [Node.js installation](https://github.com/nodesource/distributions/blob/master/README.md#debinstall).

The next step was to `git clone` the repository down and walk through the steps in the [README](https://github.com/mike-eason/solaris#readme):
- `git clone https://github.com/mike-eason/solaris.git`

#### Setting Up Apps and Services
The Node applications will be managed on the server by [PM2](https://pm2.keymetrics.io/). After installing `pm2` the setup is easy:
- API - `pm2 start ~/solaris/server/api/index.js --name solaris-api --watch`
- Server jobs - `pm2 start ~/solaris/server/jobs/index.js --name solaris-jobs --watch`
- Vue.js client - `pm2 start ~/solaris/client/node_modules/@vue/cli-service/bin/vue-cli-service.js --name solaris-client --node-args="serve"`

![pm2 status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05rvi2v5o7ui4yi4ddbf.PNG)

The above is where the magic happens. `pm2` will manage the 3 processes, automatically watch for changes and restart the apps when necessary. The app is now accessible via the local IP address `192.168.1.xxx:8080`, amazing!

![Solaris running on the Pi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlkor9qzlm6l6nlt5kfe.PNG)

#### Setting Up Visual Studio Code
Now the apps are running and watching for changes, I needed a way to actually edit files. I chose to use VS Code for this as it has an awesome [extension](https://code.visualstudio.com/docs/remote/ssh#:~:text=The%20Visual%20Studio%20Code%20Remote,anywhere%20on%20the%20remote%20filesystem.) for **remote development** via `ssh`.

#### Debugging
The only caveat with debugging is that it is *slightly* inconvenient to stop the `pm2` process and then launch a VS Code debugging session.

The best way to get around this is to enable the `--inspect` flag when launching the `pm2` process:

```
pm2 start solaris-jobs --node-args="--inspect=9230" --watch
```

Then using a VS Code configuration to attach to the process.

```
{
    "type": "node",
    "request": "attach",
    "name": "Attach to Jobs",
    "port": 9230
}
```

For more info on the `--inspect` flag, see [here](https://nodejs.org/en/docs/guides/debugging-getting-started/).

### Final Thoughts
With all of that setup I can now simply open VS Code on my development machine and get straight into coding without really having to think about getting set up first. The environment is remote so any changes are **persistent**, I can code on my desktop and then switch over to my laptop and continue what I was working on.

I could take this a step further and use something like [code-server](https://github.com/cdr/code-server) which will actually host VS Code itself on the Pi. Then all I need is a device with a browser and I'm good to go. Finally, I'll be able to code on my Samsung Smart Fridge!

*Let me know what you think in the comments or if you have any suggestions. Thanks for reading.*