Friday, May 10, 2013

Installing Emacs 24 on Ubuntu Server 12.04

tl;dr


sudo apt-get install python-software-properties

sudo add-apt-repository ppa:cassou/emacs

sudo apt-get update

sudo apt-get purge emacs-snapshot-common emacs-snapshot-bin-common emacs-snapshot emacs-snapshot-el emacs-snapshot-gtk emacs23 emacs23-bin-common emacs23-common emacs23-el emacs23-nox emacs23-lucid auctex emacs24 emacs24-bin-common emacs24-common emacs24-common-non-dfsg

sudo apt-get install emacs24 emacs24-el emacs24-common-non-dfsg

sudo rm /etc/apt/sources.list.d/cassou-emacs-precise.list


# ~/.emacs.d/init.el:
(require 'package)
(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)

# in emacs
M-x list-packages
# press [ENTER] on slim-mode
# move to buffer and press [ENTER] on [Install]


Part 1: Install emacs 24.

The docs for slim-mode talk about having Emacs 24 or package.el. As of this writing, the version of emacs available in Ubuntu 12.04 is 23.3.1 :(. We don't want to build things up on old software versions, now do we? Of course, it's more complicated than it should be, otherwise there would be no point in blogging about it.

The recommended way to get Emacs 24 on Ubuntu 12.04 appears to be by using Damien Cassou's PPA for Emacs. To do that, we need to add the PPA to apt-get:

sudo add-apt-repository ppa:cassou/emacs

which gives us the error:

sudo: add-apt-repository: command not found

Here's a great write-up about how to fix that error, as well as some additional info about using a magical apt-file tool. Let me like to the error message again for some extra link juice to that page: sudo: add-apt-repository: command not found.

So then we proceed with
sudo apt-get install python-software-properties
then
sudo add-apt-repository ppa:cassou/emacs
This writes to /etc/apt/sources.list.d/cassou-emacs-precise.list.

Then I did a
sudo apt-get update
and
sudo apt-get upgrade emacs
which appeared to be replacing Emacs 23 with 24.1, but after it finished, I still got Emacs 23.3.1 from emacs --version. :(

So let's try the instructions from Damien.

sudo apt-get purge emacs-snapshot-common emacs-snapshot-bin-common emacs-snapshot emacs-snapshot-el emacs-snapshot-gtk emacs23 emacs23-bin-common emacs23-common emacs23-el emacs23-nox emacs23-lucid auctex emacs24 emacs24-bin-common emacs24-common emacs24-common-non-dfsg

sudo apt-get install emacs24 emacs24-el emacs24-common-non-dfsg

And now,
emacs --version
gives me Emacs 24.3.1. Success!

Now, just to be paranoid, I'm going to remove the PPA from my system, as I probably don't need it anymore.

sudo rm /etc/apt/sources.list.d/cassou-emacs-precise.list

Part 2: Install slim-mode.

Now that we've got Emacs 24 installed, we go back to the slim-mode documentation. Various installation options are listed there: Manual, Marmalade, MELPA, Emacs Prelude. I tried using Emacs Prelude, but it's really rather beefy. It slowed down my emacs launching substantially, and tried to change the way I like things too much (cursor/arrow keys disabled by default, hard-to-see colors in my putty terminal, ...).

From what I can tell, MELPA is more active than Marmalade (which could have the more "stable" versions), so let's use that. To install MELPA, we add the following to ~/.emacs.d/init.el:

(require 'package)
(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)

The (require 'package) fixes the initialization errors about "Symbol's value as variable is void: package-archives" when you start emacs.

Alternatively, you can open emacs, run M-x package-initialize [ENTER], then M-x cusomize-variable [ENTER] package-archives [ENTER], and use the UI to insert a new value after the gnu archive - Archive name: melpa, URL: http://melpa.milkbox.net/packages/. Then save this for future sessions (via the [ State ] control), and it will write this to ~/.emacs for you.

So now, inside of emacs, run M-x list-packages, find slim-mode, and press [ENTER] on it, and [ENTER] on the [Install] option in the buffer that loads. This will install files in ~/.emacs.d/elpa/, and your slim-mode should be ready to roll! (I get some warnings when installing slim-mode, but it seems to work anyway).

Success!

Monday, May 6, 2013

How To Install Ruby 2.0 & Rails 4.0 on Ubuntu Server 12.04

0. Start with Ubuntu Server LTS 12.04.2

ubuntu-12.04.2-server-i386.iso, running in VMware Player, with OpenSSH server installed during ubuntu setup.

1. Use rbenv.

The packaged ruby available via apt-get is not 2.0, so use rbenv instead (and use rbenv in general, anyway). Follow the installation instructions for ubuntu here: https://github.com/sstephenson/rbenv. Of course you'll need git in order to complete that:
sudo apt-get install git

You should be able to make it through the installation of ruby-build as a rbenv plugin, but then your actual attempt to install ruby will probably fail.

2. Install other libraries to fix the build & run problems.

Here's the basic workflow:
rbenv install --list
rbenv install 2.0.0-p0
Fail. Fix. Repeat.

The "rbenv install 2.0.0-p0" takes some time to download the ruby source first, then starts to build it. This only addresses problems during the build phase, not download problems.

2.1. configure: error: no acceptable C compiler found in $PATH

The basic Ubuntu Server does not come with many development necessities. But there's a wonderful package to get you started:
sudo apt-get install build-essential

2.2. The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

Even though you have openssl on your server, you don't have the development libraries needed to build on top of it. So:

sudo apt-get install libssl-dev


However, when you get this error, you can see some other things in the log as well:

...
Failed to configure openssl. It will not be installed.
...
Failed to configure readline. It will not be installed.
...
Failed to configure zlib. It will not be installed.
...

There are several other "not installed" lines as well, but most of them are windows-related and I don't care about them. libssl-dev will actually install zlib1g-dev for you, which seems to take care of the zlib issue.

After installing libssl-dev, you'll be able to successfully install ruby. However, when you install rails, and then you try to do "rails console", you'll discover that it throws an exception due to readline:
.../completion.rb:in `require': cannot load such file -- readline (LoadError)
So you'll want to fix the readline issue as well:
sudo apt-get install libreadline-dev

Now you can do
rbenv install 2.0.0-p0
and your ruby should be ready for rails.

3. Install Rails Gems.

First, start using your new ruby.
rbenv rehash
rbenv local 2.0.0-p0

Aside: At this point, I like to tell the gem installer to ignore building the documentation. To do that, put the following in your ~/.gemrc file:
install: --no-rdoc --no-ri 
update:  --no-rdoc --no-ri


Now, install bundler and rails.
gem install bundler
gem install rails --version 4.0.0.rc1
... and don't forget to relink the shims:
rbenv rehash
At this point, you should be able to use the "rails" command.

4. Create a new Rails app.

Depending on your database requirements, you may need to install various database libraries or gems. However, you should also be able to create a new Rails app without using ActiveRecord with no problem.

4.1. Install nodejs.

You can create a rails app without any backing database by skipping active record dependencies.
rails new myapp -O
But even if you do that, you'll still run into an error:
cd myapp
rails console
.../execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
So now you should install nodejs:
sudo apt-get install nodejs
rails console
Success!

4.2 With Rails' default (SQLite)

The following command fails:
rails new myapp
with errors:

Installing sqlite3 (1.3.7)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
...
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
...
An error occurred while installing sqlite3 (1.3.7), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.7'` succeeds before bundling.

You can fix this via:

sudo apt-get install libsqlite3-dev
rm -r myapp
rails new myapp
cd myapp
rails console
Success!