Installing Solr on Ubuntu is much harder than it sounds. You'd think a simple sudo apt-get install solr would do the trick. Apparently not. The package manager in Ubuntu 12.04 will only get you Solr 1.4. That version is now years old and you probably want the latest version.
The majority of instructions I found on the topic involve downloading the package, then running an example jar file. This is all fine to get up and running quickly, but I'm not particularly keen on running an example jar file in a production environment. I put together this guide on a production ready installation of Solr because of the absence of anything else on the subject.
Solr needs to be installed on top of a java servlet container. There appear to be two major contenders: jetty and tomcat. I'm going to choose jetty because it's lighter weight and setup is supposed to be easier.
Please note this guide was originally written for installing Solr 3.6. I have updated the references to install Solr 4.3, but this has not yet been tested.
1. Install java
sudo apt-get install openjdk-7-jdk
2. Install jetty
sudo apt-get install jetty
sudo apt-get install libjetty-extra
3. Download and unpack solr
cd ~
wget http://mirror.ox.ac.uk/sites/rsync.apache.org/lucene/solr/4.3.0/solr-4.3.0.tgz
tar -xzvf solr-4.3.0.tgz
sudo mkdir /usr/share/solr
cd /usr/share/solr
sudo unzip ~/solr-4.3.0/dist/solr-4.3.0.war
cp -R ~/solr-4.3.0/example/lib/ext/* /usr/share/jetty/lib/ext/
This puts the solr web files in /usr/share/solr Note: For versions later than 4.3, the above file references will need to be updated.
4. Add solr to jetty
sudo ln -s /usr/share/solr /usr/share/jetty/webapps/solr
This ensures yoursite.com:8080/solr will serve from your solr directory.
5. Configure jetty
Add the following lines to /etc/default/jetty
NO_START=0
JETTY_HOST=0.0.0.0
JAVA_OPTIONS="-Dsolr.solr.home=/usr/share/solr $JAVA_OPTIONS"
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
Please note your JAVA_HOME directory may be different depending on your processor.
NO_START=0 allows jetty to start. JETTY_HOST=0.0.0.0 allows jetty to be accessed from outside the localhost (don't use this setting in production unless you have user authentication - see my follow up post). The JAVA_OPTIONS line lets jetty know where your solr home directory is. JAVA_HOME needs to be set for openjdk-7-jdk to ensure jetty can find your java installation.
6. Add cores to your solr configuration
sudo nano /usr/share/solr/solr.xml
<solr persistent="true">
<cores adminPath="/admin/cores">
<core name="sitename" instanceDir="sitename" dataDir="/var/lib/solr/sitename/data" />
</cores>
</solr>
This adds a core called 'sitename'. You can add as many cores as you like here.
Create the folder for the core you just defined:
sudo mkdir -p /var/lib/solr/sitename/data
Set folder owners to jetty
sudo chown -R jetty:jetty /var/lib/solr/
sudo chown -R jetty:jetty /usr/share/solr
7. Configure the core
Add some configuration for the core you just added:
sudo mkdir -p /usr/share/solr/sitename
Copy the example conf files from the original download to your core:
sudo cp -r ~/solr-4.3.0/example/solr/collection1/conf /usr/share/solr/sitename/
8. Update schema.xml with your own schema.xml
You can now overwrite /usr/share/solr/sitename/conf/schema.xml with your own file.
9. Restart jetty
sudo /etc/init.d/jetty restart
10. Delete the download from your home directory
sudo rm ~/apache-solr*
Solr should now be accessible on mysite:8080/solr
Thanks to Eric and Sean for the comments to update this guide from Solr 3.6 to 4.3.