DEV Community

Mario García
Mario García

Posted on • Updated on

Development Environment for Rust and Python on Linux

If you're a Python developer, willing to learn Rust or you're already on the way to integrate it with your projects, there are a few crates (libraries) like PyO3 or CPython for running and interacting with Python code directly from Rust or writing native Python modules.

Through this article you will learn how to configure a development environment on Linux for your Rust and Python projects.

Requirements

To develop apps using Rust and Python, you can use PyO3 or CPython, both with support for Python 3.5 and up. While CPython also works with Python 2.7, that version is no longer maintained as its support ended on January 1st this year.

Python comes preinstalled on most of the Linux distributions out there, so it won't be necessary to install it from the repositories, otherwise you can always use the package manager of your distro.

Both crates mentioned above require the Nightly version of Rust. rustup (Rust installer) is available in the repositories of some distributions, elseways you can follow the instructions on rustup.rs.

Dependencies

Cargo (package manager) and rustc (compiler) will be available on your system after installing Rust. For these tools you need the following packages for them to work properly, according to the GitHub repository:

  • git
  • curl (on Unix)
  • pkg-config (on Unix, used to figure out the libssl headers/libraries)
  • OpenSSL headers (only for Unix, this is the libssl-dev package on Ubuntu)
  • a C compiler (e.g. GCC)

When using Cargo to create a new project, a Git repository will be initialized, and Rust also requires a C compiler for building the binary of the app.

As you're also configuring Python for using it with Rust, you need to install Poetry and pyenv. The wiki of pyenv has a section (Common build problems) where you can find information about the packages required and how to install them on your distro.

  • Debian/Ubuntu:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
Enter fullscreen mode Exit fullscreen mode
  • Fedora:
sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils curl \
&& sudo yum groupinstall 'Development Tools'
Enter fullscreen mode Exit fullscreen mode
  • Centos:
sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils curl
Enter fullscreen mode Exit fullscreen mode
  • Arch and derivatives:
sudo pacman -S --needed base-devel openssl zlib bzip2 readline sqlite curl \
llvm ncurses xz tk libffi python-pyopenssl git
Enter fullscreen mode Exit fullscreen mode

Rust

On Arch Linux and derivatives, rustup is available from the repositories and you can install it running the following command:

sudo pacman -S rustup
Enter fullscreen mode Exit fullscreen mode

After installing rustup, you should install both Stable and Nightly versions of Rust:

rustup install stable nightly
Enter fullscreen mode Exit fullscreen mode

Other distributions

For other distros, run the following in your terminal, according to rustup.rs:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

The above command will download rustup and guide you through the installation process. In the terminal it will display the following:

Current instalation options:

  default host triple: x86_64-unknown-linux-gnu
    default toolchain: stable
 modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
Enter fullscreen mode Exit fullscreen mode

The line default host triple: x86_64-unknown-linux-gnu shows the architecture of the system where you're installing. In default toolchain: stable appears the version of Rust that will be installed by default.

To continue, choose option 1 (default option) and then press Enter.

  stable installed - rustc 1.43.0 (4fb7144ed 2020-04-20)

Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env
Enter fullscreen mode Exit fullscreen mode

After finishing the installation, rustup shows the version of Rust installed and it indicates to add the Cargo's bin directory to the PATH environment variable. This will be done automatically the next time you open the terminal.

To configure the current shell, run the following command:

source $HOME/.cargo/env
Enter fullscreen mode Exit fullscreen mode

And now you should install the Nightly version of Rust:

rustup install nightly
Enter fullscreen mode Exit fullscreen mode

Python

Python comes preinstalled in many Linux distros but if it wasn't the case you can install it from the repositories. Note that the package may be found as python or python3.

You can always use the version of Python available in the system or installed from the repositories but if you require a specific version for your project you may find useful to have pyenv, and Poetry or pipenv to manage the dependencies of the app.

Poetry and pyenv can be installed from the repositories of some distros or you can follow the instructions of the official documentation.

  • Arch and derivatives:
sudo pacman python-poetry pyenv
Enter fullscreen mode Exit fullscreen mode
  • Fedora:
sudo yum install poetry \
&& curl https://pyenv.run | bash
Enter fullscreen mode Exit fullscreen mode
  • Other distros:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 \
&& curl https://pyenv.run | bash
Enter fullscreen mode Exit fullscreen mode

Configuration

After installing pyenv you must add the following lines to your bashrc:

  • export PATH="$HOME/.pyenv/bin:$PATH"
  • eval "$(pyenv init -)"
  • eval "$(pyenv virtualenv-init -)"

by doing:

echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc && \
echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

To restart the current shell for the configuration to take effect, run the following command:

exec $SHELL
Enter fullscreen mode Exit fullscreen mode

Now the development environment for your Rust and Python projects is configured.

Top comments (0)