How to Edit a File on Heroku Dynos using Nano or Vim

Introduction

Heroku is a cloud platform offering server hosting and management, along with a rich ecosystem of integrated services such as PostgreSQL, Redis cache, Sentry, PaperTrail, MailTrap, NewRelic, etc.

Hosted on AWS behind the scene, Heroku is offering high-level control over your web infrastructure. It takes care of your server and application deployment, environments pipeline, continuous integration, and other Ops tasks (TLS, env variables, provisionning…)

Applications are deployed on stateless containers called dynos, which is great to quickly deploy images from one environment to the other, and to scale applications. It’s especially appreciated when you are using Docker as your development environment.

Résultat de recherche d'images pour "heroku pipeline"
Heroku Dashboard

Editing files on Heroku dyno

If you want to run a command or debug on a specific environment (let’s say production!), Heroku allows you to launch dynamic container (a Dyno) containing an image of your application code and environment. It’s totally stateless, so you can create as many Dynos as you want.

To launch it, you can use Heroku Dashboard Web UI, open targeted application/environment, and click on ‘Run console’:

You can also use Heroku CLI to get a terminal on such dyno:

> heroku run bash -a my-app
▸    heroku-cli: update available from 6.16.12-04b3626 to 6.99.0-ec9edad
Running bash on ⬢ my-app... up, run.4034 (Standard-1X)
~ $ 

As Heroku Dynos are designed to be stateless, there is no file editor installed on it. And, of course, there is no way to use apt-get or equivalent to get a file editor installed!

So if you want to modify a file, you only have two alternatives:

  • use ed, if you are a masochist ;
  • read the following paragraph!

Install your favorite editor on Heroku dynos

Lucky you! I’ve come with three ways to get a command line editor installed on Heroku.

Nano: Ninja version

Once logged in your dyno, run the following command:

mkdir /app/nano
curl https://github.com/Ehryk/heroku-nano/raw/master/heroku-nano-2.5.1/nano.tar.gz --location --silent | tar xz -C /app/nano
export PATH=$PATH:/app/nano

Done!

Vim: Plugin version

Use Heroku plugins! Please use, this simple but efficient heroku-vim plugin. From your local machine:

heroku plugins:install heroku-vim

And then:

heroku vim -a my-app

Depending on your Heroku configuration, you may face the following issue:

vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory

I haven’t figured out how to fix it. If it’s not working for you, pick another alternative!

Vim: Buildpack Version

Use Heroku Buildpacks! This one is great: heroku–buildpack-vim. Add it to your Heroku App configuration from your Heroku App Settings / Buildpack section.

From Heroku CLI, locally:

heroku buildpacks:add https://github.com/carloluis/heroku-buildpack-vim -a my-app

Or add it in your app.json file:

{
   "buildpacks": [
       {
           "url": "https://github.com/carloluis/heroku-buildpack-vim"
       }
   ]
}

Then, build and release your application.

Finally:

heroku run bash -a my-app

Hope it helps!