top | item 30987770

Ask HN: Best practices for editing remote code locally?

105 points| E_Evan | 4 years ago | reply

Working on a remote codebase from a local computer accessible via ssh, what’s the best/easiest way to write, edit and test code?

178 comments

order
[+] JamesMcMinn|4 years ago|reply
VS Code + The Remote SSH extension is a remarkable solution to developing code (not just editing!) on a remote machine.

When Covid hit and my main work machine was still a desktop, I worked from my home using VS Code and the remote plugin. This lasted for 18 months, and the only time I ever had an issue was due to a power outage in the office. Data, code (everything, really) lived on my work desktop that was sat in an empty office while I was at home.

There's no latency, the terminal opens as if you were on the remote machine, and ports are automatically forwarded. It runs code, tests, etc. all on the remote machine.

If there's a better solution out there it's almost certainly down to IDE preference.

[+] evilotto|4 years ago|reply
I like vscode + remote ssh a lot, but one thing to be aware of is that the node server that it installs on the remote can be a bit memory hungry if you're on a small machine (i.e., low-end droplet or vps). One of the rsync or sftp remote adapters is much nicer to such environments.
[+] monkeybutton|4 years ago|reply
I use VS code with the same plugin for working remotely. I also use jupyter lab with port tunneling and there's no difference to what it was like working locally. My company issued computer is just a VM off in some cloud. Working like this has a oddly retro-futuristic feel to it. Like we've gone back to the days of mainframe development.
[+] lyrr|4 years ago|reply
Since you seem to know what you’re talking about, I’m looking for some advice: I work for a large and very well known engineering company and currently our incredibly terrible workflow looks something like this: Develop locally using VStudio -> transfer repo to network drive -> login to tester via three layers of Remote Desktop Connections -> run and debug code live on tester RDC The tester setup is virtually all GUI based. It’s also located on the other side of Asia which is the reason of logging in via RDC. Is there possibly a better way of making this workflow more contained in a terminal/single ide environment, without having to jump through the RDC loops?
[+] swah|4 years ago|reply
I recently registered for a free VPS from Oracle and could get a box in my country - then those setups start working well.

With my VPS from DigitalOcean the ping will be 170ms and then its no bueno.

[+] vsgzusnex|4 years ago|reply
I started with this and still prefer it from a functionality stand point but the one use case where it falls apart for me is when you have an internet connection with occasional lag spikes.

This could be a variety of scenarios like working remote on bad ISPs or cellular or a crappy VPN but all that matters is when it acts up it will cause lag, or dropped connections depending on severity. Too many interruptions while trying to focus.

For this reason I keep everything local and use an rsync wrapper over my build system that streams output back. The added delay for each command disrupts workflow less than it happening during editing.

[+] woopwoop|4 years ago|reply
Seconded. It is so seemless that it is literally possible to not notice which machine you are on if you are just firing up something new for quick exploration.
[+] rubicon33|4 years ago|reply
Could this be used to do firmware development on Pi? One thing I’ve always hated was having to either SSH and use vim, or login directly to the Pi and use an IDE directly on it.

I would love to use my main mac + VS Code to edit code and dispatch commands like compile / run

[+] blowfish721|4 years ago|reply
How does it handle disconnects? With two kids and lots of interruptions my laptop is going to sleep a lot if I don’t have it plugged in between sessions, does it reconnect everything automatically so you wouldn’t notice it much?
[+] atwebb|4 years ago|reply
Exactly what I was thinking, it really is crazy nice for development over SSH or within a container. The Live additions are neat for sharing as well though I've only dabbled in it.
[+] posharma|4 years ago|reply
I used this setup a lot myself and it worked great except that name completions, intellisense didn't work as expected.
[+] baq|4 years ago|reply
switched to vscode from neovim in tmux a couple years ago, which used to be my weapon of choice for more than a decade (vim before neovim for the purists). practically haven't looked back. it really is amazing.
[+] rhacker|4 years ago|reply
And you can run the debugger!
[+] jve|4 years ago|reply
Visual Studio Code Remote SSH? https://code.visualstudio.com/docs/remote/ssh it will however install VS Code Server on target machine. So, for permanent development it is good. Not great if you only want to change one config file in PROD environment.

