How to install SQLAlchemy SQL Toolkit and Object Relational Mapper using PIP

How to install SQLAlchemy SQL Toolkit and Object Rational Mapper using PIP

This blog post will explain how to install SQLAlchemy SQL Toolkit and Object Relational Mapper using pip on Linux. SQLAlchemy is an open-source Python library that provides an SQL toolkit and an Object Relational Mapper for database interactions. SQLAlchemy allows developers to work with databases using Python objects with efficient and flexible database access. The key features offered by SQLAlchemy are powerful ORM, database schema migration support, multiple database compatibility, database connection pooling and transaction management, and comprehensive embedded domain-specific language for SQL in Python.

Installing the SQLAlchemy SQL Toolkit will require a Python virtual environment and take up to 15 minutes. Let’s get started!

Prerequisites

  • A server running Ubuntu 24.04 or any Linux OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

It is recommended to update the system packages to their latest versions available before any software installation:

sudo apt update -y && sudo apt upgrade -y

Step 2. Install Python

Since we need to install SQLAlchemy with a Python virtual environment, we need to install Python and its dependencies. To do that, execute the command below:

sudo apt install python3.12 python3.12-dev python3.12-venv python3-pip -y

To check the installed Python version, execute the command below:

python3 -V

You should get the following output:

root@host:~# python3 -V
Python 3.12.3

Step 3. Create a Python Virtual Environment

First, we need to create a virtual environment, which is an isolated space where you can work on your Python projects separately from your system-installed Python. To do that, execute the command below:

python3.12 -m venv sqlalchemy

Once the virtual environment is created, we need to activate it:

source sqlalchemy/bin/activate

After this, you will be in the virtual environment:

root@host:/opt# source sqlalchemy/bin/activate
(sqlalchemy) root@host:/opt#

Step 4. Install SQLAlchemy

To install SQLAclhemy using PIP, you can execute the command below into the activated environment:

pip3 install sqlalchemy

After successful installation, you will get the following output:

(sqlalchemy) root@host:/opt# pip3 install sqlalchemy
Collecting sqlalchemy
  Downloading SQLAlchemy-2.0.36-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.7 kB)
Collecting typing-extensions>=4.6.0 (from sqlalchemy)
  Downloading typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Collecting greenlet!=0.4.17 (from sqlalchemy)
  Downloading greenlet-3.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.metadata (3.8 kB)
Downloading SQLAlchemy-2.0.36-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 5.6 MB/s eta 0:00:00
Downloading greenlet-3.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (613 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 613.1/613.1 kB 5.2 MB/s eta 0:00:00
Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Installing collected packages: typing-extensions, greenlet, sqlalchemy
Successfully installed greenlet-3.1.1 sqlalchemy-2.0.36 typing-extensions-4.12.2
(sqlalchemy) root@host:/opt#

To check the installed SQLAlchemy version and other info, execute the following command:

pip3 show sqlalchemy

You should get output similar to this:

(sqlalchemy) root@host:/opt# pip3 show sqlalchemy
Name: SQLAlchemy
Version: 2.0.36
Summary: Database Abstraction Library
Home-page: https://www.sqlalchemy.org
Author: Mike Bayer
Author-email: mike_mp@zzzcomputing.com
License: MIT
Location: /opt/sqlalchemy/lib/python3.12/site-packages
Requires: greenlet, typing-extensions
Required-by: 
(sqlalchemy) root@host:/opt# 

Another way to check the version is from the Python command line. First login to the Python terminal:

python3

You will get the following:

(sqlalchemy) root@host:/opt# python3
Python 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Enter the following commands one by one in this terminal:

import sqlalchemy

sqlalchemy.__version__

You will get the version of the SQLAlchemy:

(sqlalchemy) root@host:/opt# python3
Python 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> sqlalchemy.__version__
'2.0.36'
>>> 

Step 5. Install SQLAlchemy DBAPI drivers

SQLAlchemy requires DBAPI drivers for PostgreSQL or MySQL. Depending on which database you want to use, there are different installation commands.

If you are using MySQL database service to install SQLAlchemy DBAPI drivers, you can use the following command:

apt install python3-mysqldb -y

If you are using the PostgreSQL database service to install SQLAlchemy DBAPI drivers, you can use the following command:

apt install python3-psycopg2 -y

SQLAlchemy components and interactions

In this paragraph, we will explain the components and interactions of SQLAlchemy.

SQLAlchemy deals directly with the data through RAW SQL, SQL Expression Language, and Object Relational Mapper. These are the interactions of SQLAlchemy with the data within the database.

SQLAlchemy has the following components: Engine, Dialect, and MetaData.

The Engine is the entryway for the SQLAlchemy application.

The Dialect is the framework of SQLAlchemy that interacts with the DBAPI database and includes multiple database services, such as MySQL, PostgreSQL, Oracle, SQLite, Microsoft SQL Server, and Oracle.

The MetaData consists of multiple Python assets like tables and other schema-level items.

Congratulations

That’s it. You’ve successfully installed the SQLAlchemy Toolkit and Object Relational Mapper using PIP on Ubuntu 24.04.

Of course, you don’t have to install it yourself if you have difficulties or are unfamiliar with Python, Ubuntu, or Linux OS. You can always contact our technical support. You only need to sign up for one of our fully-managed Linux VPS plans and submit a support ticket. We are available 24/7 and will take care of your request immediately.

If you liked this post about installing SQLAlchemy Toolkit and Object Relational Mapper using PIP on Ubuntu 24.04, please share it with your friends or comment below.

Leave a Comment