How To Install Tomcat In MacOS

Apache Tomcat is a widely used web server. It can be installed and run on numerous operating systems like Windows, Linux, and macOS. This article will tell you how to install tomcat on mac step by step. But before installing it, you should enable root user in macOS following article How To Enable Root User Or Change Root Password In MacOS

1. Install Tomcat On Mac Steps.

  1. First make sure you have installed JDK on macOS, run java -version in terminal to get the installed version. If you do not have installed JDK, you need to download and install one.
    sh-3.2# java -version
    java version "1.8.0_172"
    Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
    
  2. Download the tomcat install package from https://tomcat.apache.org/download-90.cgi page. You can select a zip file or tar.gz file.
  3. After download, unzip the compressed file to a local directory.
  4. Run cd go to the unzip file directory.
  5. Then run the below command to make the .sh file executable.
    sh-3.2# cd /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8
    
    sh-3.2# sudo chmod +x ./bin/*.sh
  6. Now all the .sh file in tomcat bin directory is executable, you can run the command ls -al to see that.
  7. Now run ./bin/startup.sh to start tomcat. When you see the below message, it means tomcat has been started successfully.
    sh-3.2# ./bin/startup.sh 
    Using CATALINA_BASE:   /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8
    Using CATALINA_HOME:   /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8
    Using CATALINA_TMPDIR: /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8/temp
    Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home
    Using CLASSPATH:       /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8/bin/bootstrap.jar:/Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8/bin/tomcat-juli.jar
    Tomcat started.
  8. Now open a web browser, input http://localhost:8080/ in the URL address input box. Click Enter key then you can see the apache tomcat home page.
  9. To stop tomcat, you can run the command ./bin/shutdown.sh in terminal.

