119

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?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Dave Thomas
  • 1,367
  • 2
  • 10
  • 17
  • Emacs can [edit files over SSH](http://www.gnu.org/software/emacs/manual/html_node/tramp/index.html) :p –  Oct 01 '12 at 02:20

15 Answers15

131

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.

Naaman Newbold
  • 3,324
  • 1
  • 21
  • 12
  • when you run heroky vim, I assume you startup a new dyno. Do you know of a way to edit your existing dyno? – dwenaus Sep 02 '14 at 23:01
  • 5
    @dwenaus Using vim in a dyno is just a troubleshooting tool. It's not possible to connect to a running dyno. Also, even if you could (you can with a [hack](https://github.com/nzoschke/SSHeroku)), the changes you make inside the dyno would not get persisted inside your slug. i.e. any changes you made would be lost when your app is restarted (every 24 hours). – Naaman Newbold Sep 03 '14 at 02:26
  • 4
    @dwenaus another troubleshooting tool you can use -- and I'd highly recommend using this on QA/staging apps, NOT PRODUCTION -- is [`hbuild`](https://github.com/naaman/hbuild). It lets you change your source code without git commits, so you can try out stuff on Heroku without mucking up your git history. – Naaman Newbold Sep 03 '14 at 02:28
  • exactly what I'm looking for. Once in a while there is a Heroku-specific issue and the only way to troubleshoot is via too many commits and deploys - a horribly slow way to debug. thanks! – dwenaus Sep 03 '14 at 20:27
  • Awesome. `hbuild` is under active development. Feel free to file [issues](https://github.com/naaman/hbuild/issues) in its repo. – Naaman Newbold Sep 03 '14 at 23:09
  • 5
    Another way to avoid messing up git history is to work in a branch and clean up afterwards before merging into master. If your branch is named `debug-heroku`, deploy like this: `git push heroku debug-heroku:master`. – Zubin May 17 '15 at 23:23
  • 1
    Please note we should run `heroku vim` from our local cli, **not** inside the server-side heroku bash shell – Nick Sarafa May 17 '16 at 07:44
  • 8
    Not sure this is working anymore - yarn exits with 'Refusing to download the git repo .... over HTTPS without a commit hash - possible certificate error?' – Tim Malone Jun 27 '17 at 06:28
  • 1
    That didn't work, but what worked for me: `heroku plugins:install heroku-vim`. – rtfminc Sep 22 '17 at 19:57
  • Did not work for me. When I try to launch vim after installing the plugin I get `vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory` – funkenstrahlen Oct 15 '17 at 07:58
  • 3
    This answer no longer works with the `heroku-16` stack, as it [no longer has `libxt` available at runtime](https://devcenter.heroku.com/articles/stack-packages). @funkenstrahlen – Jonah H. Oct 30 '17 at 17:31
63

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.

James
  • 1,239
  • 1
  • 11
  • 18
29

there's ed if you're a masochist.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
22

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.

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
Brian Takita
  • 1,615
  • 1
  • 15
  • 20
  • Just what I needed! Edited a command, ran it. Now heroku can remove my changes over the night, I got my job done! – mika Aug 31 '16 at 10:57
  • 1
    See more up-to-date script over there https://gist.github.com/dvdbng/7375821b20f189c189ab1bd29392c98e – nilfalse Jun 06 '19 at 19:27
15

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

James Ward
  • 29,283
  • 9
  • 49
  • 85
  • 21
    I would like to debug the environment from `heroku run bash`, so vi/vim would be very useful for me. – Brian Takita Mar 02 '13 at 23:14
  • 3
    You can now connect to the actual dyno with `heroku ps:exec`. – Nicholas Morley Dec 23 '17 at 11:47
  • @NicholasMorley You can, but how would you make a site/app notice changes in a file? – x-yuri Sep 18 '19 at 18:24
  • @x-yuri You can't do things that way on Heroku. You need to redeploy the app to make changes. – James Ward Sep 18 '19 at 22:40
  • Actually you can. You can take a dyno that's running a site, ssh into it (`heroku ps:exec`), change a file, start a second instance of the server, forward a port to the dyno (`heroku ps:forward 3000`). Then, `curl localhost:3000`. – x-yuri Sep 23 '19 at 17:42
  • You can find a more detailed explanation in [my answer](https://stackoverflow.com/a/58067826/52499). – x-yuri Sep 23 '19 at 18:00
  • Woah! I didn't know that. Thanks for the detailed answer and correction. – James Ward Sep 24 '19 at 04:13
13

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

Jason Hee
  • 151
  • 1
  • 5
7

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! :)

Jared
  • 767
  • 15
  • 15
6

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.

Jawa
  • 2,336
  • 6
  • 34
  • 39
Mikhail
  • 8,692
  • 8
  • 56
  • 82
  • You can actually [forward a port](https://stackoverflow.com/a/58067826/52499) and use your browser in place of `curl`. – x-yuri Sep 23 '19 at 18:05
5

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
Vilson Vieira
  • 717
  • 8
  • 7
2

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:

    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

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

Hope it helps!

Charles Bochet
  • 269
  • 2
  • 3
  • The plugin didn't work for me: `vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory`. But the buildpack did work. – x-yuri Sep 18 '19 at 18:28
2

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
mjk
  • 2,443
  • 4
  • 33
  • 33
1

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.

wodow
  • 3,871
  • 5
  • 33
  • 45
0

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/

ewwink
  • 18,382
  • 2
  • 44
  • 54
0

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.
x-yuri
  • 16,722
  • 15
  • 114
  • 161
0

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