How to Install Python and Pip on FreeBSD 14.0

Updated on November 15, 2024
How to Install Python and Pip on FreeBSD 14.0 header image

Introduction

Python is a modern high-level programming language for developing web applications, running artificial intelligence (AI) models, and performing data analysis. Pip, a standard package manager for Python, allows you to install and manage libraries or dependencies in your Python project.

This article explains how to install Pip on FreeBSD 14. You will also install multiple Python versions and create an isolated development environment using the venv module.

Prerequisites

Before you begin:

Install Python

The FreeBSD operating system preinstalls Python by default, and you can also pull it from the package repositories using the pkg package manager. However, those Python versions might be out of date. Follow the steps below to verify the default Python version and install the latest Python version on your server by building the application from source.

  1. View the default Python version.

    console
    $ python --version
    

    Output:

    Python 3.11.9
  2. Download the latest Python version release file. For example, version 3.13. Visit the Python releases page to verify the latest version.

    console
    $ wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz
    
  3. Extract all files from the archive.

    console
    $ tar -xvzf Python-3.13.0.tgz
    
  4. Switch to the new Python directory. For example, Python-3.13.0.

    console
    $ cd Python-3.13.0
    
  5. Run the configure script to initialize the build process.

    console
    $ ./configure --with-system-ffi --with-computed-gotos --enable-shared
    
  6. Use make to build Python from source.

    console
    $ make -j$(sysctl -n hw.ncpu)
    
  7. Install Python. The altinstall option installs the Python version without overwriting the default version.

    console
    $ sudo make altinstall
    
  8. View the new Python version.

    console
    $ python3.13 --version
    

    Output:

    Python 3.13.0
  9. Verify that the default version is available and unchanged.

    console
    $ python3 --version
    

    Output:

    Python 3.11.9

    You have installed the latest Python version on your FreeBSD server.

  10. Run the following command to remove the pre-installed version.

    console
    $ sudo rm /usr/local/bin/python3
    
  11. Allow the python3 command to use the new version by default.

    console
    $ sudo ln -s /usr/local/bin/python3.13 /usr/local/bin/python3
    

Install PIP on FreeBSD 14

Pip is the standard Python package manager for installing and managing project dependencies. Depending on the running Python version, follow the steps below to verify and install Pip on FreeBSD 14 server.

  1. View the default Pip version on your server.

    console
    $ pip --version
    

    Verify that the command fails with the following output:

    -su: pip: not found
  2. Download the latest Pip installation script.

    console
    $ wget https://bootstrap.pypa.io/get-pip.py
    
  3. Install a Pip version based on your Python version, such as the Python 3.13 you installed earlier.

    console
    $ sudo python3.13 get-pip.py
    

    Output:

    Collecting pip
      Using cached pip-24.2-py3-none-any.whl.metadata (3.6 kB)
    Using cached pip-24.2-py3-none-any.whl (1.8 MB)
    Installing collected packages: pip
      Attempting uninstall: pip
        Found existing installation: pip 24.2
        Uninstalling pip-24.2:
          Successfully uninstalled pip-24.2
    Successfully installed pip-24.2
    • Install Pip using the default Python version on your server.

      console
      $ sudo python3 get-pip.py
      
  4. View the new Pip version.

    console
    $ pip3.13 --version
    

    Output:

    pip 24.2 from /usr/local/lib/python3.13/site-packages/pip (python 3.13)

    Find out how to install pip on ubuntu to manage Python packages effectively in your development environment.

Test the Python Package

Follow the steps below to test the Python package on FreeBSD.

  1. Access the Python shell.

    console
    $ python3.13
    
  2. Run the following command to display a Hello World, Greetings from Vultr! message in your terminal.

    python
    >>> print('Hello World, Greetings from Vultr!')
    

    Output:

    Hello World, Greetings from Vultr!
  3. Exit the Python shell.

    python
    >>> quit()
    
  4. Install a new Python module, such as flask using Pip.

    console
    $ pip3.13 install flask
    

    Output:

    Collecting flask
      Downloading flask-3.0.3-py3-none-any.whl.metadata (3.2 kB)
    Collecting Werkzeug>=3.0.0 (from flask)
      Downloading werkzeug-3.0.4-py3-none-any.whl.metadata (3.7 kB)
    Collecting Jinja2>=3.1.2 (from flask)
      Downloading jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB)
    Collecting itsdangerous>=2.1.2 (from flask)

Install Multiple Python Versions

Multiple projects may require different Python versions on your server. You can install new Python versions using the pkg package manager or use a project management tool such as pyenv to install and manage multiple Python versions.

  1. Update the server's package information index.

    console
    $ sudo pkg update
    
  2. Install the git dependency package.

    console
    $ sudo pkg install -y git
    
  3. Install the pyenv tool.

    console
    $ sudo pkg install -y pyenv
    
  4. View the available Python versions you can install on your server.

    console
    $ pyenv install --list
    

    Output:

    Available versions:
     2.1.3
    .........
     3.9.13
     3.9.14
     3.9.15
    .........
     3.10.10
     3.10.11
    ..........
     3.11.8
     3.11.9
     3.11.10
     3.12.0
     3.12-dev
    .........
     3.12.6
     3.13.0rc2
     3.13.0rc2t
     3.13-dev
     3.13t-dev
     3.14-dev
     3.14t-dev
    .........
  5. Install a specific Python version such as 3.14-dev.

    console
    $ pyenv install 3.14-dev
    
  6. Install another version, such as 3.12.6.

    console
    $ pyenv install 3.12.6
    
  7. View all installed Python versions in your environment.

    console
    $ pyenv versions
    

    Output:

    * system (set by /home/user/.pyenv/version)
      3.12.6
      3.14-dev
  8. Set a specific Python version as the default in your environment. For instance, 3.12.6.

    console
    $ pyenv global 3.12.6
    
  9. View the active Python version in your environment.

    console
    $ pyenv version
    

    Output:

    3.12.6 (set by /home/user/.pyenv/version)

Create a Python Virtual Environment

Python uses the venv module to create isolated virtual environments on your server, allowing you to separate specific dependencies in your project from the system-wide Python environment. Follow the steps below to create a new virtual environment using the venv module.

  1. Create a new myenv virtual environment.

    console
    $ python3 -m venv myenv
    
  2. Activate the myenv virtual environment.

    console
    $ . myenv/bin/activate
    

    Verify that your terminal prompt changes and includes the name of the new environment similar to the output below.

    console
    (myenv) user@vultr:~ $
    
  3. View the active python binary path.

    console
    $ which python3
    

    Verify that the Python binary is active in your virtual environment path similar to the output below.

    /home/user/myenv/bin/python3
  4. Install a Python package such as requests using Pip in the new environment.

    console
    $ pip install requests
    

    Output:

    Successfully installed certifi-2024.8.30 charset-normalizer-3.4.0 idna-3.10 requests-2.32.3 urllib3-2.2.3
  5. List all available Python modules in the environment.

    console
    $ pip freeze
    

    Output:

    certifi==2024.8.30
    charset-normalizer==3.4.0
    idna==3.10
    requests==2.32.3
    urllib3==2.2.3
  6. Exit the virtual environment.

    console
    $ deactivate
    

Conclusion

You have installed Pip on FreeBSD 14 along with Python. Additionally, you've installed multiple Python versions to use with different projects and packages on the server. Python supports various tools that can be integrated with other applications, such as database servers, to match your project needs. For further details, please visit the Python documentation.