Table of Contents
In this article, we will see how to install sbt on Ubuntu Linux. If you are looking to build complex and multi-module scala projects then you should definitely go with sbt command line tool. Although this open source tool has lot of features but it is mostly known for its incremental compilation model. In this model, sbt would only compile the part of project that have changed since last build. This saves lot of compilation time in a large project. SBT (Simple Build Tool) works in an interactive mode which means it will keep the project loaded in memory, ensuring faster results from the command given.
Since sbt uses apache ivy for dependency management, this makes it easy to handle downloading, updating and managing libraries and its dependencies. You can also extend the functionality of sbt by using suitable plugins. It support plugins for tasks like code coverage, static analysis, web deployment, and integration with various frameworks and tools. It can also be used with CI tools like Jenkins, Travis CI and GitHub Actions. Since the build definitions of sbt are written in scala, this allows you to exploit the full power of scala to write custom build logic, conditions and task definitions. More on official website.
How to Install sbt on Ubuntu 22.04
Also Read: How to Install Kong Gateway on Ubuntu Linux
Step 1: Prerequisites
a) You would need a fully functional Ubuntu 22.04
System.
b) You would also need root or root like access to run privileged commands.
c) You would also require a default ubuntu apt
or apt-get
package manager to be installed in your system.
Step 2: Update Your Server
Run below command to check and install latest package upgrades, security fixes and feature updates in your system.
cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
Step 3: Install required packages
Run below command to install few mandatory packages which are required for sbt installation.
cyberithub@ubuntu:~$ sudo apt-get install apt-transport-https curl gnupg -yqq
[sudo] password for cyberithub:
Step 4: Add Repo
Since sbt package is only available through its repo, run below command to add the repo in your repo list.
cyberithub@ubuntu:~$ echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list deb https://repo.scala-sbt.org/scalasbt/debian all main
Step 5: Import GPG Key
To verify the integrity and authenticity of a package, import and add gpg key before downloading and installing the package. This can be done by running below curl
command. You can get the latest step to import key from official website.
cyberithub@ubuntu:~$ curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo -H gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/scalasbt-release.gpg --import gpg: keyring '/etc/apt/trusted.gpg.d/scalasbt-release.gpg' created gpg: directory '/root/.gnupg' created gpg: /root/.gnupg/trustdb.gpg: trustdb created gpg: key 99E82A75642AC823: public key "sbt build tool <scalasbt@gmail.com>" imported gpg: Total number processed: 1 gpg: imported: 1
Step 6: Change Permission
Modify and provide appropriate permission to the gpg file using below chmod
command.
cyberithub@ubuntu:~$ sudo chmod 644 /etc/apt/trusted.gpg.d/scalasbt-release.gpg
Step 7: Update Repository Cache
To load information of all the packages from newly added repo, run sudo apt update
command. This will update the package cache and prepare the package for installation.
cyberithub@ubuntu:~$ sudo apt update [sudo] password for cyberithub: Get:1 https://packages.microsoft.com/ubuntu/22.04/prod jammy InRelease [3,632 B] Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB] Hit:3 http://in.archive.ubuntu.com/ubuntu jammy InRelease Get:5 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB] Get:6 https://deb.dl.getenvoy.io/public/deb/ubuntu jammy InRelease [2,975 B] Get:7 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 Packages [141 kB] Ign:8 https://download.konghq.com/gateway-3.x-ubuntu-jammy default InRelease Get:9 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main arm64 Packages [35.7 kB] Hit:11 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease Get:4 https://scala.jfrog.io/artifactory/debian all InRelease [4,410 B] Get:12 http://security.ubuntu.com/ubuntu jammy-security/main i386 Packages [445 kB] Get:13 http://in.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages [611 kB] Hit:10 https://download.konghq.com/gateway-3.x-ubuntu-jammy default Release Ign:14 https://download.konghq.com/gateway-3.x-ubuntu-jammy default Release.gpg Get:15 https://scala.jfrog.io/artifactory/debian all/main i386 Packages [2,703 B] Get:16 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1,346 kB] Get:17 https://scala.jfrog.io/artifactory/debian all/main amd64 Packages [2,703 B] Get:18 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1,562 kB] Get:19 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [237 kB] Get:20 http://security.ubuntu.com/ubuntu jammy-security/universe i386 Packages [600 kB] Get:21 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [852 kB] Get:22 http://in.archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [298 kB] Get:23 http://in.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [1,754 kB] Get:24 http://in.archive.ubuntu.com/ubuntu jammy-updates/restricted i386 Packages [38.1 kB] Get:25 http://in.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [295 kB] Get:26 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,075 kB] Get:27 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe i386 Packages [699 kB] Get:28 http://in.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [42.7 kB] Get:29 http://in.archive.ubuntu.com/ubuntu jammy-updates/multiverse i386 Packages [4,472 B] Get:30 http://in.archive.ubuntu.com/ubuntu jammy-updates/multiverse Translation-en [10.4 kB] Fetched 10.3 MB in 8s (1,313 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done
Step 8: Install sbt
Now that all the packages information are loaded in repository cache, you can easily download and install sbt command line tool by running sudo apt-get install sbt
command as shown below. This will resolve and install all the required dependencies along with sbt package.
cyberithub@ubuntu:~$ sudo apt-get install sbt [sudo] password for cyberithub: Reading package lists... Done Building dependency tree... Done Reading state information... Done The following NEW packages will be installed: sbt 0 upgraded, 1 newly installed, 0 to remove and 37 not upgraded. Need to get 20.0 kB of archives. After this operation, 50.2 kB of additional disk space will be used. Get:1 https://scala.jfrog.io/artifactory/debian all/main amd64 sbt all 1.9.9 [20.0 kB] Fetched 20.0 kB in 2s (8,604 B/s) Selecting previously unselected package sbt. (Reading database ... 247213 files and directories currently installed.) Preparing to unpack .../apt/archives/sbt_1.9.9_all.deb ... Unpacking sbt (1.9.9) ... Setting up sbt (1.9.9) ... Creating system group: sbt Creating system user: sbt in sbt with sbt daemon-user and shell /bin/false Processing triggers for man-db (2.10.2-1) ...
Step 9: Create an example sbt build
Before launching sbt shell, create an example sbt build under a directory. For example, in our case we are creating a separate directory called example_build
using mkdir example_build
command. Then switch to it using cd example_build
command. Finally create an empty build.sbt
file using touch build.sbt
command.
cyberithub@ubuntu:~$ mkdir example_build cyberithub@ubuntu:~$ cd example_build/ cyberithub@ubuntu:~/example_build$ touch build.sbt
Step 10: Launch sbt interactive shell
To launch sbt, run sudo sbt
command. This will launch an interactive shell where you can perform various project compilation and management tasks.
cyberithub@ubuntu:~/example_build$ sudo sbt [sudo] password for cyberithub: downloading sbt launcher 1.9.9 copying runtime jar... [info] [launcher] getting org.scala-sbt sbt 1.9.9 (this may take some time)... [info] [launcher] getting Scala 2.12.18 (for sbt)... WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.jline.terminal.impl.exec.ExecTerminalProvider$ReflectionRedirectPipeCreator (file:/root/.sbt/boot/scala-2.12.18/org.scala-sbt/sbt/1.9.9/jline-terminal-3.24.1.jar) to constructor java.lang.ProcessBuilder$RedirectPipeImpl() WARNING: Please consider reporting this to the maintainers of org.jline.terminal.impl.exec.ExecTerminalProvider$ReflectionRedirectPipeCreator WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [info] Updated file /home/cyberithub/example_build/project/build.properties: set sbt.version to 1.9.9 [info] welcome to sbt 1.9.9 (Ubuntu Java 11.0.22) [info] loading project definition from /home/cyberithub/example_build/project [info] Updating https://repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.pom 100.0% [##########] 19.4 KiB (21.2 KiB / s) [info] Resolved dependencies [info] Fetching artifacts of https://repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.jar 100.0% [##########] 262.5 KiB (239.3 KiB / s) [info] Fetched artifacts of [info] loading settings for project example_build from build.sbt ... [info] set current project to example_build (in build file:/home/cyberithub/example_build/) [info] sbt server started at local:///root/.sbt/1.0/server/a9014407bff42aedcde7/sock [info] started sbt server sbt:example_build>
Step 11: Compile a Project
To compile a project, run compile
command. This will only compile the incremental changes done after the last build.
sbt:example_build> compile [success] Total time: 1 s, completed 21-Apr-2024, 1:00:22 PM
Step 12: Exit sbt shell
You can exit sbt shell anytime by running exit
command as shown below.
sbt:example_build> exit [info] shutting down sbt server
Step 13: Check all available options
You can check all the options available with sbt command line utility using sbt --help
command as shown below.
cyberithub@ubuntu:~$ sbt --help
Step 14: Uninstall sbt
If you don't need sbt tool anymore, you can remove it from your system by using sudo apt remove sbt
command as shown below. To remove all the dependencies of the package along with the package itself, add --auto-remove
option with below command.
cyberithub@ubuntu:~$ sudo apt remove sbt Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages will be REMOVED: sbt 0 upgraded, 0 newly installed, 1 to remove and 44 not upgraded. After this operation, 50.2 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 247226 files and directories currently installed.) Removing sbt (1.9.9) ... Processing triggers for man-db (2.10.2-1) ...