What text editor is available in Heroku bash shell?

HerokuText Editor

Heroku Problem Overview


I'm trying to update httpd.conf in my Cedar-based Heroku app. I got to my Heroku bash with

 heroku run bash

and found the conf dir under apache. But when I try to open any editor vi, vim, or emacs, I can't find any of these programs. How do you edit conf files on Heroku?

Heroku Solutions


Solution 1 - Heroku

I recently turned the original gist into a heroku cli plugin.

Just install:

heroku plugins:install https://github.com/naaman/heroku-vim

And use:

heroku vim

The heroku vim command will drop you into a bash shell with vim installed on your $PATH. All you have to do is retrain your fingers to type heroku vim instead of heroku run bash.

Solution 2 - Heroku

If you don't want to mess around with plugins and just want a copy of nano in your one-off dyno, just run

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

This will download a copy of nano from this plugin and put it in your PATH.

Solution 3 - Heroku

there's ed if you're a masochist.

Solution 4 - Heroku

It looks like you can download and install vim for one session:

#!/usr/bin/env bash
curl https://s3.amazonaws.com/heroku-jvm-buildpack-vi/vim-7.3.tar.gz --output vim.tar.gz
mkdir vim && tar xzvf vim.tar.gz -C vim
export PATH=$PATH:/app/vim/bin

This idea was found here.

Solution 5 - Heroku

Even if you could edit the files with vi it probably wouldn't solve your problem because the file system is ephemeral. Meaning... If you edit a file via heroku run bash you aren't actually changing the file for other dynos. To change a file for all dynos you need to either change what you push in a Git repo or change the buildpack. More details:
https://devcenter.heroku.com/articles/oneoff-admin-ps#formation-dynos-vs-oneoff-dynos

Solution 6 - Heroku

The plugin provided by Naaman Newbold is no longer working with heroku-16 stack, so I made a new plugin out of this updated gist.

Install:

heroku plugins:install @jasonheecs/heroku-vim

And use:

heroku vim

Solution 7 - Heroku

In the comments on the Brian Takita's answer link, there is the more recent solution to get Vim working on the Heroku console:

https://gist.github.com/dvdbng/7375821b20f189c189ab1bd29392c98e

Just saved me a lot of time! :)

Solution 8 - Heroku

Debugging on Heroku

Prepare the dyno

After installing naaman/heroku-vim you can create a new ephemeral dyno via heroku vim. As pointed out correctly by other posts you won't be able to see your changes when viewing through the browser because changes won't be propagated, but... you can actually view the changes from inside the dyno itself.

I've only experimented with "browsing" via curl, but if you could get lynx on there, or better yet get an ssh tunnel -- could be really great.

Start the server

The web server won't be running when you instantiate heroku-vim so you'll need to do it yourself. In my example I'm running php:

~ $ cat Procfile
web: vendor/bin/heroku-php-apache2

You can start this command yourself!

~ $ vendor/bin/heroku-php-apache2 2>/dev/null &
[2] 845

It's now running in the background!

curl your website

Dynos start up on random ports. Luckily you know which one because it's the $PORT variable!

~ $ curl localhost:$PORT
Hello World!

Editing

Do your vim thing now, but when you save the file and curl again - you won't see the changes. I don't understand where it's cached, but it's cached. You have to kill the server and restart it.

Restarting the server

  1. Find the process id

~ $ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
u6897        3     1  0 05:34 ?        00:00:00 bash
u6897      582     3  0 05:53 ?        00:00:00 bash vendor/bin/heroku-php-apache2
u6897      652   582  0 05:53 ?        00:00:00 bash vendor/bin/heroku-php-apache2
u6897      653   582  0 05:53 ?        00:00:00 bash vendor/bin/heroku-php-apache2

Here 582 is the parent id -- use that.

    kill 582

2. Wait just 1 second, and then start the server again (you'll get a new process id!). Curling via the same command will now give you the updated page.

Solution 9 - Heroku

An urgent alternative to edit a file in Heroku:

  1. place a copy of it on some remote host. I like to use Gist
  2. edit the file on Gist and when finished get the raw URL to it
  3. wget the raw URL on your Heroku bash
  4. copy the fetched file to the path of original file

Solution 10 - Heroku

I wrote a complete article on How to Edit a File on Heroku Dynos using Nano or Vim, but basically:

  • You can use command line:

    mkdir vim && tar xzvf vim.tar.gz -C vim
    export PATH=$PATH:/app/vim/bin```
    
  • You can use Heroku Plugins: heroku-vim

  • You can use Heroku Buildpacks: heroku-buildpack-vip

Hope it helps!

Solution 11 - Heroku

If you would like to just view the contents of the file then:

  1. cd to the folder where the file is located e.g. $ cd folder
  2. run cat command + the filename e.g. $ cat filename.csv

Solution 12 - Heroku

There are now a number of buildpacks that include vim: https://elements.heroku.com/search/buildpacks?q=vim

You could add one of these to the Heroku app in question, using support buildpack support.

Solution 13 - Heroku

the alternative way if your server run php is to upload PHP File Manager, it single file and you can download it from

http://phpfm.sourceforge.net/

Solution 14 - Heroku

One can change files in a dyno and see the result without pushing to Heroku:

  1. Install heroku-buildpack-vim buildpack:

     $ heroku buildpacks:add \
     https://github.com/carloluis/heroku-buildpack-vim
    
  2. Ssh into a dyno:

     $ heroku ps:exec
    
  3. Create and run start.sh:

     #!/usr/bin/env bash
     set -eu
     export DATABASE_URL=...
     bin/rails s -p 4000
    
  4. Forward port 4000 (second console):

     $ heroku ps:forward
    
  5. Open localhost:4000 in your browser.

  6. Stop start.sh, change a file, start again, refresh the browser page.

Solution 15 - Heroku

I prefer Nano editor, you can use following buildpack... https://github.com/velizarn/heroku-buildpack-nano

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDave ThomasView Question on Stackoverflow
Solution 1 - HerokuNaaman NewboldView Answer on Stackoverflow
Solution 2 - HerokuJamesView Answer on Stackoverflow
Solution 3 - Herokujcomeau_ictxView Answer on Stackoverflow
Solution 4 - HerokuBrian TakitaView Answer on Stackoverflow
Solution 5 - HerokuJames WardView Answer on Stackoverflow
Solution 6 - HerokuJason HeeView Answer on Stackoverflow
Solution 7 - HerokuJaredView Answer on Stackoverflow
Solution 8 - HerokuMikhailView Answer on Stackoverflow
Solution 9 - HerokuVilson VieiraView Answer on Stackoverflow
Solution 10 - HerokuCharles BochetView Answer on Stackoverflow
Solution 11 - HerokuVadim MalakhovskiView Answer on Stackoverflow
Solution 12 - HerokuwodowView Answer on Stackoverflow
Solution 13 - HerokuewwinkView Answer on Stackoverflow
Solution 14 - Herokux-yuriView Answer on Stackoverflow
Solution 15 - HerokuVelizar NenovView Answer on Stackoverflow