> The Visual Studio Code Remote - SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code's feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.

Once connected, even terminal runs on remote host. Moreover you get to forward ports from remote host to local using VSCode UI.

[+] dezmou|4 years ago|reply
It also do a lot of other stuff, for instance, if you have a python linter that need the python binary to check code, then you can specify the python path of the remote machine and vs-code will use it for linting, work also with other langages like C.

Also you can use embeded terminal and type "code myfile.txt" and it will open the file.

If used with windows, it also automaticaly remap localhost to the machine if you are launching a local server with your terminal (can be annoying tho)

Basicaly it try to do everything to make you feel that you aren't on a remote ssh connexion and it work really well

[+] samwillis|4 years ago|reply
I have recently started using this for a project, I’m on a Mac and the legacy codebase only supports CentOS7… I have a VM setup on my machine and use VS Code to “remotely” develop on the VM. Works really well!
[+] captrb|4 years ago|reply
I'm experimenting with using this VSCode feature to edit code on a Spark master, with the code storage on an EFS volume. So far, this seems to be allowing me to have a local-feeling environment, with a well-factored codebase that I can reference from a Jupyter notebook (inside VSCode), that has high-bandwidth and low-latency access to our data repositories.

I can also choose to temporarily vertically scale the remote host if I want to run single-node operations (ie Pandas).

[+] rank0|4 years ago|reply
Don’t overthink it!

Keep it traditional baby all you need is vim and tmux.

The initial learning curve for these sorts of cli apps is a bit steep, but it’s a lifelong skill and easily learnable for any competent developer.

Writing code is 1000x more difficult than learning vim philosophy and memorizing some key bindings.

Since I do all my editing in terminal, I just have a portable environment in a git repo. It has an install script that soft links all my scripts and config files to the appropriate locations. My work environment comes with me everywhere!

[+] jitl|4 years ago|reply
- My preference is to run my IDE or editor on the remote, and only run a thin client (RDP > X11, mosh > ssh) on the local. Because I’m happy in nvim, I would just install nvim and my dot files on the remote and access it via mosh/tmux. But this is not for everyone; if you’re doing Java an IDE is a must. So, try RDP or X11 forwarding for those.

If you can’t install software on the remote (or you can’t install a GUI stack), read on:

- Most IDEs like Intellij can be configured to do all their execution on a remote over SSH. I worked this way using PyCharm for my student job at UC Berkeley in 2012 and it worked great.

- SSHFS is okay but not great because of slowness. It’s fine for editing but sucks for running interpreted code locally. So, try to do all execution on the remote.

- VS Code has handy support of editing on a remote without needing SSHFS

- if permitted, a two-way file sync solution like Unison or Mutagen gets you native file system performance on the local. We used Unison for local editing with remote execution for Airbnb’s Ruby codebase for several years (2015-2018+). But it’s more fiddly to work with than SSHFS.

[+] chrisshroba|4 years ago|reply
Can you elaborate on the situation? Personally I’d just keep a local copy of the code and then use git or rsync to sync the remote system to my local version whenever I’m ready to.

If it’s just small things like a quick config change or something, I’d personally just ssh in and modify with vim.

For testing, you probably shouldn’t be doing that on a production system, perhaps you could stick the code in a docker container and run the tests locally? If you’re familiar with docker, that shouldn’t take more than a couple hours to set up, and likely much less time than that if the project isn’t super complex.

Feel free to explain your use case further and I bet people could give more targeted advice!