2. Use HomeBrew To Install Tomcat On macOS.

  1. If you find the above method is complicated, you can use HomeBrew to install tomcat directly.
  2. Before installing the Tomcat with HomeBrew you should make sure the JDK has been installed on your macOS with the java -version commands.
    $ java -version
    openjdk version "1.8.0_212"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)
  3. If you do not has JDK installed, you can run the command brew install java to install it first.
  4. Open a terminal and run the command brew install tomcat, then it will start to install tomcat.
  5. If you meet an error message like the below, that means your HomeBrew has a shallow clone, and the new HomeBrew version needs to remove the shallow clone before use the brew command.
    $ brew install tomcat
    Error: 
      homebrew-core is a shallow clone.
      homebrew-cask is a shallow clone.
    To `brew update`, first run:
      git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
      git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
    These commands may take a few minutes to run due to the large size of the repositories.
    This restriction has been made on GitHub's request because updating shallow
    clones is an extremely expensive operation due to the tree layout and traffic of
    Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
    automatically to avoid repeatedly performing an expensive unshallow operation in
    CI systems (which should instead be fixed to not use shallow clones). Sorry for
    the inconvenience!
    Error: tomcat: Unsupported special dependency :java
  6. You can run the command git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow and git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch –unshallow to unshallow the HomeBrew shallow clone.
  7. If you are lucky, the above command will run successfully. If you meet an error message like below, that is because of your local firewall, you need to contact your administrator to bypass the firewall block. Or maybe restart your OS can fix this issue, I found this workaround from the internet.
    $ git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
    fatal: unable to access 'https://github.com/Homebrew/homebrew-cask/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 
    
  8. When the brew install tomcat process completes successfully, you can find its installation directory is /usr/local/Cellar/tomcat/9.0.43.
  9. When you step into the tomcat installation folder, there is a bin folder in the folder,  and there is an executable file catalina in the bin folder.
    $ pwd
    /usr/local/Cellar/tomcat/9.0.43/bin
    $ ls -l
    -r-xr-xr-x  1 songzhao  admin  116 Mar  5 14:13 catalina
  10. Run the command catalina in a command line, it will list the help data about how to start, stop, restart Tomcat, etc.
  11. Run the command catalina start to start Tomcat server.
    $ catalina start
    Using CATALINA_BASE:   /usr/local/Cellar/tomcat/9.0.43/libexec
    Using CATALINA_HOME:   /usr/local/Cellar/tomcat/9.0.43/libexec
    Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/9.0.43/libexec/temp
    Using JRE_HOME:        /usr/local/opt/openjdk
    Using CLASSPATH:       /usr/local/Cellar/tomcat/9.0.43/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/9.0.43/libexec/bin/tomcat-juli.jar
    Using CATALINA_OPTS:   
    Tomcat started.
    
  12. Open a web browser and browse URL http://localhost:8080/, you can find the Tomcat default web page.
  13. To stop the Tomcat server, run the command /usr/local/Cellar/tomcat/9.0.43/bin/catalina stop.
  14. There is another folder that contains the Tomcat start, stop, restart script files, the other folder is /usr/local/Cellar/tomcat/9.0.43/libexec/bin.
    $ pwd
    /usr/local/Cellar/tomcat/9.0.43/libexec/bin
    $ ls -l
    total 1632
    -rw-r-----  1 songzhao  admin   34805 Jan 29 04:28 bootstrap.jar
    -rw-r-----  1 songzhao  admin    1664 Jan 29 04:29 catalina-tasks.xml
    -rwxr-x---  1 songzhao  admin   25294 Jan 29 04:28 catalina.sh
    -rwxr-x---  1 songzhao  admin    1997 Jan 29 04:28 ciphers.sh
    -rw-r-----  1 songzhao  admin  207420 Jan 29 04:28 commons-daemon-native.tar.gz
    -rw-r-----  1 songzhao  admin   25357 Jan 29 04:28 commons-daemon.jar
    -rwxr-x---  1 songzhao  admin    1922 Jan 29 04:28 configtest.sh
    -rwxr-x---  1 songzhao  admin    9100 Jan 29 04:28 daemon.sh
    -rwxr-x---  1 songzhao  admin    1965 Jan 29 04:28 digest.sh
    -rwxr-x---  1 songzhao  admin    3382 Jan 29 04:28 makebase.sh
    -rwxr-x---  1 songzhao  admin    3708 Jan 29 04:28 setclasspath.sh
    -rwxr-x---  1 songzhao  admin    1902 Jan 29 04:28 shutdown.sh
    -rwxr-x---  1 songzhao  admin    1904 Jan 29 04:28 startup.sh
    -rw-r-----  1 songzhao  admin   46756 Jan 29 04:28 tomcat-juli.jar
    -rw-r-----  1 songzhao  admin  423135 Jan 29 04:28 tomcat-native.tar.gz
    -rwxr-x---  1 songzhao  admin    5540 Jan 29 04:28 tool-wrapper.sh
    -rwxr-x---  1 songzhao  admin    1908 Jan 29 04:28 version.sh
  15. And the Tomcat server configuration file server.xml file is saved in the directory /usr/local/Cellar/tomcat/9.0.43/libexec/conf.
    $ pwd
    /usr/local/Cellar/tomcat/9.0.43/libexec/conf
    $ ls -l
    drwxr-x---  3 songzhao  admin      96 Mar  5 14:55 Catalina
    -rw-------  1 songzhao  admin   12873 Jan 29 04:29 catalina.policy
    -rw-------  1 songzhao  admin    7262 Jan 29 04:29 catalina.properties
    -rw-------  1 songzhao  admin    1400 Jan 29 04:29 context.xml
    -rw-------  1 songzhao  admin    1149 Jan 29 04:29 jaspic-providers.xml
    -rw-------  1 songzhao  admin    2313 Jan 29 04:29 jaspic-providers.xsd
    -rw-------  1 songzhao  admin    4144 Jan 29 04:29 logging.properties
    -rw-------  1 songzhao  admin    7588 Jan 29 04:29 server.xml
    -rw-------  1 songzhao  admin    2164 Jan 29 04:29 tomcat-users.xml
    -rw-------  1 songzhao  admin    2558 Jan 29 04:29 tomcat-users.xsd
    -rw-------  1 songzhao  admin  172359 Jan 29 04:29 web.xml
    
  16. If you want the Tomcat server auto-start when you log in to macOS, you can run the command brew services start tomcat to do it.
    $ brew services start tomcat
    ==> Tapping homebrew/services
    Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
    remote: Enumerating objects: 20, done.
    remote: Counting objects: 100% (20/20), done.
    remote: Compressing objects: 100% (20/20), done.
    remote: Total 1141 (delta 8), reused 0 (delta 0), pack-reused 1121
    Receiving objects: 100% (1141/1141), 331.53 KiB | 94.00 KiB/s, done.
    Resolving deltas: 100% (484/484), done.
    Tapped 1 command (40 files, 420.5KB).
    ==> Successfully started `tomcat` (label: homeb
  17. The command brew services stop tomcat will stop the Tomcat server auto-start when the user login to macOS.
    $ brew services stop tomcat
    Stopping `tomcat`... (might take a while)
    ==> Successfully stopped `tomcat` (label: h

3. How To Change Tomcat Default Listening Port Number.

Tomcat default listen on port 8080. When you meet an error during startup tomcat, the reason may be that the port number has been used by another process. So you need to change the port number to what you need. Below are the steps to change the tomcat server HTTP port number.

  1. Open tomcat-install-directory/conf/server.xml file in a file editor. In my environment, the server.xml file located at /Users/zhaosong/Documents/WorkSpace/tool/apache-tomcat-9.0.8/conf/server.xml.
  2. Find below connector configuration in server.xml file. And change the port number from 8080 to what you want  such as 8089
    <Connector port="8089" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
    
  3. Save the above settings and start tomcat again, now you can access tomcat from the new URL http://localhost:8089/ with the new port number that you configured.

7 thoughts on “How To Install Tomcat In MacOS”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.