Sub topic 7
Installing and starting SQLcl
This chapter covers getting SQLcl onto your machine and launching it in the three ways you will use: with a connection, without one, and in a non-interactive mode. It assumes you know what host, port, service name, and credentials are, and that you have a terminal and an Oracle Database account to connect to. Connecting is covered in the next chapter. Here the goal is a working sql command.
What SQLcl is
SQLcl (SQL Command Line) is Oracle's command-line client for Oracle Database. It is a Java application, so it runs on any operating system with a supported Java runtime. It has no Oracle Client installation requirement of its own. By default it connects using the JDBC thin driver, which is pure Java.
It understands SQL, PL/SQL, and most SQL*Plus commands. It adds features such as command history, line editing, saved connections, and output formatting.
Prerequisites for installation
You need two things:
- A supported Java runtime (JRE or JDK). Each SQLcl release states the minimum Java version in its release notes. Check them before installing, because recent releases have raised the minimum.
- Permission to unpack a zip file into a directory you can read and run from. No administrator rights are needed.
Some distributions bundle a Java runtime. If yours does, you can skip Java setup. Otherwise sql will look for Java through the JAVA_HOME environment variable and then your PATH.
Installing from the zip file
The standard installation is a download and an unzip. Oracle publishes a stable "latest" link, so the steps are the same on every platform.
# Download the current SQLcl release
curl -L -o sqlcl-latest.zip \
https://download.oracle.com/otn_software/java/sqldeveloper/sqlcl-latest.zip
# Unpack it. This creates a directory named "sqlcl"
unzip sqlcl-latest.zip -d "$HOME/tools"
# Confirm the launcher is there
ls "$HOME/tools/sqlcl/bin"The bin directory contains the launcher scripts. On Linux and macOS the launcher is sql. On Windows it is sql.exe (older releases shipped sql.bat). The lib directory contains the Java libraries, and you should not move files out of it.
Putting `sql` on your PATH
Without a PATH entry you must type the full path every time. Add the bin directory once.
On Linux or macOS, add this line to your shell profile (~/.bashrc or ~/.zshrc):
export PATH="$HOME/tools/sqlcl/bin:$PATH"On Windows, add the full path to sqlcl\bin to the user Path environment variable through System Properties, then open a new terminal.
Other ways to get SQLcl
The zip is the most predictable method, but there are alternatives:
- Package managers. On macOS, Homebrew offers a
sqlclpackage. The package manager handles thePATHentry, but its version can lag behind the Oracle download. - Oracle Cloud Infrastructure Cloud Shell. SQLcl is preinstalled, so you can launch it with no setup.
- Editor integrations. Oracle's SQL Developer extension for Visual Studio Code uses SQLcl internally.
Checking Java
If sql fails to start, the cause is almost always Java. Check which Java the launcher will find.
java -version
echo "$JAVA_HOME"If java -version reports a version older than the minimum, or reports nothing, install a supported JDK and set JAVA_HOME to its directory. When both JAVA_HOME and a different java on PATH exist, the launcher prefers JAVA_HOME, so a stale JAVA_HOME is a common cause of "wrong version" errors.
Verifying the installation
Run the launcher with the version flag. This does not connect to any database.
sql -VIt prints the SQLcl version and build. If that works, the installation is complete. You can also print the command-line help:
sql -HThree ways to start SQLcl
How you start SQLcl decides whether it connects immediately, waits for you, or runs without interaction.
Start and connect in one step
Pass a user name and a connect identifier. SQLcl then asks for the password.
sql hr@//dbhost.example.com:1521/orclpdb1.example.comThe part after @ is an Easy Connect identifier in the form //host:port/service_name. SQLcl prompts for the password and does not echo it. If the connect identifier is a TNS alias, you write hr@ORCLPDB instead, as long as SQLcl can find your tnsnames.ora.
Start without a connection
Use /nolog to start SQLcl and skip the login. You get a prompt and connect when you are ready.
sql /nolog
SQL> connect hr@//dbhost.example.com:1521/orclpdb1.example.com
Password? (**********?) ********
Connected.This is the better habit in shared environments. A password typed as part of the launch command, such as sql hr/secret@orclpdb, is stored in your shell history and is visible to other users in the process list while SQLcl runs. Starting with /nolog and typing the password at the prompt avoids both problems.
/nolog is also the right choice when you need to set options before connecting. For example, you may need to point SQLcl at a wallet or a custom tnsnames.ora directory first, which later chapters cover.
Start for scripts
For automation, add flags that stop SQLcl from waiting for a person.
sql -S -L hr@orclpdb @report.sqlThe flags have these meanings:
| Flag | Meaning |
|---|---|
| `-S` | Silent mode. Suppresses the banner and the echo of commands, so output contains only results. |
| `-L` | Limits the login to one attempt. If the credentials are wrong, SQLcl exits instead of asking again. |
| `@report.sql` | Runs the script file after connecting. |
Without -L, a failed login in an unattended job can leave SQLcl waiting at a prompt forever. The scripting chapter covers how to supply credentials to such jobs without putting a password in the file.
Working at the SQLcl prompt
Once you have a prompt, a few basics are useful on day one:
- Press the up arrow to recall earlier commands. Unlike SQL*Plus, SQLcl keeps history across the session and lets you edit multi-line statements.
- Run
historyto list recent statements. - End a SQL statement with
;or a/on its own line. A PL/SQL block needs the/. - Type
helpto list commands. Typehelp <command>for details on one. - Leave with
exitorquit. This closes the connection and returns you to your shell.
Do not confuse the SQL> prompt in SQLcl with a connected session. After sql /nolog you see SQL> even though no database is attached. Running a query then fails because there is no connection. The next chapter shows how to confirm what you are connected to.
Common startup problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `sql: command not found` or `'sql' is not recognized` | The `bin` directory is not on `PATH`, or the terminal was not restarted | Add the directory to `PATH` and open a new terminal, or run the launcher by full path |
| A message about a missing or unsupported Java version | Old JDK, or `JAVA_HOME` pointing to the wrong place | Install a supported JDK and correct `JAVA_HOME` |
| A different program starts when you type `sql` | Another tool on `PATH` uses the same name | Run `which sql` (Linux/macOS) or `where sql` (Windows) and put the SQLcl directory earlier on `PATH` |
| Startup works but connecting fails | A network or naming problem, not an installation problem | See the troubleshooting chapter for ORA- error codes |
Exercise
You are on a shared server. You want to start SQLcl, connect as HR to //dbhost.example.com:1521/orclpdb1.example.com, and avoid exposing the password in shell history. Which launch method do you use, and what does sql -S -L hr@orclpdb @report.sql do differently?
Show answer
Start with sql /nolog, then run connect hr@//dbhost.example.com:1521/orclpdb1.example.com and type the password at the prompt, so it never appears in the command line or shell history. The sql -S -L hr@orclpdb @report.sql command runs non-interactively. -S suppresses the banner and command echo, -L allows only one login attempt so a bad login exits instead of waiting, and @report.sql runs the script after connecting.