[+] Y-bar|4 years ago|reply
If you are on a Mac, using Transmit (https://panic.com/transmit/) you can mount a SSH/SFTP connection as a local disk and edit there in your editor of choice.
[+] mekster|4 years ago|reply
Do people who work over sshfs never search for a string over all files? Or your editor wants to parse codes from all files to find dependencies etc? Save on upload of a local copy is far superior.
[+] paskozdilar|4 years ago|reply
Since I like my local development environment, I mostly use `sshfs`.

It mounts a remote directory onto a local directory and lets you access remote files as if they're local, with slight latency. Then you can use whatever tools you use locally to edit/compile/run the software.

[+] ufo|4 years ago|reply
I also use this setup. Works well for editing and reading the files. But be aware that some file-intensive operations don't work as well. It is better to grep or git in the remore server instead of in the local mount. Be careful if your shell prompt is configured to show git status, because that won't be instantaneous.
[+] mekster|4 years ago|reply
sshfs works for only 10 files or less project.
[+] aardvark179|4 years ago|reply
Emacs tramp can edit files over ssh very nicely, along with other things like editing files as another user through sudo.
[+] globular-toast|4 years ago|reply
Best practice is to write, test, build etc. locally using a version control system and deploy a tested bundle to the remote computer when ready.

Editing code directly on a remote server is something I used to do in the 90s before git and other tools made good practices much easier.

[+] datalopers|4 years ago|reply
Best practice changes rapidly, local dev environments have proven to be an endless pain in the ass. Apple M1 silicon is the final nail.

The next 10 years of “best practices” will return to dumb clients and dynamically provisioned dev environments. It’s simply so much easier.

[+] ArcMex|4 years ago|reply
Oh, I love your response. Thank you.

My workflow was as follows

- Remote into server and develop from there

- Git to different location, could even be same server

- Occasionally pull on local machine for redundant backup

This allowed me to

- Segregate builds for my testing and the business's UAT

- Allowed me to always have a stable build locally that I could look at from home

Before, I did develop on my local machine and push to a server. Some part of me still thinks that's the proper way to do it.

But I am always seeing what works best for the current situation and developing directly on my server served me well once upon a time.

[+] blunte|4 years ago|reply
I have recently built a dev server for myself using https://github.com/coder/code-server (VS Code in the browser, hosted on the remote server). With very little effort, you get an always-on dev environment with built in terminal so you don't even have to ssh in if you don't feel like it (if you're happy working within the editor's terminal window pane).

As a bonus, I also setup openvnc, cloudflared (DNS over HTTPS), and pihole. And tmux and mosh of course.

With this setup I can even do my work from an iPad (once you get the VPN setup correctly).

It's nice to be able to reboot my laptop without losing my place in my work.

[+] ChuckMcM|4 years ago|reply
That the answer isn't "well you just tunnel X11 and bring up your regular work flow." is interesting to me. Because for literally decades this was a completely solved problem.

That it is a question always makes me wonder what was in it for the purveyor's of OS software to make this so impossible to do.

[+] Steltek|4 years ago|reply
Achieving tunneled X applications is not hard, as you said it's a solved problem, but I have never found it worth the effort either.

1. VNC has a "stable session" that doesn't collapse when SSH closes. Even between Linux machines, I chose VNC when needing a permanent remote GUI.

2. Remote machines don't always have your preferred applications or the associated preferred configuration. Copying a light .vimrc was always easier than trying to export-import a heavier app.

3. Performance always sucked. Even between machines sitting next to each other.

4. I didn't edit files with a GUI app anyway. Didn't everyone just use Vim, Emacs, or Nano? Was remote GUI apps ever anyone's preferred method (X11 or not)?

[+] qbasic_forever|4 years ago|reply
These days services are typically small containers with just enough binaries and dependencies to run the code, so bringing up a full X11 server session and tunneling it locally isn't really feasible or smart.
[+] rank0|4 years ago|reply
MacOS appears to make this intentionally difficult. I've tried with stuff like xQuartz but its finicky and Apple is extremely hostile to non-apple interoperability.
[+] emacs28|4 years ago|reply
I use Emacs but don't enjoy the latency of using a remote Emacs session or locally using tramp. Recently my solution is to edit locally, then whenever I need to run the codebase on the remote machine I wrap the run command in a script with two rsync commands. First (using rsync) it will sync the remote codebase with any local changes. Then when the remote command completes, any relevant result files I want to analyze locally after are synced back to the local machine (using rsync). Rsync is very flexible and has lots of options such as ignore filters.
[+] ctur|4 years ago|reply
Check out mutagen (discussion on https://news.ycombinator.com/item?id=30957156) -- it basically does bidirectional, background, transparent rsync. It also does port forwarding if you need that. It looks quite solid for the use case you describe.
[+] michaelt|4 years ago|reply
I've seen some of my colleagues running 'Visual Studio Code remote' [1] which allowed them to edit source code on a remote machine with a native text editor; run the code and tests remotely with a local interactive debugger, and so on.

I haven't tried it myself, but they seemed to like it.

[1] https://code.visualstudio.com/docs/remote/ssh

[+] drewg123|4 years ago|reply
Old unix tools like vi or emacs and "make" were built when nearly all computers were remote, and work really well for this. Especially when combined with something like "screen" to preserve multiple sessions..
[+] yabones|4 years ago|reply
Ideally, just use a text editor on the remote machine such as Vim, Nano, or Emacs.

If you want a full IDE, or just your set of extensions & tools, you could install it there and use X11 forwarding like so:

    ssh -X me@box /usr/bin/code
I believe VSCode also has a remote mode, but I haven't used it.
[+] rvieira|4 years ago|reply
Couple of solutions (some already mentioned):

- SSH remote editing (e.g. ssh + remote Emacs/Vim)

- VS Code + Remote SSH

- Emacs TRAMP

- JetBrains Gateway

- mutagen? (I haven't played with it, I'm not sure if it's suitable for your case)

[+] maicro|4 years ago|reply
While there are plenty of other more powerful solutions suggested here, I prefer my fairly light weight option - WinSCP connecting to the remote server over SFTP, then opening the files I want to change in my preferred editor (Sublime Text). WinSCP handles uploading file changes to the remote server automatically; there might also be a way to trigger actions on upload, but that gets into specifics of how your codebase is setup / what you need to run to test changes. Bonuses of using WinSCP are drag-and-drop file transfers, and a pretty thorough site manager (/address book / credential storage / login manager, whatever you want to call it).
[+] hokumguru|4 years ago|reply
Nova (https://nova.app) has a remote file browser + terminal that I've found work surprisingly well, it has the advantage of being Mac native as well!
[+] giobox|4 years ago|reply
Nova is nice, but its selection of plugins and integrations (things like linters, code syntax highlighting for specific languages etc) is just vastly behind VSCode. Mac native is nice to have, but the feature set in VSCode is so hard for a small competitor like Panic to beat now, especially when Nova is relatively exspensive.

If you are purely doing web design work, I could maybe recommend Nova for its nice CSS/HTML tooling, but again you can recreate much of this with VSCode anyway. When you factor in Nova is 99 dollars this conversation becomes a lot more difficult when VSCode is such a strong free competitor.

Nova has been through several iterations/product resets, I owned it back when it was called "Coda" too. VSCode by comparison has been relatively stable and its ecosystem a lot healthier.

I'd actually recommend VSCode + another Panic Software app for remote file access on a Mac: Transmit. Transmit is mac native standalone remote filesystem access tool, its basically the tool they built into Nova as a standalone, and 45 bucks:

https://panic.com/transmit/

VSCode + Transmit is IMO the best in class tools on MacOS for this kind of remote access work, although you can easily substitute Transmit for a free tool like CyberDuck or similar too and keep software cost at zero.

[+] ArcMex|4 years ago|reply
Thank you for sharing this. I will give the 30-day trial a shot. I find VSCode just fits my needs on my boxes (Mac, Win, Linux) but I am always open to try stuff.
[+] jraph|4 years ago|reply
Kate is a fantastic code editor (if you don't need an IDE, though it gained support for LSP) and is able to edit files over SFTP (without having to mount anything with SSHFS, which would be less efficient anyway because Kate would not be aware it is editing a distant file this way). KDevelop can probably too, and it is an IDE. I'd try QtCreator too.

In the same vain, Gedit and its forks can do this to if that's your cup of tea.

or vim, emacs or nano through a traditional SSH session :-)

[+] mekster|4 years ago|reply
Does anyone seriously use nano for programming?

Also I'd probably get pissed for using vim over ssh session to program when sometimes the network could sporadically slows down my cursor movement.