198

I'm trying to set up Apache Tomcat on my pc, and it wants me to set up an environment variable for CATALINA_HOME. Does any know how to do this?

Roger Light
  • 1,384
jumpnett
  • 6,185

8 Answers8

218

In bash you can set variables like this:

export CATALINA_HOME=/opt/catalina

most other shells follow this convention, but not all. You can set it permanently in ~/.profile for bash (and as before, other shells have other locations)

Ressu
  • 14,086
67

To set permanent environment variables in latest Ubuntu versions (from 14.04 and above) add the variables to /etc/environment. For that follow the below instructions,

Open the terminal and run

sudo -H gedit /etc/environment

the provide your password, then in the prompted text file

then add the variables like

ANT_HOME="/opt/ANT/"

Sample of the /etc/environment is given below

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME="/usr/lib/jvm/java-8-oracle/"
AXIS2_HOME="/opt/axis2-1.7.4/"
ANT_HOME="/opt/apache-ant-1.9.7/"

don't forget to logout and login again to enable the environment variables.

Pablo Bianchi
  • 17,371
31

Environment variables should already work

If you are using the tomcat6 package from the Ubuntu repositories, then the CATALINA_HOME and other environment variables are already set, in the /etc/init.d/tomcat6 startup script.

If you are installing tomcat outside the package manager (hopefully in /opt or somewhere else outside the managed file system), then running the TOMCAT/bin/startup.sh should use the relative location to define the CATALINA_HOME.

Setting the Environment variable

If for some reason you still need to set an environment variable you can open a terminal window and type in the command:

export CATALINA_HOME=/path/to/the/root/folder/of/tomcat

This environment variable will now work within that terminal window, but if you open another window or logout/login you loose that setting.

Make the environment variable permanent

To make the environment variable setting permanent, there are several places you can define the setting.

To be really sure the setting is being picked up, add the above setting to one of the startup script for tomcat:

yourtomcatfolder/bin/startup.sh

yourtomcatfolder/bin/catalina.sh

Note: startup.sh calls the catalina.sh. You should add the setting at the start of one of these files (after any initial comments)

The standard way for global environment variables would be to add an entry in /etc/environment (you do not use the command export in this file as it is not a normal bash script)

CATALINA_HOME=/path/to/the/root/folder/of/tomcat

Not recommended

You can set the environment variables in the bash (command line shell) configuration files, but these are not recommended as they are not always picked up (eg. if you are running a server that you dont login to to run tomcat): ~/.bashrc | ~/.profile | /etc.bash.bashrc | /etc/profile

12

Open your Bash runcom file:

nano ~/.bashrc

This will most likely contain quite a bit of data already. Most of the definitions here are for setting bash options, which are unrelated to environmental variables. You can set environmental variables just like you would from the command line:

export VARNAME=value

See How To Read and Set Environmental and Shell Variables on Linux

I tested it on Ubuntu 16.04. Works great.

Greenonline
  • 2,182
Sunny127
  • 1,305
  • 2
  • 9
  • 4
8

The best place for this depends on how and where you've installed Tomcat, what applications you want to pick up this setting and how global you want the scope to be.

The Ubuntu documentation on Environment Variables discusses the pros and cons of the various options.

McDowell
  • 393
3

As above, I will use the export to save an environment variable with a small difference. I prefer to save them in a local file.

echo "export POSTMARK_SERVER_TOKEN=sekritvalue" >> .env

In this way, anytime and from any terminal, your variable will work and be there with your project. Don't forget to include .env to your .gitignore, DO NOT push them to Git.

hellyworld
  • 31
  • 1
2

After going through Ubuntu Documentation on Environment Variables, I came up with following workaround:

##Save & run the following in a shell script,

export ENVIRON_VAR_NAME = Value
# any other initializations like
export PATH=$PWD:$PATH
bash

The last line creates a child shell, which inherits Environment Variable values from parent shell (which have just been set).

muru
  • 207,228
1

You can use systemd's environment.d:

As a user, you can drop a file in ~/.config/environment.d/foo.conf with VAR=value.

For all users, /etc/environment.d/foo.conf, or /etc/environment can be used.

You can also configure /etc/systemd/system/user@UID.service.d/foo.conf for a specific user or /etc/systemd/system/user@.service.d/foo.conf for all users. The contents f the file is then

[Service]
Environment="FOO=value"
Flimm
  • 44,031