This is a little experiment to run ubuntu linux on your Mac using multipass to develop SPAs using a vscode Remote-SSH development environment.

This is not the quickest way to setup a remote development environment but, it does offer an isolated “development environment” without depending on docker that you can fully control and easily reset (or create multiples of).

Install multipass

Download latest installer from github: https://github.com/canonical/multipass/releases

or install using brew:

brew install --cask multipass

Configure primary

The default instance is pretty limited, before launching the primary you can change the settings. You can have as many instances as you want.

multipass launch -c 2 -m 4G -d 8G -n primary

The primary will also automatically mount your Mac home folder into `~/Home on the virtual linux machine. For better isolation you can unmount it.

multipass unmount primary

Enable ssh access from Mac

Assuming you have already generated ssh keys on your mac and they are in the ~/.ssh folder:

On your mac, copy the contents from the id_rsa.pub file

cat ~/.ssh/id_rsa.pub

Select the contents of the file and copy into your clipboard

Login to the multipass instance:

multipass shell

Edit the ~/.ssh/authorized_keys file in the linux instance and paste the contents of the clipboard into it (each entry should be in one line):

vi ~/.ssh/authorized_keys

Configure vscode to access instance

Make sure you have the Remote-SSH extension installed in vscode:

Configure SSH

From vscode, open command pallet, type ssh, and select Remote-SSH: Add New Host…

Follow the prompts. The primary most likely has the 192.168.64.3 and you will end up with ssh config file with a section like this:

Host multipass
  HostName 192.168.64.3
  User ubuntu
  ForwardAgent yes

Connect to host

Open the command pallet again, select Remote-SSH: connect to host… and connect to your multipass host.

A new vscode window will be launched.

Check out code from github

Make sure to NOT CHECK OUT the code to any subfolder of the mounted ~/Home host folder – the accessing files from the multipass host will be very slow.

You can clone from guthub directly or if you want to clone using ssh make sure to first add the gihub.com to the list of known hosts in the multipass host.

open the terminal in your new vscode windows connected to the multipass host and type:

ssh -T git@github.com

If you get a permission denied error:

ubuntu@primary:~/.ssh$ ssh -T git@github.com
Warning: Permanently added the ECDSA host key for IP address '140.82.112.3' to the list of known hosts.
git@github.com: Permission denied (publickey).

You may need to add your default key to the ssh-forward agent (from the Mac host terminal):

ssh-add

In order so you can push your changes back, make sure to configure the git credentials in the multipass host (using the vscode terminal):

git config --global user.email "youremail@domain.com"
git config --global user.name "your full name"

Check out your code:

Install node/yarn to your linux instance

In order to develop SPAs using node:

Install nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Install node (LTS)

nvm install --lts

You may get an error because the new path is not set in the current terminal session. In this case, you can simply restart the terminal.

to enable the yarn shim, enable corepack

corepack enable

Troubleshooting

Access private github repos from the linux instance

Assuming you are using ssh to access your github repos and have your ssh public key configured.

Change the ssh configuration (~/.ssh/config) for your multipass agent to enable forwarding agent:

Host multipass
  HostName 192.168.64.3
  User ubuntu
  ForwardAgent yes

Access private node packages

On your linux instance, using multipass shell, create a file in your home folder (~/.npmrc).

Change file limits

The default limit for file watchers may not be enough for node to monitor source files for changes. If you get this error:

... ENOSPC: System limit for number of file watchers reached, watch ...

You can try to fix by changing it:

sudo sysctl -w fs.inotify.max_user_watches=524288

To make the change survive restarts

sudo su
echo "fs.inotify.max_user_watches=524288" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf

References


0 Comments

Leave a Reply

Avatar placeholder