Why and How to install OCI CLI in a Virtual Environment on Oracle Cloud

Introduction

Oracle Cloud Infrastructure (OCI) Command Line Interface (CLI) is a small-footprint tool that lets you create and manage resources in Oracle Cloud using a command line interface. It enables you to automate tasks and makes things easier.

The Quickstart documentation provides quick installation instructions. However, installations via RPM (Red Hat Package Manager) on Base and Exadata Database VMs on Oracle Cloud might lead to conflicts and issues while updating the VM’s operating system.

Suppose you need to install OCI CLI on the database host, e.g., to scale up Exadata VM Cluster from within the system. In that case, it’s highly recommended to install the CLI manually and ensure it runs in a virtual environment. This will ensure not to conflict with the RPMs on the database machine.

This blog post provides a step-by-step guide on manual installation on Oracle Linux 7, the default operating system for Base and Exadata Database VMs in Oracle Cloud.

The Environment

  • VM DB System in Oracle Cloud running Oracle Linux 7.

Step 1: Install Python

The latest version of Python! Installing python from the yum repository via sudo yum install python3 will install Python version 3.6. If you have it on your system and run OCI CLI, you will get the following warning:

[opc@hostfra bin]$ oci -v
/home/opc/lib/oracle-cli/lib64/python3.6/site-packages/oci/_vendor/httpsig_cffi/sign.py:10: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
  from cryptography.hazmat.backends import default_backend  # noqa: F401
3.15.2

You, and your tools, expect just a JSON response. If the response includes a warning in text format, you will not be able to process the response correctly.

If not already done, add the ol7_latest yum repository:

[opc@vmdbhost ~]$ sudo vi /etc/yum.repos.d/ol7.repo

[ol7_latest]
name=Oracle Linux $releasever Latest ($basearch)
baseurl=http://yum-fra.oracle.com/repo/OracleLinux/OL7/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

Install python 3.8 from the Software Collection Library (SCL) as follows:

[opc@vmdbhost ~]$ sudo yum install -y oracle-softwarecollection-release-el7
[opc@vmdbhost ~]$ sudo yum install -y scl-utils rh-python38
[opc@vmdbhost ~]$ scl enable rh-python38 bash

[opc@vmdbhost ~]$ python --version
Python 3.8.13

Step 2: Create and Configure a Virtual Environment

Choose a directory where you want to install the virtual environment. Run the following commands to create and activate the virtual environment:

[opc@vmdbhost ~]$ mkdir virtual_env
[opc@vmdbhost ~]$ cd virtual_env/
[opc@vmdbhost virtual_env]$ python3.8 -m venv oracle-cli
[opc@vmdbhost virtual_env]$ source oracle-cli/bin/activate
(oracle-cli) [opc@vmdbhost virtual_env]$

Step 3: Install OCI CLI using PyPI

The Python Package Index (PyPI) is a software repository for the Python programming language. Run the following command to upgrade PyPI and install OCI CLI:

(oracle-cli) [opc@vmdbhost virtual_env]$ pip install --upgrade pip
(oracle-cli) [opc@vmdbhost virtual_env]$ pip install oci-cli

(oracle-cli) [opc@vmdbhost virtual_env]$ oci -v
3.16.0

Step 4: Authenticate OCI CLI via Resource Principals

For Base and Exadata Database VMs, use the Resource Principals authentication method described in Authentication Method #3: Resource Principals in this blog post.

Run your CLI commands from the virtual environment:

[opc@vmdbhost ~]$ . virtual_env/oracle-cli/bin/activate
(oracle-cli) [opc@vmdbhost ~]$ oci ...

Conclusion

If you need to install OCI CLI on a database VM in Oracle Cloud, install it manually and ensure it runs in a virtual environment to avoid issues when updating the operating system. Choose Resource Principals for authentication for higher security without the need to manage and store user credentials on the VM host.

Further Reading

Would you like to get notified when the next post is published?