Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install the cryptography Library for Python

Install the cryptography package on Linux and Windows with pip, and handle the common errors.

How-To Guides1 min read10 screenshots

Linux

Step 1: Check Python and pip

python3 --version && pip --version
Checking Python and pip

Step 2: Install the package

pip install cryptography
The externally managed environment error

On new Linux systems (Debian 12, Ubuntu 23.04 and newer) pip may refuse with error: externally-managed-environment. This protects the system Python. Use a virtual environment:

python3 -m venv myenv
source myenv/bin/activate
pip install cryptography
Installing in a virtual environment

Step 3: Check the install

pip show cryptography
The package information
python -c "import cryptography; print(cryptography.__version__)"
The cryptography version

Windows

Step 1: Check Python and pip

python --version
pip --version
Checking Python and pip on Windows

Step 2: Install the package

pip install cryptography
Installing cryptography on Windows

Step 3: Check the install

pip show cryptography
The package information on Windows
python -c "import cryptography; print(cryptography.__version__)"
The version on Windows

Troubleshooting

If the build fails on Linux, install the build tools and the headers:

sudo apt install build-essential libssl-dev libffi-dev python3-dev
Installing the build dependencies

On Red Hat based systems: sudo dnf install gcc openssl-devel libffi-devel python3-devel.

Upgrade pip if you see build errors, because new pip versions install ready-made wheels:

python3 -m pip install --upgrade pip

A quick example

from cryptography.fernet import Fernet

key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"secret message")
print(f.decrypt(token))

Quick recap

  • pip install cryptography, in a virtual environment on new Linux systems.
  • Check with pip show cryptography.
  • Install the build tools if the install fails.