Linux Application Installation: Best Practices for /var vs /opt vs /usr/local Directory Placement


17 views

When working with Linux systems, the Filesystem Hierarchy Standard (FHS) defines the purpose of each directory. Here's what you need to know about the key directories for application installation:

/var - Variable data files (logs, databases, spool files)
/opt - Optional application software packages
/usr/local - Locally installed software (not part of OS distribution)
/etc - Configuration files (not for application binaries)

The /opt directory is ideal for self-contained, third-party applications that don't follow standard Unix directory structure. For example:

# Typical /opt installation structure for Tomcat
/opt/apache-tomcat-9.0.54/
├── bin/
├── conf/
├── lib/
├── logs/ -> /var/log/tomcat9
├── webapps/

Advantages of /opt:

  • Keeps all application files in one place
  • Easier to remove entire applications
  • Common practice for commercial software

While /var shouldn't contain application binaries, it's perfect for:

# Example of proper /var usage
/var/lib/tomcat9/ - Persistent application data
/var/log/tomcat9/ - Application log files
/var/cache/tomcat9/ - Cached data

Use /usr/local when compiling software from source that should be available system-wide:

# Typical ./configure command for source installation
./configure --prefix=/usr/local
make
sudo make install

This follows the standard Unix directory structure:

  • Binaries in /usr/local/bin
  • Libraries in /usr/local/lib
  • Configuration in /usr/local/etc

For a Java application like Elasticsearch, here's how I would install it:

# Extract to /opt
sudo tar -xzf elasticsearch-7.15.0-linux-x86_64.tar.gz -C /opt

# Create symlink for easier version management
sudo ln -s /opt/elasticsearch-7.15.0 /opt/elasticsearch

# Configure data directories in /var
sudo mkdir -p /var/lib/elasticsearch
sudo chown elasticsearch:elasticsearch /var/lib/elasticsearch

Different distributions may have conventions:

  • RHEL/CentOS: Prefers /opt for third-party software
  • Debian/Ubuntu: Often uses /usr/share for application files
  • Always check distro-specific guidelines

The Linux Filesystem Hierarchy Standard defines specific purposes for each directory:

/var - Variable data (logs, spools, temp files)
/opt - Optional/add-on application software packages
/usr/local - Locally installed software (not from distribution packages)
/etc - Configuration files for system and applications

/opt is ideal for self-contained applications that don't follow standard Unix directory structure. Common use cases:

  • Commercial software (Oracle Database, MATLAB)
  • Large applications with all dependencies bundled
  • Applications that may need multiple versions installed

Example Tomcat installation in /opt:

wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.8/bin/apache-tomcat-10.1.8.tar.gz
sudo tar -xzvf apache-tomcat-10.1.8.tar.gz -C /opt
sudo ln -s /opt/apache-tomcat-10.1.8 /opt/tomcat

/var should be used for runtime data rather than application binaries. Typical scenarios:

  • Application working directories
  • PID files, lock files
  • Log files (/var/log)
  • Spool directories (/var/spool)

Example Java application runtime directory structure:

/var/lib/myapp/ - Application data
/var/log/myapp/ - Log files
/var/run/myapp/ - PID files
/var/cache/myapp/ - Cache files

While both are for locally installed software, they follow different conventions:

Directory Structure Best For
/opt Application bundles all files in single directory Large, self-contained apps
/usr/local Follows standard Unix hierarchy (bin, lib, etc.) Locally compiled software

/etc should only contain configuration files, not binaries or application data. Example Tomcat configuration:

/etc/tomcat10/server.xml - Main configuration
/etc/tomcat10/web.xml - Default web application settings
/etc/default/tomcat10 - Environment variables

For Java web applications:

  • Install Tomcat in /opt/tomcat
  • Place WAR files in /var/lib/tomcat/webapps
  • Store logs in /var/log/tomcat

For system services:

  • Place init scripts in /etc/init.d or /lib/systemd/system
  • Runtime files in /var/run

For custom compiled software:

  • ./configure --prefix=/usr/local
  • Creates proper hierarchy under /usr/local

Here's how I typically structure a Java/Tomcat/PostgreSQL setup:

# PostgreSQL
/var/lib/postgresql/14/main - Data files
/etc/postgresql/14/main - Configuration

# Java
/usr/lib/jvm/java-11-openjdk-amd64 - JDK

# Tomcat
/opt/apache-tomcat-10.1.8 - Installation
/var/lib/tomcat/webapps - Deployed applications
/var/log/tomcat - Logs
/etc/tomcat - Configuration

/opt makes version management easier through symlinks:

/opt/tomcat-9.0.75
/opt/tomcat-10.1.8
/opt/tomcat -> /opt/tomcat-10.1.8

Update the symlink when switching versions while keeping configs in /etc/tomcat.

  • /opt and /usr/local typically need root for installation
  • /var directories often need specific user/group permissions
  • Follow principle of least privilege for each directory

Distribution packages will automatically place files in correct locations:

# Ubuntu/Debian
apt install tomcat9 - Installs to FHS-compliant locations

# Manual install gives more control but requires proper placement